<?php

declare (strict_types=1);
namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Action\Api\Ucp;

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DataProvider\UcpSettingsDataProvider;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields\UcpSettingsFormFields;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Service\Listener\Items\Hookable\Hookable;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\HeaderValidator;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\IdempotencyStore;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Handler\CancelCheckoutSession;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Handler\CheckoutRouteHandler;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Handler\CompleteCheckoutSession;
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\Decorator\IdempotentCheckoutRouteHandler;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Integration\WcPaymentConfigProvider;
/**
 * Registers all UCP checkout session endpoints.
 */
final class EndpointRegistrationAction implements Hookable
{
    private const NAMESPACE = 'ucp/v1';
    private UcpSettingsDataProvider $settings;
    private WcPaymentConfigProvider $wc_payments;
    private HeaderValidator $header_validator;
    /**
     * @var CheckoutRouteHandler[]
     */
    private array $handlers;
    public function __construct(UcpSettingsDataProvider $settings, WcPaymentConfigProvider $wc_payments, HeaderValidator $header_validator, IdempotencyStore $idempotency_store, CreateCheckoutSession $create_handler, GetCheckoutSession $get_handler, UpdateCheckoutSession $update_handler, CompleteCheckoutSession $complete_handler, CancelCheckoutSession $cancel_handler)
    {
        $this->settings = $settings;
        $this->wc_payments = $wc_payments;
        $this->header_validator = $header_validator;
        $this->handlers = [new IdempotentCheckoutRouteHandler($create_handler, $idempotency_store), $get_handler, new IdempotentCheckoutRouteHandler($update_handler, $idempotency_store), new IdempotentCheckoutRouteHandler($complete_handler, $idempotency_store), new IdempotentCheckoutRouteHandler($cancel_handler, $idempotency_store)];
    }
    public function hooks(): void
    {
        \add_action('rest_api_init', [$this, 'register_routes']);
    }
    /**
     * @internal
     */
    public function register_routes(): void
    {
        if (!$this->is_available()) {
            return;
        }
        foreach ($this->handlers as $handler) {
            \register_rest_route(self::NAMESPACE, $handler->get_path(), ['methods' => $handler->get_method(), 'callback' => function ($request) use ($handler) {
                $error = $this->header_validator->validate($request);
                if (null !== $error) {
                    return $error->to_rest_response();
                }
                return $handler->handle($request);
            }, 'permission_callback' => '__return_true']);
        }
    }
    private function is_available(): bool
    {
        if (!$this->settings->has(UcpSettingsFormFields::ENABLE_UCP)) {
            return \false;
        }
        $enabled = \filter_var($this->settings->get(UcpSettingsFormFields::ENABLE_UCP), \FILTER_VALIDATE_BOOLEAN);
        return $enabled && $this->wc_payments->is_available();
    }
}
