<?php
/**
 * Centralised asset enqueue handler.
 *
 * Provides two hooks for registering and enqueueing plugin assets:
 * - admin_enqueue_scripts  → enqueue_admin_assets()
 * - wp_enqueue_scripts     → enqueue_frontend_assets()
 *
 * Inline <script> and <style> blocks scattered across templates will be
 * migrated here incrementally.
 */

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

class Elex_MV_Assets_Loader {

	/**
	 * Absolute URL to the plugin root (with trailing slash).
	 *
	 * @var string
	 */
	private $plugin_url;

	/**
	 * Plugin version, used as cache-buster for enqueued assets.
	 *
	 * @var string
	 */
	private $version;

	/**
	 * Constructor.
	 *
	 * @param string $plugin_url  plugin_dir_url() result for the main plugin file.
	 * @param string $version     Plugin version string.
	 */
	public function __construct( string $plugin_url, string $version ) {
		$this->plugin_url = trailingslashit( $plugin_url );
		$this->version    = $version;
	}

	/**
	 * Register WordPress hooks.
	 */
	public function init(): void {
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) );
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_assets' ) );
	}

	/**
	 * Enqueue admin-only scripts and styles.
	 * Hooked to admin_enqueue_scripts.
	 *
	 * @param string $hook_suffix The current admin page hook suffix.
	 */
	public function enqueue_admin_assets( string $hook_suffix ): void {
		$this->enqueue_general_settings_assets( $hook_suffix );
		$this->enqueue_review_and_trobleshoot_notify_assets();
		$this->enqueue_order_controller_assets( $hook_suffix );
		$this->enqueue_dashboard_assets( $hook_suffix );
		$this->enqueue_payment_table_assets( $hook_suffix );
		$this->enqueue_checklist_assets( $hook_suffix );
		$this->enqueue_vendor_setup_assets( $hook_suffix );
		$this->enqueue_admin_products_assets( $hook_suffix );
		$this->enqueue_product_vendor_assign_assets( $hook_suffix );
		$this->enqueue_vendor_name_display_assets( $hook_suffix );
		$this->enqueue_admin_advanced_filter_assets( $hook_suffix );
		$this->enqueue_settlement_assets( $hook_suffix );
		$this->enqueue_setup_tabs_assets( $hook_suffix );
		$this->enqueue_manage_vendors_assets( $hook_suffix );
	}

	private function enqueue_admin_advanced_filter_assets( string $hook_suffix ): void {
		if ( false === strpos( $hook_suffix, 'elex-multi-vendor-for-woocommerce-products' ) ) {
			return;
		}

	// Enqueue select2 (ensure local, not CDN)
	wp_enqueue_script( 'select2' );
	wp_enqueue_style( 'select2' );

	// Enqueue your JS
	wp_enqueue_script(
		'elex-mv-product-filter',
		$this->plugin_url . 'assets/js/admin/admin-product-filter.js',
		array( 'jquery', 'select2' ),
		$this->version,
		array(
			'in_footer' => true,
			'strategy'  => 'defer',
		)
	);

	// Get vendor data
	// phpcs:ignore WordPress.Security.NonceVerification.Recommended --  read-only search parameter used only for UI initialization.
	$vendor_id   = ! empty( $_GET['vendor'] ) ? absint( $_GET['vendor'] ) : '';
	$vendor_post = $vendor_id ? get_post( $vendor_id ) : null;

	$vendor_name = $vendor_post ? $vendor_post->post_title : '';

	// Pass data to JS
	wp_add_inline_script(
		'elex-mv-product-filter',
		'const elexFilterData = ' . wp_json_encode(
			array(
				'vendor_id'   => $vendor_id,
				'vendor_name' => $vendor_name,
				'ajax_url'    => admin_url( 'admin-ajax.php' ),
				'nonce'       => wp_create_nonce( 'elex_mv_prod_page_nonce' ),
			) 
		) . ';',
		'before'
	);
	}

	/**
	 * Assets for the Settlement settings tab.
	 * Page slug: elex-multi-vendor-for-woocommerce-settings
	 */
	private function enqueue_settlement_assets( string $hook_suffix ): void {
		if ( false === strpos( $hook_suffix, 'elex-multi-vendor-for-woocommerce-settings' ) ) {
			return;
		}

		wp_enqueue_script(
			'elex-mv-settlement',
			$this->plugin_url . 'assets/js/admin/settlement.js',
			array( 'jquery', 'select2' ),
			$this->version,
			true
		);
	}

	/**
	 * Assets for the Admin Setup wizard tabs (setup-ui + pay-settle tab).
	 * Page slug: elex-multi-vendor-for-woocommerce-setup
	 */
	private function enqueue_setup_tabs_assets( string $hook_suffix ): void {
		if ( false === strpos( $hook_suffix, 'elex-multi-vendor-for-woocommerce-setup' ) ) {
			return;
		}

		wp_enqueue_script(
			'elex-mv-admin-setup-ui',
			$this->plugin_url . 'assets/js/admin/admin-setup-ui.js',
			array( 'jquery' ),
			$this->version,
			true
		);

		wp_localize_script(
			'elex-mv-admin-setup-ui',
			'elexMvSetupUiParams',
			array(
				'ajax_url' => esc_url( admin_url( 'admin-ajax.php' ) ),
			)
		);

		wp_enqueue_script(
			'elex-mv-admin-pay-settle',
			$this->plugin_url . 'assets/js/admin/admin-pay-settle.js',
			array( 'jquery', 'select2' ),
			$this->version,
			true
		);
	}

	/**
	 * Assets for the Manage Vendors admin page (dashboard + add/edit popups).
	 * Page slug: elex-multi-vendor-for-woocommerce-manage-vendor
	 */
	private function enqueue_manage_vendors_assets( string $hook_suffix ): void {
		if ( false === strpos( $hook_suffix, 'elex-multi-vendor-for-woocommerce-manage-vendor' ) ) {
			return;
		}

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended --  read-only search parameter used only for UI initialization.
		$vendor_search = sanitize_text_field(
			wp_unslash( filter_input( INPUT_GET, 'vendor_search', FILTER_SANITIZE_SPECIAL_CHARS ) ?? '' )
		);

		$site_settings = get_option( 'elex_mv_admin_installation_params', array() );
		$commission    = isset( $site_settings['commission_for_each_sale'] )
			? (float) $site_settings['commission_for_each_sale']
			: 0;

		wp_enqueue_script(
			'elex-mv-manage-vendors-dashboard',
			$this->plugin_url . 'assets/js/admin/manage-vendors-dashboard.js',
			array( 'jquery', 'select2' ),
			$this->version,
			true
		);

		wp_localize_script(
			'elex-mv-manage-vendors-dashboard',
			'elexMvManageVendorsParams',
			array(
				'vendor_search' 		=> esc_js( $vendor_search ),
				'plugin_url'    		=> esc_url( $this->plugin_url ),
				'ajax_url'      		=> esc_url( admin_url( 'admin-ajax.php' ) ),
				'commission'    		=> $commission,
				'nonce'         		=> wp_create_nonce( 'elex_mv_manage_vendors_nonce' ),
				'load_states_nonce' 	=> wp_create_nonce( 'elex_mv_load_states_nonce' ),
			)
		);

		wp_enqueue_script(
			'elex-mv-add-vendors-popup',
			$this->plugin_url . 'assets/js/admin/add-vendors-popup.js',
			array( 'jquery', 'select2', 'elex_mv_intl_tel_input_js' ),
			$this->version,
			true
		);

		wp_enqueue_script(
			'elex-mv-edit-vendors-popup',
			$this->plugin_url . 'assets/js/admin/edit-vendors-popup.js',
			array( 'jquery', 'select2', 'elex_mv_intl_tel_input_js' ),
			$this->version,
			true
		);
	}

	/**
	 * Assets for the Vendor Setup step of the onboarding wizard.
	 * Page slug: elex-multi-vendor-for-woocommerce-setup
	 */
	private function enqueue_vendor_setup_assets( string $hook_suffix ): void {
		if ( false === strpos( $hook_suffix, 'elex-multi-vendor-for-woocommerce-setup' ) ) {
			return;
		}

		wp_enqueue_script(
			'elex-mv-vendor-setup',
			$this->plugin_url . 'assets/js/admin/admin-vendor-setup.js',
			array( 'jquery' ),
			$this->version,
			true
		);

		wp_enqueue_script(
			'elex-mv-store-setup',
			$this->plugin_url . 'assets/js/admin/admin-store-setup.js',
			array( 'jquery', 'select2' ),
			$this->version,
			true
		);

		wp_localize_script(
			'elex-mv-store-setup',
			'elexMvStoreSetupParams',
			array(
				'load_states_nonce' => wp_create_nonce( 'elex_mv_load_states_nonce' ),
			)
		);
	}

	/**
	 * Assets for the vendor address fields on the WP user profile / edit-user pages.
	 * Hooks: show_user_profile (profile.php), edit_user_profile (user-edit.php)
	 */
	private function enqueue_checklist_assets( string $hook_suffix ): void {
		if ( ! in_array( $hook_suffix, array( 'profile.php', 'user-edit.php' ), true ) ) {
			return;
		}

		wp_enqueue_script(
			'elex-mv-checklist',
			$this->plugin_url . 'assets/js/admin/checklist.js',
			array( 'jquery', 'select2' ),
			$this->version,
			true
		);

		// Resolve which user is being viewed/edited.
		if ( 'user-edit.php' === $hook_suffix ) {
			// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only GET used solely to fetch user ID for script localization.
			$user_id = isset( $_GET['user_id'] ) ? absint( $_GET['user_id'] ) : 0;
		} else {
			$user_id = get_current_user_id();
		}

		wp_localize_script(
			'elex-mv-checklist',
			'elexMvChecklistParams',
			array(
				'selected_state' => esc_js( (string) get_user_meta( $user_id, 'elex-multi-vendor-for-woocommerce-state-vendor', true ) ),
			)
		);
	}

	private function enqueue_payment_table_assets( string $hook_suffix ): void {
		if ( false === strpos( $hook_suffix, 'elex-multi-vendor-for-woocommerce-payments' ) ) {
			return;
		}

		wp_enqueue_script(
			'elex-mv-payment-table',
			$this->plugin_url . 'assets/js/admin/admin-payment-table.js',
			array( 'jquery' ),
			$this->version,
			true
		);

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only search parameter used only for UI initialization.
		$search_term = sanitize_text_field( wp_unslash( filter_input( INPUT_GET, 'search_term' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ?? '' ) );

		wp_localize_script(
			'elex-mv-payment-table',
			'elexMvPaymentTableParams',
			array(
				'search_term' => $search_term,
			)
		);
	}

	private function enqueue_dashboard_assets( string $hook_suffix ): void {
		if ( false === strpos( $hook_suffix, 'elex-multi-vendor-for-woocommerce-dashboard' ) ) {
			return;
		}

		wp_enqueue_script(
			'elex-mv-admin-dashboard',
			$this->plugin_url . 'assets/js/admin/admin-dashboard.js',
			array( 'jquery' ),
			$this->version,
			true
		);

		$installation_date = get_option( 'elex_mv_installation_date' );
		$current_minus_29  = wp_date( 'Y-m-d', strtotime( '-29 days' ) );

		if ( $installation_date && strtotime( $installation_date ) >= strtotime( $current_minus_29 ) ) {
			$date_to_display = $installation_date;
		} else {
			$date_to_display = $current_minus_29;
		}

		wp_localize_script(
			'elex-mv-admin-dashboard',
			'elexMvDashboardParams',
			array(
				'ajax_url'        => admin_url( 'admin-ajax.php' ),
				'nonce'           => wp_create_nonce( 'elex_mv_admin_dashboard' ),
				'date_to_display' => esc_attr( $date_to_display ),
			)
		);
	}



	private function enqueue_order_controller_assets( string $hook_suffix ): void {
		if ( false === strpos( $hook_suffix, 'wc-orders' ) ) {
			return;
		}
			wp_register_style( 'elex-mv-order-controller', false, array(), $this->version );
			wp_enqueue_style( 'elex-mv-order-controller' );
	}

	private function enqueue_review_and_trobleshoot_notify_assets(): void {
		wp_enqueue_style(
			'elex-mv-notices',
			$this->plugin_url . 'assets/css/admin/review-and-trobleshoot-notify.css',
			array(),
			$this->version
		);
	}

	/**
	 * Assets for the General Settings admin page.
	 * Page slug: elex-multi-vendor-for-woocommerce-settings
	 *
	 * @param string $hook_suffix
	 */
	private function enqueue_general_settings_assets( string $hook_suffix ): void {
		if ( false === strpos( $hook_suffix, 'elex-multi-vendor-for-woocommerce-settings' ) ) {
			return;
		}

		wp_enqueue_script(
			'elex-mv-general-settings',
			$this->plugin_url . 'assets/js/admin/general-settings.js',
			array( 'jquery' ),
			$this->version,
			true
		);
	}

	/**
	 * Assets for the Vendor Products admin page.
	 * Page slug: elex-multi-vendor-for-woocommerce-products
	 *
	 * @param string $hook_suffix
	 */
	private function enqueue_admin_products_assets( string $hook_suffix ): void {
		if ( false === strpos( $hook_suffix, 'elex-multi-vendor-for-woocommerce-products' ) ) {
			return;
		}

		wp_enqueue_script(
			'elex-mv-admin-products',
			$this->plugin_url . 'assets/js/admin/admin-products.js',
			array( 'jquery', 'select2' ),
			$this->version,
			true
		);

		$search_term = sanitize_text_field( wp_unslash( filter_input( INPUT_GET, 'search_term', FILTER_SANITIZE_SPECIAL_CHARS ) ?? '' ) );

		wp_localize_script(
			'elex-mv-admin-products',
			'elexMvAdminProductsParams',
			array(
				'search_term' => $search_term,
			)
		);
	}

	/**
	 * Assets for the vendor name display / assign widget on product edit pages.
	 * Corresponds to ElexMultiVendor::elex_mv_product_vendor_name_display().
	 * NOTE: that method is not currently hooked anywhere; this enqueue is registered
	 * so the script is available when the hook is re-enabled.
	 * product_id is localized from elex_mv_product_vendor_name_display().
	 *
	 * @param string $hook_suffix
	 */
	private function enqueue_vendor_name_display_assets( string $hook_suffix ): void {
		if ( ! in_array( $hook_suffix, array( 'post.php', 'post-new.php' ), true ) ) {
			return;
		}

		if ( 'post-new.php' === $hook_suffix ) {
			$post_type = sanitize_text_field( wp_unslash( filter_input( INPUT_GET, 'post_type' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ?? 'post' ) );
		} else {
			$post_id   = absint( filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT ) );
			$post_type = $post_id ? (string) get_post_type( $post_id ) : '';
		}

		if ( 'product' !== $post_type ) {
			return;
		}

		wp_enqueue_script(
			'elex-mv-vendor-name-display',
			$this->plugin_url . 'assets/js/admin/vendor-name-display.js',
			array( 'jquery' ),
			$this->version,
			true
		);
	}

	/**
	 * Assets for the vendor-assign meta box on the WooCommerce product edit page.
	 * Loads on post.php and post-new.php for products only.
	 * store_id / store_name are passed via wp_localize_script from
	 * ElexMultiVendor::add_custom_product_general_field().
	 *
	 * @param string $hook_suffix
	 */
	private function enqueue_product_vendor_assign_assets( string $hook_suffix ): void {
		if ( ! in_array( $hook_suffix, array( 'post.php', 'post-new.php' ), true ) ) {
			return;
		}

		if ( 'post-new.php' === $hook_suffix ) {
			$post_type = sanitize_text_field( wp_unslash( filter_input( INPUT_GET, 'post_type' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ?? 'post' ) );
		} else {
			$post_id   = absint( filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT ) );
			$post_type = $post_id ? (string) get_post_type( $post_id ) : '';
		}

		if ( 'product' !== $post_type ) {
			return;
		}

		wp_enqueue_script(
			'elex-mv-product-vendor-assign',
			$this->plugin_url . 'assets/js/admin/product-vendor-assign.js',
			array( 'jquery', 'select2' ),
			$this->version,
			true
		);
	}

	/**
	 * Enqueue frontend (public) scripts and styles.
	 * Hooked to wp_enqueue_scripts.
	 */
	public function enqueue_frontend_assets(): void {
		$this->enqueue_frontend_products_assets();
		$this->enqueue_frontend_categories_assets();
		$this->enqueue_store_profile_assets();
		$this->enqueue_store_page_setting_assets();
		$this->enqueue_vendor_general_settings_assets();
		$this->enqueue_vendor_dashboard_assets();
		$this->enqueue_vendor_dashboard_outer_assets();
		$this->enqueue_vendor_shipping_assets();
		$this->enqueue_vendor_review_assets();
		$this->enqueue_vendor_store_page_assets();
		$this->enqueue_vendor_account_data_assets();
		$this->register_review_hide_form_style();
		$this->enqueue_product_tab_assets();
		$this->enqueue_vendor_onboarding_assets();
	}

	/**
	 * Assets for the vendor Products tab on the frontend dashboard.
	 * Loads only when menu_tab=product (the URL param used by the vendor dashboard).
	 */
	private function enqueue_frontend_products_assets(): void {
		$menu_tab = sanitize_text_field( wp_unslash( filter_input( INPUT_GET, 'menu_tab' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ?? '' ) );

		if ( 'product' !== $menu_tab ) {
			return;
		}

		wp_enqueue_script(
			'elex-mv-frontend-products',
			$this->plugin_url . 'assets/js/frontend/products.js',
			array( 'jquery', 'elex_mv_bootstrap_js' ),
			$this->version,
			true
		);
	}

	/**
	 * Assets for the Store Settings tab on the vendor frontend dashboard.
	 * Loads only when menu_tab=store_page_settings.
	 */
	private function enqueue_store_page_setting_assets(): void {
		$menu_tab = sanitize_text_field( wp_unslash( filter_input( INPUT_GET, 'menu_tab' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ?? '' ) );

		if ( 'store_page_settings' !== $menu_tab ) {
			return;
		}

		wp_enqueue_script(
			'elex-mv-store-page-setting',
			$this->plugin_url . 'assets/js/frontend/store-page-setting.js',
			array( 'jquery', 'elex-mv-vendor-dashboard-outer' ),
			$this->version,
			true
		);
	}

	/**
	 * Assets for the Store Profile tab on the vendor frontend dashboard.
	 * Loads only when menu_tab=store_profile.
	 */
	private function enqueue_store_profile_assets(): void {
		$menu_tab = sanitize_text_field( wp_unslash( filter_input( INPUT_GET, 'menu_tab' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ?? '' ) );

		if ( 'store_profile' !== $menu_tab ) {
			return;
		}

		wp_enqueue_script(
			'elex-mv-store-profile',
			$this->plugin_url . 'assets/js/frontend/store-profile.js',
			array( 'jquery', 'select2', 'elex_mv_intl_tel_input_js', 'elex-mv-vendor-dashboard-outer' ),
			$this->version,
			true
		);

		$store_posts = get_posts(
			array(
				'post_type'   => 'elex_vendor',
				'author'      => get_current_user_id(),
				'post_status' => array( 'pending', 'private', 'publish', 'draft', 'reject' ),
				'numberposts' => 1,
			)
		);
		$store_id    = ! empty( $store_posts ) ? $store_posts[0]->ID : 0;

		wp_localize_script(
			'elex-mv-store-profile',
			'elexMvStoreProfileParams',
			array(
				'ajax_url'          => esc_url( admin_url( 'admin-ajax.php' ) ),
				'selected_state'    => esc_js( (string) get_post_meta( $store_id, 'state', true ) ),
				'country_iso'       => esc_js( (string) get_post_meta( $store_id, 'country_iso', true ) ),
				'load_states_nonce' => wp_create_nonce( 'elex_mv_load_states_nonce' ),
			)
		);
	}

	/**
	 * Assets for the vendor Categories sub-tab on the frontend dashboard.
	 * Loads only when menu_tab=product and tab=categories.
	 * tab defaults to 'product' when absent (see product-topbar.php), so this
	 * only fires on an explicit ?tab=categories request.
	 *
	 * The handle is always registered (not just enqueued) so that
	 * wp_add_inline_script() called from inside the template can always attach
	 * to it — even before we know whether the script will be printed.
	 */
	private function enqueue_frontend_categories_assets(): void {
		// Always register — gives wp_add_inline_script() a valid target.
		// Bootstrap is already enqueued globally by the main plugin, so we only
		// need jquery here; declaring elex_mv_bootstrap_js could silently block
		// enqueue if that handle isn't resolved at wp_enqueue_scripts time.
		wp_register_script(
			'elex-mv-frontend-categories',
			$this->plugin_url . 'assets/js/frontend/categories.js',
			array( 'jquery' ),
			$this->version,
			true
		);

		$menu_tab = sanitize_text_field( wp_unslash( filter_input( INPUT_GET, 'menu_tab' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ?? '' ) );
		$tab      = sanitize_text_field( wp_unslash( filter_input( INPUT_GET, 'tab' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ?? '' ) );

		if ( 'product' !== $menu_tab || 'categories' !== $tab ) {
			return;
		}

		wp_enqueue_script( 'elex-mv-frontend-categories' );
	}

	/**
	 * Assets for the General Settings (vendor) tab.
	 * Loads only when menu_tab=general_settings.
	 */
	private function enqueue_vendor_general_settings_assets(): void {
		$menu_tab = sanitize_text_field( wp_unslash( filter_input( INPUT_GET, 'menu_tab' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ?? '' ) );

		if ( 'general_settings' !== $menu_tab ) {
			return;
		}

		wp_enqueue_script(
			'elex-mv-vendor-general-settings',
			$this->plugin_url . 'assets/js/frontend/vendor-general-settings.js',
			array( 'jquery' ),
			$this->version,
			true
		);
	}

	/**
	 * Assets for the vendor Dashboard tab.
	 * Loads only when menu_tab=dashboard.
	 */
	private function enqueue_vendor_dashboard_assets(): void {
		$menu_tab = sanitize_text_field( wp_unslash( filter_input( INPUT_GET, 'menu_tab' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ?? '' ) );

		// The dashboard is the default view when no menu_tab is provided.
		// Load assets when explicitly on the dashboard tab, or when on the vendor-dashboard
		// page with no tab specified (template fires elex_mv_dashboard_page in that case).
		if ( 'dashboard' !== $menu_tab ) {
			if ( '' !== $menu_tab || ! is_page( 'vendor-dashboard' ) ) {
				return;
			}
		}

		wp_enqueue_style(
			'elex-mv-vendor-dashboard',
			$this->plugin_url . 'assets/css/frontend/vendor-dashboard.css',
			array(),
			$this->version
		);

		// Chart.js — renders the "Sales Last 7 days" bar chart.
		// Handle matches ElexMultiVendor.php so WordPress deduplicates automatically.
		wp_enqueue_script(
			'chart-js',
			$this->plugin_url . 'assets/lib/chartjs/chart.min.js',
			array( 'jquery' ),
			$this->version,
			true
		);

		// Leaflet.js — renders the "Top Countries" map.
		wp_enqueue_script(
			'leaflet-js',
			$this->plugin_url . 'assets/lib/leaflet/leaflet.min.js',
			array( 'jquery' ),
			$this->version,
			true
		);

		wp_enqueue_style(
			'elex-mv-leaflet-css',
			$this->plugin_url . 'assets/lib/leaflet/leaflet.min.css',
			array(),
			$this->version
		);

		// Country centroid lookup table required by the Leaflet map.
		wp_enqueue_script(
			'elex-mv-country-centroids',
			$this->plugin_url . 'assets/js/elex-mv-country-centroids.js',
			array(),
			$this->version,
			true
		);

		// Main vendor dashboard: data loading, Chart.js rendering, Leaflet map.
		wp_enqueue_script(
			'elex-mv-vendor-dashboard-main',
			$this->plugin_url . 'assets/js/vendor-dashboard.js',
			array( 'jquery', 'chart-js', 'leaflet-js', 'elex-mv-country-centroids', 'elex_mv_select_2_js' ),
			$this->version,
			true
		);

		wp_localize_script(
			'elex-mv-vendor-dashboard-main',
			'elex_mv_vendor_dashboard_params',
			array(
				'ajax_url' => admin_url( 'admin-ajax.php' ),
				'nonce'    => wp_create_nonce( 'elex_mv_vendor_dashboard' ),
			)
		);

		// Small script: sets initial "from" date input and handles the Apply button.
		wp_enqueue_script(
			'elex-mv-vendor-dashboard',
			$this->plugin_url . 'assets/js/frontend/vendor-dashboard.js',
			array( 'jquery', 'elex-mv-vendor-dashboard-main' ),
			$this->version,
			true
		);

		wp_localize_script(
			'elex-mv-vendor-dashboard',
			'elexMvVendorDashboardParams',
			array(
				'ajax_url'  => admin_url( 'admin-ajax.php' ),
				'nonce'     => wp_create_nonce( 'elex_mv_vendor_dashboard' ),
				'from_date' => esc_attr( wp_date( 'Y-m-d', strtotime( '-29 days' ) ) ),
			)
		);
	}

	/**
	 * Assets for the vendor dashboard outer wrapper (elex-vendor-dashboard-html.php).
	 * Loads on any vendor dashboard page (any menu_tab is set for logged-in vendors).
	 */
	private function enqueue_vendor_dashboard_outer_assets(): void {
		$menu_tab = sanitize_text_field( wp_unslash( filter_input( INPUT_GET, 'menu_tab' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ?? '' ) );

		// When no tab is specified, the dashboard view is the default. Load outer assets
		// only when on the actual vendor-dashboard page to avoid loading on unrelated pages.
		if ( empty( $menu_tab ) && ! is_page( 'vendor-dashboard' ) ) {
			return;
		}

		wp_enqueue_script(
			'elex-mv-vendor-dashboard-outer',
			$this->plugin_url . 'assets/js/frontend/vendor-dashboard-outer.js',
			array( 'jquery' ),
			$this->version,
			true
		);
	}

	/**
	 * Assets for the vendor Shipping tab.
	 * Loads only when menu_tab=shipping.
	 * Includes both shipping.php and carrier-list.php scripts (carrier-list is
	 * included once inside the shipping template).
	 */
	private function enqueue_vendor_shipping_assets(): void {
		$menu_tab = sanitize_text_field( wp_unslash( filter_input( INPUT_GET, 'menu_tab' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ?? '' ) );

		if ( 'shipping' !== $menu_tab ) {
			return;
		}

		wp_enqueue_script(
			'elex-mv-vendor-shipping',
			$this->plugin_url . 'assets/js/frontend/vendor-shipping.js',
			array( 'jquery' ),
			$this->version,
			true
		);

		wp_enqueue_script(
			'elex-mv-vendor-carrier-list',
			$this->plugin_url . 'assets/js/frontend/vendor-carrier-list.js',
			array( 'jquery' ),
			$this->version,
			true
		);
	}

	/**
	 * Assets for the vendor Reviews tab.
	 * Loads only when menu_tab=reviews.
	 */
	private function enqueue_vendor_review_assets(): void {
		$menu_tab = sanitize_text_field( wp_unslash( filter_input( INPUT_GET, 'menu_tab' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ?? '' ) );

		if ( 'reviews' !== $menu_tab ) {
			return;
		}

		wp_enqueue_script(
			'elex-mv-vendor-review',
			$this->plugin_url . 'assets/js/frontend/vendor-review.js',
			array( 'jquery' ),
			$this->version,
			true
		);
	}

	/**
	 * Assets for the public vendor store page ([elex-vendor-store-page] shortcode).
	 * Loads only when the store_title query var is set.
	 */
	private function enqueue_vendor_store_page_assets(): void {
		$store_title = sanitize_text_field( get_query_var( 'store_title', '' ) );

		if ( empty( $store_title ) ) {
			return;
		}

		wp_enqueue_script(
			'elex-mv-vendor-store-page',
			$this->plugin_url . 'assets/js/frontend/vendor-store-page.js',
			array( 'jquery' ),
			$this->version,
			true
		);
	}

	/**
	 * Registers the style handle used by hide_review_form_if_not_purchased().
	 * The handle has no file (false URL) — it only carries inline CSS added at
	 * runtime via wp_add_inline_style() when the purchase-restriction condition
	 * is met. wp_print_styles() is called immediately after wp_add_inline_style()
	 * inside the filter callback to flush the style inline at that point in the
	 * page output.
	 */
	private function register_review_hide_form_style(): void {
		if ( ! is_product() ) {
			return;
		}
		wp_register_style( 'elex-mv-review-hide-form', false, array(), $this->version );
	}

	/**
	 * Assets for the vendor Account Data (payments) tab.
	 * Loads only when menu_tab=account_data.
	 */
	private function enqueue_vendor_account_data_assets(): void {
		$menu_tab = sanitize_text_field( wp_unslash( filter_input( INPUT_GET, 'menu_tab' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ?? '' ) );

		if ( 'account_data' !== $menu_tab ) {
			return;
		}

		wp_enqueue_script(
			'elex-mv-vendor-account-data',
			$this->plugin_url . 'assets/js/frontend/vendor-account-data.js',
			array( 'jquery' ),
			$this->version,
			true
		);
	}

	/**
	 * Assets for the vendor product management templates (add/edit product tabs).
	 * All product template scripts/styles load when menu_tab=product.
	 */
	private function enqueue_product_tab_assets(): void {
		$menu_tab = sanitize_text_field( wp_unslash( filter_input( INPUT_GET, 'menu_tab' , FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ?? '' ) );

		if ( 'product' !== $menu_tab ) {
			return;
		}

		wp_enqueue_style(
			'elex-mv-product-details-form',
			$this->plugin_url . 'assets/css/frontend/product-details-form.css',
			array(),
			$this->version
		);

		wp_enqueue_style(
			'elex-mv-product-attributes',
			$this->plugin_url . 'assets/css/frontend/product-attributes.css',
			array(),
			$this->version
		);

		wp_enqueue_style(
			'elex-mv-edit-product-main',
			$this->plugin_url . 'assets/css/frontend/edit-product-main.css',
			array(),
			$this->version
		);

		wp_enqueue_style(
			'elex-mv-edit-product-attributes',
			$this->plugin_url . 'assets/css/frontend/edit-product-attributes.css',
			array(),
			$this->version
		);

		wp_enqueue_script(
			'elex-mv-product-details-form',
			$this->plugin_url . 'assets/js/frontend/product-details-form.js',
			array( 'jquery' ),
			$this->version,
			true
		);

		wp_enqueue_script(
			'elex-mv-product-inventory',
			$this->plugin_url . 'assets/js/frontend/product-inventory.js',
			array( 'jquery' ),
			$this->version,
			true
		);

		wp_enqueue_script(
			'elex-mv-product-attributes',
			$this->plugin_url . 'assets/js/frontend/product-attributes.js',
			array( 'jquery', 'select2' ),
			$this->version,
			true
		);

		wp_enqueue_script(
			'elex-mv-edit-product-main',
			$this->plugin_url . 'assets/js/frontend/edit-product-main.js',
			array( 'jquery' ),
			$this->version,
			true
		);

		wp_enqueue_script(
			'elex-mv-edit-product-attributes',
			$this->plugin_url . 'assets/js/frontend/edit-product-attributes.js',
			array( 'jquery', 'select2' ),
			$this->version,
			true
		);

		wp_enqueue_script(
			'elex-mv-edit-product-variations',
			$this->plugin_url . 'assets/js/frontend/edit-product-variations.js',
			array( 'jquery' ),
			$this->version,
			true
		);
	}

	/**
	 * Assets for the vendor onboarding wizard.
	 * Loads when menu_tab is absent and the current user is a vendor.
	 * The onboarding templates are included directly (no menu_tab param)
	 * when vendor_onboarding_wizard is enabled.
	 */
	private function enqueue_vendor_onboarding_assets(): void {
		// Onboarding scripts are enqueued directly in ElexMultiVendor::Elex_Mv_Dashboard_page()
		// at the point where the onboarding template is actually rendered.
	}
}
