<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_ProductAttachments
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */
declare(strict_types=1);

namespace Mageplaza\ProductAttachments\Model\Resolver;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\App\RequestInterface;
use Mageplaza\ProductAttachments\Helper\Data;
use Mageplaza\ProductAttachments\Model\Api\ProductAttachmentsRepository;

/**
 * Class GetConfiguration
 * @package Mageplaza\ProductAttachments\Model\Resolver
 */
class GetConfiguration implements ResolverInterface
{
    /**
     * @var RequestInterface
     */
    private $request;

    /**
     * @var Data
     */
    protected $helperData;

    /**
     * @var ProductAttachmentsRepository
     */
    protected $productAttachment;

    /**
     * GetConfiguration constructor.
     *
     * @param Data $helperData
     * @param ProductAttachmentsRepository $productAttachmentsRepository
     * @param RequestInterface $request
     */
    public function __construct(
        Data $helperData,
        ProductAttachmentsRepository $productAttachmentsRepository,
        RequestInterface $request
    ) {
        $this->request           = $request;
        $this->helperData        = $helperData;
        $this->productAttachment = $productAttachmentsRepository;
    }

    /**
     * @inheritdoc
     */
    public function resolve(Field $field, $context, ResolveInfo $info, ?array $value = null, ?array $args = null)
    {
        $storeId = $args['storeId'];
        if (!$this->helperData->isEnabled($storeId)) {
            throw new GraphQlInputException(__('The module is disabled.'));
        }
        $params = $this->request->getParams();
        $params = array_merge($params, $args);
        $this->request->setParams($params);

        return $this->productAttachment->getDataConfigLocation($storeId);
    }
}
