<?php
/**
 * Conditional front-end assets.
 *
 * @package WPDudeProducts
 */

declare(strict_types=1);

namespace WPDude\Products\Frontend;

use WPDude\Products\REST\REST_Loader;
use WPDude\Products\Settings\Settings;

/**
 * Enqueue CSS/JS only when a product shortcode/block is rendered.
 */
final class Assets {

	private static bool $needed    = false;
	private static bool $localized = false;

	/**
	 * Register hooks.
	 */
	public function register(): void {
		add_action( 'wp_enqueue_scripts', array( $this, 'register_handles' ), 5 );
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ), 20 );
		add_action( 'wp_footer', array( $this, 'enqueue' ), 1 );
	}

	/**
	 * Mark assets as required.
	 */
	public static function mark_needed(): void {
		self::$needed = true;
	}

	/**
	 * Register script/style handles.
	 */
	public function register_handles(): void {
		wp_register_style(
			'wpdude-dc-frontend',
			WPDUDE_DC_URL . 'assets/css/frontend.css',
			array(),
			WPDUDE_DC_VERSION
		);

		wp_register_script(
			'wpdude-dc-frontend',
			WPDUDE_DC_URL . 'assets/js/frontend.js',
			array(),
			WPDUDE_DC_VERSION,
			true
		);

		wp_register_script(
			'wpdude-dc-buy-now',
			WPDUDE_DC_URL . 'assets/js/buy-now.js',
			array( 'wpdude-dc-frontend' ),
			WPDUDE_DC_VERSION,
			true
		);
	}

	/**
	 * Enqueue when needed.
	 */
	public function enqueue(): void {
		if ( ! self::$needed ) {
			return;
		}

		wp_enqueue_style( 'wpdude-dc-frontend' );
		wp_enqueue_script( 'wpdude-dc-frontend' );
		wp_enqueue_script( 'wpdude-dc-buy-now' );

		if ( self::$localized ) {
			return;
		}
		self::$localized = true;

		wp_localize_script(
			'wpdude-dc-frontend',
			'wpdudeDc',
			array(
				'restBase'     => esc_url_raw( trailingslashit( rest_url( REST_Loader::NAMESPACE ) ) ),
				'confirmUrl'   => esc_url_raw( rest_url( REST_Loader::NAMESPACE . '/confirm-payment' ) ),
				'restNonce'    => wp_create_nonce( 'wp_rest' ),
				'localConfirm' => Settings::allows_local_payment_confirm(),
				'i18n'         => array(
					'confirming'     => __( 'Your payment is being confirmed. This page will update shortly.', 'dude-commerce' ),
					'checkingStripe' => __( 'Checking with Stripe…', 'dude-commerce' ),
					'notCompleted'   => __( 'Payment was not completed.', 'dude-commerce' ),
					'confirmFailed'  => __( 'Could not confirm payment yet.', 'dude-commerce' ),
					'waiting'        => __( 'Confirming your payment…', 'dude-commerce' ),
				),
			)
		);

		wp_localize_script(
			'wpdude-dc-buy-now',
			'wpdudeDcBuyNow',
			array(
				'restUrl'    => esc_url_raw( rest_url( REST_Loader::NAMESPACE . '/checkout' ) ),
				'nonce'      => wp_create_nonce( 'wpdude_dc_checkout' ),
				'restNonce'  => wp_create_nonce( 'wp_rest' ),
				'i18n'       => array(
					'error'         => __( 'Could not start checkout. Please try again.', 'dude-commerce' ),
					'loading'       => __( 'Redirecting to checkout…', 'dude-commerce' ),
					'chooseOptions' => __( 'Please choose all options.', 'dude-commerce' ),
				),
			)
		);
	}
}
