<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory;

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Config\MenuConfig;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Helper\PluginHelper;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Request\Request;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Service\Dependency\Resolver\Abstraction\DependencyResolverInterface;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\View\Abstraction\Displayable;
use RuntimeException;
/**
 * Class ViewActionFactory, view action factory.
 *
 * @package WPDesk\Library\DropshippingExport\Factory
 */
class ViewActionFactory
{
    /**
     * @var DependencyResolverInterface
     */
    private $resolver;
    /**
     * @var PluginHelper
     */
    private $plugin_helper;
    public function __construct(DependencyResolverInterface $resolver, PluginHelper $plugin_helper)
    {
        $this->resolver = $resolver;
        $this->plugin_helper = $plugin_helper;
    }
    public function create_from_request(Request $request): Displayable
    {
        $page = $request->get_param('get.page')->getAsString();
        $action = empty($request->get_param('get.action')->getAsString()) ? MenuConfig::ACTION_DEFAULT : $request->get_param('get.action')->getAsString();
        if ($this->plugin_helper->is_plugin_page($page, $action)) {
            return $this->resolver->resolve($this->plugin_helper->get_view_by_page_action($page, $action));
        }
        throw new RuntimeException('View service not exists');
    }
}
