<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Config;

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Config\Abstraction\AbstractSingleConfig;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Config\Config;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Service\Dependency\Binder\DependencyBinderCollection;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Service\Dependency\Resolver\DependencyResolver;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Service\Listener\Items\Conditional\ConditionalServiceListener;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Service\Listener\Items\Hookable\HookableServiceListener;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Service\Listener\Items\Initable\InitableServiceListener;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Service\Listener\Items\Registrable\RegistrableServiceListener;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Service\Container\ServiceContainer;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Service\Listener\ListenerCollection;
use DropshippingExportFreeVendor\WPDesk\View\Resolver\ChainResolver;
use DropshippingExportFreeVendor\WPDesk\View\Resolver\DirResolver;
use DropshippingExportFreeVendor\WPDesk\View\Renderer\Renderer;
use DropshippingExportFreeVendor\WPDesk\View\Renderer\SimplePhpRenderer;
use DropshippingExportFreeVendor\WPDesk\View\Resolver\Resolver;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Request\Request;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\View\FormView;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\CheckoutOrderCreatorInterface;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\CheckoutPaymentExecutorInterface;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\LockedCheckoutPaymentExecutor;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\StoreApiOrderCreator;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\IdempotencyStore;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\ProductResolver;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\HeaderValidator;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\SessionLoader;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Handler\CreateCheckoutSession;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Handler\GetCheckoutSession;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Handler\UpdateCheckoutSession;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Handler\CompleteCheckoutSession;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Handler\CancelCheckoutSession;
use DropshippingExportFreeVendor\Monolog\Logger;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields\ExportOptionsFormFields;
use DropshippingExportFreeVendor\WPDesk\Logger\Settings as LoggerSettings;
use DropshippingExportFreeVendor\WPDesk\Logger\SimpleLoggerFactory;
use Automattic\WooCommerce\StoreApi\Utilities\CartController;
use Automattic\WooCommerce\StoreApi\Utilities\OrderController;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\CheckoutOrderFinder;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\CheckoutStatusResolver;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Payment\PaymentHandlerResolver;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Payment\Processor\GooglePayPaymentProcessor;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Payment\Processor\MockPaymentProcessor;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Payment\Processor\StoreApiPaymentExecutor;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\State\CheckoutStateRepository;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\State\CheckoutSessionDataRepository;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Response\CouponTitleFormatter;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Integration\WcPaymentConfigProvider;
/**
 * Class ServiceConfig, configuration class for services and it's dependencies.
 */
final class ServiceConfig extends AbstractSingleConfig
{
    public const ID = 'service';
    public const LOGGER_NAME = 'dropshipping-export';
    public function get(): array
    {
        $config = $this->get_config();
        $request = new Request();
        return ['container' => ServiceContainer::class, 'resolver' => DependencyResolver::class, 'binder_collection' => DependencyBinderCollection::class, 'listener_collection' => ListenerCollection::class, 'bind' => [Config::class => $config, Request::class => $request, Resolver::class => function () use ($config) {
            $resolver = new ChainResolver();
            $resolver->appendResolver(new DirResolver($config->get_param('templates.dir')->get()));
            $resolver->appendResolver(new DirResolver($config->get_param('templates.form_fields_dir')->get()));
            $resolver->appendResolver(new DirResolver($config->get_param('templates.core_dir')->get()));
            $resolver->appendResolver(new DirResolver($config->get_param('templates.core_form_fields_dir')->get()));
            return $resolver;
        }, Renderer::class => SimplePhpRenderer::class, Logger::class => function () {
            $logger_options = new LoggerSettings();
            $logger_options->use_wp_log = \false;
            return (new SimpleLoggerFactory(self::LOGGER_NAME, $logger_options))->getLogger();
        }, ExportOptionsFormFields::class => ['uid' => $this->get_uid($request)], ProductResolver::class => ProductResolver::class, IdempotencyStore::class => IdempotencyStore::class, CartController::class => CartController::class, OrderController::class => OrderController::class, HeaderValidator::class => HeaderValidator::class, SessionLoader::class => SessionLoader::class, CheckoutStateRepository::class => CheckoutStateRepository::class, CheckoutSessionDataRepository::class => CheckoutSessionDataRepository::class, CouponTitleFormatter::class => CouponTitleFormatter::class, CheckoutOrderFinder::class => CheckoutOrderFinder::class, CheckoutStatusResolver::class => CheckoutStatusResolver::class, CheckoutOrderCreatorInterface::class => StoreApiOrderCreator::class, CheckoutPaymentExecutorInterface::class => LockedCheckoutPaymentExecutor::class, StoreApiPaymentExecutor::class => StoreApiPaymentExecutor::class, GooglePayPaymentProcessor::class => GooglePayPaymentProcessor::class, MockPaymentProcessor::class => MockPaymentProcessor::class, PaymentHandlerResolver::class => function () {
            $binders = new DependencyBinderCollection();
            $binders->add($this->get());
            $resolver = new DependencyResolver($binders);
            return new PaymentHandlerResolver([$resolver->resolve(GooglePayPaymentProcessor::class), $resolver->resolve(MockPaymentProcessor::class)]);
        }, CreateCheckoutSession::class => CreateCheckoutSession::class, GetCheckoutSession::class => GetCheckoutSession::class, UpdateCheckoutSession::class => UpdateCheckoutSession::class, CompleteCheckoutSession::class => CompleteCheckoutSession::class, CancelCheckoutSession::class => CancelCheckoutSession::class, WcPaymentConfigProvider::class => WcPaymentConfigProvider::class], 'forbidden' => [FormView::class], 'listeners' => [ConditionalServiceListener::class, RegistrableServiceListener::class, HookableServiceListener::class, InitableServiceListener::class]];
    }
    public function get_id(): string
    {
        return self::ID;
    }
    protected function get_uid(Request $request): string
    {
        $uid = $request->get_param('post.uid')->get();
        if (empty($uid)) {
            $uid = $request->get_param('get.uid')->get();
        }
        return !empty($uid) ? $uid : '';
    }
}
