<?php

declare(strict_types=1);

namespace Autogedal\Labels\Controller\Adminhtml\Label;

use Autogedal\Labels\Model\Attribute\ValueOptionsProvider;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\Result\JsonFactory;

class AttributeOptions extends Action
{
    public const ADMIN_RESOURCE = 'Autogedal_Labels::labels';

    private JsonFactory $resultJsonFactory;
    private ValueOptionsProvider $valueOptionsProvider;

    public function __construct(
        Action\Context $context,
        JsonFactory $resultJsonFactory,
        ValueOptionsProvider $valueOptionsProvider
    ) {
        parent::__construct($context);
        $this->resultJsonFactory = $resultJsonFactory;
        $this->valueOptionsProvider = $valueOptionsProvider;
    }

    public function execute()
    {
        $attributeCode = (string)$this->getRequest()->getParam('attr_code', '');
        $resultJson = $this->resultJsonFactory->create();
        $resultJson->setData($this->valueOptionsProvider->get($attributeCode));

        return $resultJson;
    }
}
