<?php
/**
 * Checkout Module bootstrapper.
 *
 * @package TitanCart\Modules\Checkout
 */

namespace TitanCart\Modules\Checkout;

use TitanCart\REST\Checkout\CheckoutController;

defined( 'ABSPATH' ) || exit;

class CheckoutModule {

    public function __construct() {
        add_action( 'rest_api_init', [ $this, 'register_routes' ] );
        // NOTE: order-received rewrite handled by StorefrontModule (tc_order_received).
        //       Do NOT register a competing rewrite here.
        add_shortcode( 'titancart_checkout', [ $this, 'checkout_shortcode' ] );
    }

    public function register_routes(): void {
        ( new CheckoutController() )->register_routes();
    }

    /** [titancart_checkout] */
    public function checkout_shortcode(): string {
        $manager = new CheckoutManager();
        $data    = $manager->get_checkout_data();
        return \TitanCart\Storefront\TemplateLoader::get_template(
            'checkout/checkout.php',
            compact( 'data' ),
            true
        );
    }

    public static function init(): void {
        new self();
    }
}
