<?php
/**
 * Checkout Scripts Handler
 *
 * Centralizes script loading for all Square payment gateways.
 * Loads unminified JS in development, minified in production.
 *
 * @package FunnelKit_Square
 */

namespace FKWCSQ\Includes;

use FKWCSQ\Square;
use FKWCSQ\Includes\Traits\Instance;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Checkout_Scripts Class
 */
class Checkout_Scripts {

	use Instance;

	/**
	 * Whether scripts have been registered.
	 *
	 * @var bool
	 */
	private $scripts_registered = false;

	/**
	 * Whether development mode is active.
	 *
	 * @var bool|null
	 */
	private $is_dev_mode = null;

	/**
	 * Constructor.
	 */
	public function __construct() {
		add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ), 5 );
	}

	/**
	 * Check if development mode is enabled.
	 *
	 * Uses FKWCSQ_IS_DEV constant if defined, otherwise defaults to false (production).
	 *
	 * @return bool
	 */
	public function is_dev_mode() {
		if ( null === $this->is_dev_mode ) {
			$this->is_dev_mode = defined( 'FKWCSQ_IS_DEV' ) && \FKWCSQ_IS_DEV;
		}
		return $this->is_dev_mode;
	}

	/**
	 * Get the script suffix based on mode.
	 *
	 * @return string '.min' for production, '' for development.
	 */
	public function get_script_suffix() {
		return $this->is_dev_mode() ? '' : '.min';
	}

	/**
	 * Register scripts.
	 *
	 * @return void
	 */
	public function register_scripts() {
		if ( $this->scripts_registered ) {
			return;
		}

		if ( ! is_checkout() && ! is_add_payment_method_page() ) {
			return;
		}

		// Register Square Web SDK.
		wp_register_script(
			'square-web-sdk',
			Square::get_web_sdk_url(),
			array(),
			null, // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- External SDK
			true
		);

		// Register bundled checkout script.
		$suffix = $this->get_script_suffix();
		wp_register_script(
			'fkwcsq-checkout',
			\FKWCSQ_PLUGIN_URL . 'assets/js/fkwcsq-checkout' . $suffix . '.js',
			array( 'jquery', 'square-web-sdk' ),
			\FKWCSQ_VERSION,
			true
		);

		$this->scripts_registered = true;
	}

	/**
	 * Enqueue the bundled checkout script.
	 *
	 * @return void
	 */
	public function enqueue_checkout_script() {
		$this->register_scripts();

		wp_enqueue_script( 'square-web-sdk' );
		wp_enqueue_script( 'fkwcsq-checkout' );
	}

	/**
	 * Get base localize data common to all gateways.
	 *
	 * @return array
	 */
	public function get_base_localize_data() {
		$api = API::get_instance();

		return array(
			'application_id' => Square::get_application_id(),
			'location_id'    => $api->get_location_id(),
			'is_sandbox'     => Square::is_sandbox_mode(),
			'currency'       => get_woocommerce_currency(),
			'country_code'   => WC()->countries->get_base_country(),
			'ajax_url'       => admin_url( 'admin-ajax.php' ),
			'cart_total'     => WC()->cart ? WC()->cart->get_total( 'edit' ) : 0,
			'i18n'           => array(
				'error_generic' => __( 'An error occurred. Please try again.', 'funnelkit-payment-gateway-square-for-woocommerce' ),
			),
		);
	}

	/**
	 * Get gateway-specific localize data.
	 *
	 * Merges base data with gateway-specific data and applies single filter.
	 *
	 * @param string $gateway_id   The gateway ID (e.g., 'fkwcsq_ach', 'fkwcsq_cash_app').
	 * @param array  $gateway_data Gateway-specific data to merge with base data.
	 * @return array
	 */
	public function get_gateway_localize_data( $gateway_id, $gateway_data = array() ) {
		$base_data = $this->get_base_localize_data();
		$data      = array_merge( $base_data, $gateway_data );

		/**
		 * Filter the gateway localize data.
		 *
		 * @param array  $data       Merged localize data array.
		 * @param string $gateway_id The gateway ID.
		 * @param array  $base_data  Base data before merge.
		 */
		return apply_filters( 'fkwcsq_gateway_localize_data', $data, $gateway_id, $base_data );
	}

	/**
	 * Localize script data for a gateway.
	 *
	 * @param string $var_name     The JavaScript variable name.
	 * @param string $gateway_id   The gateway ID.
	 * @param array  $gateway_data Gateway-specific data.
	 * @return void
	 */
	public function localize_gateway_data( $var_name, $gateway_id, $gateway_data = array() ) {
		$data = $this->get_gateway_localize_data( $gateway_id, $gateway_data );
		wp_localize_script( 'fkwcsq-checkout', $var_name, $data );
	}

	/**
	 * Get the script handle.
	 *
	 * @return string Script handle.
	 */
	public function get_script_handle() {
		return 'fkwcsq-checkout';
	}
}
