<?php
/**
 * WCS_ATT_Display class
 *
 * @package  WooCommerce All Products for Subscriptions
 * @since    APFS 1.0.0
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Front-end support and single-product template modifications.
 *
 * @class    WCS_ATT_Display
 * @version  6.0.0
 */
class WCS_ATT_Display {

	/**
	 * Initialization.
	 */
	public static function init() {

		// Cart display hooks.
		WCS_ATT_Display_Cart::init();
		// Single-product display hooks.
		WCS_ATT_Display_Product::init();

		self::add_hooks();
	}

	/**
	 * Hook-in.
	 */
	private static function add_hooks() {

		// Enqueue scripts and styles.
		add_action( 'wp_enqueue_scripts', array( __CLASS__, 'frontend_scripts' ) );
	}

	/*
	|--------------------------------------------------------------------------
	| Filters
	|--------------------------------------------------------------------------
	*/

	/**
	 * Front end styles and scripts.
	 *
	 * Loads webpack-generated APFS frontend styles and consolidated JavaScript bundle.
	 * RTL styles are automatically generated by webpack.
	 * JavaScript bundle includes both single-add-to-cart.js and cart.js functionality.
	 *
	 * @return void
	 */
	public static function frontend_scripts() {

		// Load webpack-generated asset file with fallback.
		$asset_file_path = WCS_ATT()->plugin_path() . '/build/apfs-frontend.asset.php';
		$asset_file      = file_exists( $asset_file_path ) ? include $asset_file_path : array(
			'dependencies' => array(),
			'version'      => WC_Subscriptions::$version,
		);

		// Register and enqueue consolidated frontend CSS.
		wp_register_style( 'wcsatt-css', WCS_ATT()->plugin_url() . '/build/apfs-frontend.css', false, $asset_file['version'], 'all' );
		wp_style_add_data( 'wcsatt-css', 'rtl', 'replace' );
		wp_enqueue_style( 'wcsatt-css' );

		// Merge webpack dependencies with WooCommerce-specific dependencies.
		// Webpack automatically detects jQuery and Backbone as externals, but we need to ensure
		// WooCommerce scripts are included as dependencies.
		$script_dependencies = array_merge(
			$asset_file['dependencies'],
			array( 'jquery', 'jquery-blockui', 'backbone', 'wc-country-select', 'wc-address-i18n' )
		);

		// Remove duplicates if webpack also added these.
		$script_dependencies = array_unique( $script_dependencies );

		// Register and enqueue consolidated frontend JavaScript bundle.
		wp_register_script( 'wcsatt-frontend', WCS_ATT()->plugin_url() . '/build/apfs-frontend.js', $script_dependencies, $asset_file['version'], true );
		wp_enqueue_script( 'wcsatt-frontend' );

		// Localize script with single product parameters (always available for product pages).
		$single_product_params = array(
			'i18n_subs_load_error'  => __( 'Failed to load matching subscriptions. If this issue persists, please re-load the page and try again.', 'woocommerce-subscriptions' ),
			/* translators: %s: trial period string, e.g. "7 days" or "1 month" */
			'i18n_free_trial_label' => __( 'Free trial: %s', 'woocommerce-subscriptions' ),
			/* translators: %s: formatted signup fee amount */
			'i18n_signup_fee_label' => __( 'Sign-up fee: %s', 'woocommerce-subscriptions' ),
			'wc_ajax_url'           => WC_AJAX::get_endpoint( '%%endpoint%%' ),
		);
		wp_localize_script( 'wcsatt-frontend', 'wcsatt_single_product_params', $single_product_params );

		// Localize script with cart parameters when on cart or checkout pages.
		// phpcs:ignore WooCommerce.Commenting.CommentHooks.HookCommentWrongStyle
		if ( apply_filters( 'wcsatt_enqueue_cart_script', is_cart() || ( is_checkout() && 'off' !== get_option( 'wcsatt_add_cart_to_subscription', 'off' ) ) ) ) {
			$cart_params = array(
				'i18n_update_cart_sub_error' => __( 'Failed to update your cart. If this issue persists, please re-load the page and try again.', 'woocommerce-subscriptions' ),
				'i18n_subs_load_error'       => __( 'Failed to load matching subscriptions. If this issue persists, please re-load the page and try again.', 'woocommerce-subscriptions' ),
				'update_cart_option_nonce'   => wp_create_nonce( 'wcsatt_update_cart_option' ),
				'wc_ajax_url'                => WC_AJAX::get_endpoint( '%%endpoint%%' ),
			);
			wp_localize_script( 'wcsatt-frontend', 'wcsatt_cart_params', $cart_params );
		}
	}
}
