<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Action\Loader\Assets;

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Helper\PluginHelper;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Service\Listener\Items\Conditional\Conditional;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Service\Listener\Items\Hookable\Hookable;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Config\Config;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Request\Request;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Action\Process\ExportProcessAction;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Action\Ajax\StopExportAjaxAction;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields\Component\ConditionalLogicComponent;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Action\Ajax\FindCategoryAjaxAction;
/**
 * Class AjaxAssetsLoaderAction, loads variables required by javascript.
 */
class AjaxAssetsLoaderAction implements Hookable, Conditional
{
    const CONDITIONAL_LOGIC = 'conditional';
    const CONDITIONAL_LOGIC_DATA = 'data';
    /**
     * @var Config
     */
    private $config;
    /**
     * @var Request
     */
    private $request;
    /**
     * @var PluginHelper
     */
    private $plugin_helper;
    public function __construct(Config $config, Request $request, PluginHelper $helper)
    {
        $this->config = $config;
        $this->request = $request;
        $this->plugin_helper = $helper;
    }
    public function isActive(): bool
    {
        return $this->plugin_helper->is_plugin_page($this->request->get_param('get.page')->getAsString(), $this->request->get_param('get.action')->getAsString());
    }
    public function hooks()
    {
        add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts'], 91);
    }
    public function enqueue_scripts()
    {
        wp_localize_script('dropshipping_admin', ExportProcessAction::AJAX_ACTION, ['nonce' => wp_create_nonce(ExportProcessAction::AJAX_NONCE)]);
        wp_localize_script('dropshipping_admin', StopExportAjaxAction::AJAX_ACTION, ['nonce' => wp_create_nonce(StopExportAjaxAction::AJAX_NONCE)]);
        wp_localize_script('dropshipping_admin', FindCategoryAjaxAction::AJAX_ACTION, ['nonce' => wp_create_nonce(FindCategoryAjaxAction::AJAX_NONCE)]);
        wp_localize_script('dropshipping_admin', self::CONDITIONAL_LOGIC, [self::CONDITIONAL_LOGIC_DATA => json_encode(ConditionalLogicComponent::get_value_type_conditions())]);
    }
}
