<?php

namespace EasyShippingRate\Frontend\Blocks;

use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface;
use EasyShippingRate\Frontend\Frontend;

/**
 * Shows the free shipping notice on the block-based Checkout, which can't receive it the
 * same way as the classic checkout.
 */
class FreeShippingNotice implements IntegrationInterface {

	const HANDLE = 'esraw-free-shipping-notice';

	public static function boot(): void {
		if ( ! function_exists( 'woocommerce_store_api_register_endpoint_data' ) ) {
			return;
		}

		woocommerce_store_api_register_endpoint_data(
			array(
				'endpoint'        => \Automattic\WooCommerce\StoreApi\Schemas\V1\CartSchema::IDENTIFIER,
				'namespace'       => 'easy-shipping-rate',
				'data_callback'   => array( self::class, 'cart_data' ),
				'schema_callback' => array( self::class, 'cart_schema' ),
				'schema_type'     => ARRAY_A,
			)
		);

		add_action(
			'woocommerce_blocks_checkout_block_registration',
			function ( $registry ) {
				$registry->register( new self() );
			}
		);
	}

	/**
	 * @return array{show: bool, message: string}
	 */
	public static function cart_data(): array {
		$remaining = ( new Frontend( 'esraw', ESRAW_VERSION ) )->free_shipping_remaining();
		if ( is_null( $remaining ) ) {
			return array(
				'show'    => false,
				'message' => '',
			);
		}
		$amount = html_entity_decode( wp_strip_all_tags( wc_price( $remaining ) ) );

		// The rel attribute is our hook to style only this notice (see esraw-blocks.css).
		$message = sprintf(
			/* translators: %s: formatted price amount */
			esc_html__( 'Spend %s more to get free shipping', 'easy-shipping-rate' ),
			esc_html( $amount )
		);
		$link = sprintf(
			'<a href="%s" rel="esraw-free-shipping">%s</a>',
			esc_url( wc_get_page_permalink( 'shop' ) ),
			esc_html__( 'Continue shopping', 'easy-shipping-rate' )
		);

		return array(
			'show'    => true,
			'message' => $message . '<br/>' . $link,
		);
	}

	/**
	 * @return array<string, array<string, mixed>>
	 */
	public static function cart_schema(): array {
		return array(
			'show'    => array(
				'description' => __( 'Whether the free shipping notice should be shown.', 'easy-shipping-rate' ),
				'type'        => 'boolean',
				'readonly'    => true,
			),
			'message' => array(
				'description' => __( 'The free shipping notice text.', 'easy-shipping-rate' ),
				'type'        => 'string',
				'readonly'    => true,
			),
		);
	}

	public function get_name() {
		return 'easy-shipping-rate';
	}

	public function initialize() {
		wp_register_script(
			self::HANDLE,
			ESRAW_URL . 'src/Frontend/js/esraw-free-shipping-notice.js',
			array( 'wp-data', 'wc-blocks-data-store' ),
			ESRAW_VERSION,
			true
		);

		wp_enqueue_style(
			self::HANDLE,
			ESRAW_URL . 'src/Frontend/css/esraw-blocks.css',
			array(),
			ESRAW_VERSION
		);

		add_filter(
			'script_loader_tag',
			function ( $tag, $handle ) {
				if ( self::HANDLE !== $handle ) {
					return $tag;
				}
				return preg_replace( '/<script /', '<script type="module" ', $tag, 1 );
			},
			10,
			2
		);
	}

	public function get_script_handles() {
		return array( self::HANDLE );
	}

	public function get_editor_script_handles() {
		return array();
	}

	public function get_script_data() {
		return array();
	}
}
