<?php

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

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Enums\UcpVersion;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Integration\PolicyLinkProvider;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Ucp\Integration\WcPaymentConfigProvider;
/**
 * Maps checkout data to UCP response format.
 *
 * @see https://ucp.dev/2026-01-23/schemas/shopping/checkout.json
 *
 * @phpstan-type Capability array{version: string, spec: string, schema: string, extends?: string}
 * @phpstan-type PaymentHandler array{id: string, version: string, spec: string, config: object}
 * @phpstan-type PaymentHandlerNode array<string, array<int, PaymentHandler>>
 * @phpstan-type UcpEnvelope array{version: string, capabilities: array<string, array<int, Capability>>, payment_handlers: PaymentHandlerNode}
 */
final class CheckoutResponseMapper
{
    private WcPaymentConfigProvider $config_provider;
    private PolicyLinkProvider $policy_links;
    public function __construct(WcPaymentConfigProvider $config_provider, PolicyLinkProvider $policy_links)
    {
        $this->config_provider = $config_provider;
        $this->policy_links = $policy_links;
    }
    /**
     * Map a checkout data source to UCP response array.
     *
     * @param string             $checkout_id Checkout session ID.
     * @param CheckoutDataSource $source      Data source (cart or order).
     *
     * @return array<string, mixed>
     */
    public function map(string $checkout_id, CheckoutDataSource $source): array
    {
        $response = ['ucp' => $this->format_ucp_envelope(), 'id' => $checkout_id, 'status' => $source->get_status(), 'currency' => $source->get_currency(), 'line_items' => $source->get_line_items(), 'totals' => $source->get_totals(), 'links' => $this->policy_links->get_policy_links(), 'buyer' => $source->get_buyer(), 'fulfillment' => $source->get_fulfillment(), 'discounts' => $source->get_discounts()];
        if ($source instanceof ProvidesPaymentInstruments) {
            $payment = $source->get_payment();
            if (!empty($payment['instruments'])) {
                $response['payment'] = $payment;
            }
        }
        if ($source instanceof ProvidesCheckoutMessages) {
            $messages = $source->get_messages();
            if (!empty($messages)) {
                $response['messages'] = $messages;
            }
        }
        if ($source instanceof ProvidesOrderConfirmation) {
            $response['order'] = $source->get_order();
        }
        return $response;
    }
    /**
     * Format UCP envelope.
     *
     * @return UcpEnvelope
     */
    private function format_ucp_envelope(): array
    {
        return ['version' => UcpVersion::CURRENT, 'capabilities' => ['dev.ucp.shopping.checkout' => [['version' => UcpVersion::CURRENT, 'spec' => 'https://ucp.dev/specification/checkout', 'schema' => 'https://ucp.dev/' . UcpVersion::CURRENT . '/schemas/shopping/checkout.json']], 'dev.ucp.shopping.fulfillment' => [['version' => UcpVersion::CURRENT, 'spec' => 'https://ucp.dev/specification/fulfillment', 'schema' => 'https://ucp.dev/' . UcpVersion::CURRENT . '/schemas/shopping/fulfillment.json', 'extends' => 'dev.ucp.shopping.checkout']], 'dev.ucp.shopping.discount' => [['version' => UcpVersion::CURRENT, 'spec' => 'https://ucp.dev/specification/discount', 'schema' => 'https://ucp.dev/' . UcpVersion::CURRENT . '/schemas/shopping/discount.json', 'extends' => 'dev.ucp.shopping.checkout']], 'dev.ucp.shopping.buyer_consent' => [['version' => UcpVersion::CURRENT, 'spec' => 'https://ucp.dev/specification/buyer-consent', 'schema' => 'https://ucp.dev/' . UcpVersion::CURRENT . '/schemas/shopping/buyer_consent.json', 'extends' => 'dev.ucp.shopping.checkout']]], 'payment_handlers' => $this->config_provider->get_google_pay_handler()];
    }
}
