<?php
declare(strict_types=1);

namespace Autogedal\Manufacturers\Model\Config\Source;

use Autogedal\Manufacturers\Model\Config;
use Magento\Catalog\Model\Product;
use Magento\Eav\Model\Config as EavConfig;
use Magento\Framework\Data\OptionSourceInterface;

class Manufacturers implements OptionSourceInterface
{
    public function __construct(
        private readonly EavConfig $eavConfig,
        private readonly Config $config
    ) {
    }

    public function toOptionArray(): array
    {
        $attribute = $this->eavConfig->getAttribute(Product::ENTITY, $this->config->getProductAttribute());
        if (!$attribute?->usesSource()) {
            return [];
        }

        $options = [];
        foreach ($attribute->getSource()->getAllOptions(false) as $option) {
            $options[] = [
                'value' => $option['value'],
                'label' => $option['label']
            ];
        }

        return $options;
    }
}
