<?php
/**
 * Order Button Text module.
 *
 * @package Kramar
 * @since   1.0.0
 */

declare( strict_types=1 );

namespace Kramar\Modules\Compliance;

// Prevent direct access.
defined( 'ABSPATH' ) || exit;

/**
 * Order Button Text module.
 *
 * @since 1.0.0
 */
final class Order_Button extends \Kramar\Module {

	/**
	 * {@inheritDoc}
	 */
	public function get_slug(): string {
		return 'order-button';
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_category(): string {
		return 'compliance';
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_name(): string {
		return __( 'Order Button Text', 'kramar' );
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_description(): string {
		return __( 'Override the checkout order button text to comply with EU consumer law. Default: "Order and pay".', 'kramar' );
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_default_settings(): array {
		return [
			'enabled'     => false,
			'button_text' => __( 'Order and pay', 'kramar' ),
		];
	}

	/**
	 * {@inheritDoc}
	 */
	public function sanitize_settings( array $input ): array {
		return [
			'enabled'     => ! empty( $input['enabled'] ),
			'button_text' => sanitize_text_field( $input['button_text'] ?? __( 'Order and pay', 'kramar' ) ),
		];
	}

	/**
	 * {@inheritDoc}
	 */
	protected function run(): void {
		add_filter( 'woocommerce_order_button_text', [ $this, 'override_button_text' ] );
		add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_blocks_filter' ] );
	}

	/**
	 * Override the button label in the WooCommerce Blocks checkout.
	 *
	 * @since 1.0.0
	 */
	public function enqueue_blocks_filter(): void {
		if ( ! function_exists( 'is_checkout' ) || ! is_checkout() ) {
			return;
		}

		if ( ! wp_script_is( 'wc-blocks-checkout', 'enqueued' ) && ! wp_script_is( 'wc-blocks-checkout', 'registered' ) ) {
			return;
		}

		$text = (string) $this->plugin->get_setting( $this->get_category(), $this->get_slug(), 'button_text' );
		if ( '' === $text ) {
			$text = __( 'Order and pay', 'kramar' );
		}

		wp_add_inline_script(
			'wc-blocks-checkout',
			sprintf(
				'try{const e=wc.blocksCheckout;e.registerCheckoutFilters("kramar-order-button",{placeOrderButtonLabel:()=>%s});}catch(e){}',
				wp_json_encode( $text )
			)
		);
	}

	/**
	 * Return the configured button text.
	 *
	 * @since 1.0.0
	 *
	 * @return string Button text.
	 */
	public function override_button_text(): string {
		$text = (string) $this->plugin->get_setting( $this->get_category(), $this->get_slug(), 'button_text' );
		return $text !== '' ? $text : __( 'Order and pay', 'kramar' );
	}

	/**
	 * {@inheritDoc}
	 */
	public function render_extra_settings( string $option_name ): void {
		$slug = $this->get_slug();
		$cat  = $this->get_category();

		$button_text = (string) $this->plugin->get_setting( $cat, $slug, 'button_text' );
		?>
		<div class="kramar-module__extra">
			<label for="kramar-order-button-text"><?php esc_html_e( 'Button text', 'kramar' ); ?></label>
			<input
				type="text"
				id="kramar-order-button-text"
				name="<?php echo esc_attr( $option_name ); ?>[<?php echo esc_attr( $cat ); ?>][<?php echo esc_attr( $slug ); ?>][button_text]"
				value="<?php echo esc_attr( $button_text ); ?>"
				class="regular-text"
			/>
			<span class="description"><?php esc_html_e( 'EU consumer law requires the order button to clearly indicate a payment obligation. Safe options: "Order and pay", "Pay now", "Place order with payment obligation".', 'kramar' ); ?></span>
		</div>
		<?php
	}
}
