<?php

declare (strict_types=1);
namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Handler;

use Automattic\WooCommerce\StoreApi\Utilities\CartController;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\CheckoutStatusResolver;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Violations\CheckoutError;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Response\CartDataSource;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Response\CheckoutResponseMapper;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Response\CouponTitleFormatter;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\SessionLoader;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\State\CheckoutSessionDataRepository;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\State\CheckoutStateRepository;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Checkout\Violations\Violations;
use WP_REST_Request;
use WP_REST_Response;
/**
 * Cancel a checkout session.
 */
final class CancelCheckoutSession implements CheckoutRouteHandler
{
    private SessionLoader $session_loader;
    private CheckoutResponseMapper $mapper;
    private CartController $cart_controller;
    private CheckoutStatusResolver $status_resolver;
    private CheckoutStateRepository $state;
    private CheckoutSessionDataRepository $session_data;
    private CouponTitleFormatter $coupon_formatter;
    public function __construct(SessionLoader $session_loader, CheckoutResponseMapper $mapper, CartController $cart_controller, CheckoutStatusResolver $status_resolver, CheckoutStateRepository $state, CheckoutSessionDataRepository $session_data, CouponTitleFormatter $coupon_formatter)
    {
        $this->session_loader = $session_loader;
        $this->mapper = $mapper;
        $this->cart_controller = $cart_controller;
        $this->status_resolver = $status_resolver;
        $this->state = $state;
        $this->session_data = $session_data;
        $this->coupon_formatter = $coupon_formatter;
    }
    public function get_path(): string
    {
        return '/checkout-sessions/(?P<id>[a-zA-Z0-9\-\_\.]+)/cancel';
    }
    public function get_method(): string
    {
        return 'POST';
    }
    public function handle(WP_REST_Request $request): WP_REST_Response
    {
        $session_id = $request->get_param('id');
        $this->session_loader->load($session_id);
        if (!$this->state->exists()) {
            return CheckoutError::checkout_not_found($session_id)->to_rest_response();
        }
        $status = $this->status_resolver->get_status($session_id);
        if ('completed' === $status) {
            return CheckoutError::checkout_already_completed()->to_rest_response();
        }
        if ('canceled' === $status) {
            return CheckoutError::checkout_already_canceled()->to_rest_response();
        }
        $this->cart_controller->empty_cart();
        $this->state->mark_canceled();
        $source = new CartDataSource(\WC()->cart, \WC()->customer, Violations::empty(), $this->session_data, $this->coupon_formatter, 'canceled');
        $response_data = $this->mapper->map($session_id, $source);
        return new WP_REST_Response($response_data, 200);
    }
}
