<?php

namespace WarrantyCart\Frontend;

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

/**
 * Frontend Assets Class
 */
class Assets {

	/**
	 * Constructor
	 */
	public function __construct() {
		\add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts_styles' ) );
	}

	/**
	 * Enqueue frontend scripts
	 *
	 * Loads JavaScript and CSS assets for the frontend warranty selector.
	 */
	public function enqueue_scripts_styles(): void {
		/**
		 * Filter whether to enqueue frontend assets
		 *
		 * @param bool $enqueue Whether to enqueue assets.
		 */
		$enqueue_assets = \apply_filters( 'warranty_cart_enqueue_frontend_assets', true );

		if ( ! $enqueue_assets || ! $this->should_enqueue_assets() ) {
			return;
		}

		$frontend_asset_file = sprintf( '%s/build/frontend.asset.php', WARRANTY_CART_PATH );
		if ( ! file_exists( $frontend_asset_file ) ) {
			return;
		}

		$frontend_asset = require $frontend_asset_file;

		/**
		 * Filter frontend asset dependencies
		 *
		 * @param array $dependencies Asset dependencies.
		 */
		$frontend_asset_deps = \apply_filters(
			'warranty_cart_frontend_asset_dependencies',
			array_merge( $frontend_asset['dependencies'], array( 'jquery' ) )
		);

		$frontend_asset_version = $frontend_asset['version'] ?? WARRANTY_CART_VERSION;

		/**
		 * Filter frontend script handle
		 *
		 * @param string $handle Script handle.
		 */
		$frontend_script_handle = \apply_filters( 'warranty_cart_frontend_script_handle', 'warranty-frontend' );

		\wp_enqueue_script(
			$frontend_script_handle,
			WARRANTY_CART_URL . 'build/frontend.js',
			$frontend_asset_deps,
			$frontend_asset_version,
			true
		);

		/**
		 * Filter frontend style handle
		 *
		 * @param string $handle Style handle.
		 */
		$frontend_style_handle = \apply_filters( 'warranty_cart_frontend_style_handle', 'warranty-frontend' );

		\wp_enqueue_style(
			$frontend_style_handle,
			WARRANTY_CART_URL . 'build/style-frontend.css',
			array(),
			$frontend_asset_version
		);

		// Enqueue checkout styles
		\wp_enqueue_style(
			'warranty-checkout',
			WARRANTY_CART_URL . 'assets/css/warranty-checkout.css',
			array(),
			WARRANTY_CART_VERSION
		);

		\wp_set_script_translations( 'warranty-frontend', 'warranty-cart' );

		/**
		 * Filter frontend script localization data
		 *
		 * @param array $data Localization data.
		 */
		$localization_data = \apply_filters(
			'warranty_cart_frontend_script_localization',
			array(
				'root'  => \esc_url_raw( \rest_url() ),
				'nonce' => \wp_create_nonce( 'wp_rest' ),
			)
		);

		\wp_localize_script(
			'warranty-frontend',
			'warrantyCartApiSettings',
			$localization_data
		);

		/**
		 * Action after enqueuing frontend assets
		 */
		\do_action( 'warranty_cart_after_enqueue_frontend_assets' );
	}


	/**
	 * Check if assets should be enqueued
	 *
	 * Determines whether to load frontend assets based on current page content.
	 *
	 * @return bool True if assets should be loaded, false otherwise.
	 */
	private function should_enqueue_assets(): bool {
		global $post;

		/**
		 * Filter whether assets should be enqueued on current page
		 *
		 * @param bool         $enqueue Whether to enqueue assets.
		 * @param WP_Post|null $post    Current post object.
		 */
		$should_enqueue = \apply_filters( 'warranty_cart_should_enqueue_frontend_assets', null, $post );

		if ( null !== $should_enqueue ) {
			return (bool) $should_enqueue;
		}

		// Enqueue on pages with the shortcode
		if ( \is_a( $post, 'WP_Post' ) && \has_shortcode( $post->post_content, 'Warranty_Cart' ) ) {
			return true;
		}

		// Enqueue on checkout page
		if ( \is_checkout() ) {
			return true;
		}

		return false;
	}
}
