<?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_EditOrder
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */

namespace Mageplaza\EditOrder\Plugin\Component;

use Magento\Framework\App\RequestInterface;
use Magento\Framework\UrlInterface;
use Magento\Ui\Component\MassAction as ComponentMassAction;
use Mageplaza\EditOrder\Helper\Data as HelperData;

/**
 * Class MassAction
 * @package Mageplaza\EditOrder\Plugin\Component
 */
class MassAction
{
    /**
     * @var RequestInterface
     */
    protected $_request;

    /**
     * @var UrlInterface
     */
    protected $_url;

    /**
     * @var HelperData
     */
    protected $_helperData;

    /**
     * MassAction constructor.
     *
     * @param RequestInterface $request
     * @param UrlInterface $url
     * @param HelperData $helperData
     */
    public function __construct(
        RequestInterface $request,
        UrlInterface $url,
        HelperData $helperData
    ) {
        $this->_request            = $request;
        $this->_url                = $url;
        $this->_helperData         = $helperData;
    }

    /**
     * @param ComponentMassAction $massAction
     */
    public function afterPrepare(ComponentMassAction $massAction)
    {
        if (
            $this->_helperData->isEnabled() &&
            $this->_helperData->isEnabledTrackingShipment() &&
            $this->_request->getFullActionName() === 'sales_shipment_index'
        ) {
            $config = $massAction->getData('config');

            $addTrackAction = [
                'component' => 'uiComponent',
                'type'      => 'mp_shipment_add_track',
                'label'     => __('Add Track Shipping for Order')
            ];

            $config['actions'][] = $addTrackAction;

            $massAction->setData('config', $config);
        }
    }
}
