<?php
/**
 * KooKoa Folders — Admin / builder asset enqueue.
 *
 * Owns every wp_enqueue_style/script path: the standard folder admin screens
 * (Media Library, edit/post lists, settings), the shared builder+modal bundle
 * (Divi / WPBakery / Beaver front-end builders), and the Elementor editor.
 * Pulls the localized JS payload + builder modal JSON from KookoaWPF_Localize.
 *
 * Instantiated lazily and dispatched from KookoaWPF, which keeps the hook
 * registrations (so external references and registered callbacks never change).
 *
 * @package KookoaWPF
 * @license GPL-2.0-or-later
 */

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

/**
 * Enqueues CSS/JS for folder admin screens, page builders and Elementor.
 */
class KookoaWPF_Assets {

	/**
	 * Singleton instance.
	 *
	 * @var self|null
	 */
	private static $instance = null;

	/**
	 * Return the singleton instance.
	 *
	 * @return self
	 */
	public static function instance(): self {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Constructor. Hook registration lives on KookoaWPF (init_hooks), so this
	 * collaborator registers nothing of its own.
	 */
	private function __construct() {
		// Registration is owned by KookoaWPF (the hook surface).
	}

	/**
	 * Enqueue all builder/modal assets shared between builder, Elementor, and admin builder paths.
	 *
	 * @param string $suffix '.min' or ''.
	 */
	private function enqueue_builder_modal_assets( $suffix ): void {
		$css_ver = kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/css/admin' . $suffix . '.css' );
		$js_ver  = kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/admin' . $suffix . '.js' );

		wp_enqueue_style( 'kookoawpf-font', KOOKOAWPF_PLUGIN_URL . 'assets/css/font' . $suffix . '.css', array(), KOOKOAWPF_VERSION );
		wp_enqueue_style( 'kookoawpf-jstree', KOOKOAWPF_PLUGIN_URL . 'assets/css/jstree.min.css', array(), KOOKOAWPF_VERSION );
		wp_enqueue_script( 'kookoawpf-jstree', KOOKOAWPF_PLUGIN_URL . 'assets/js/jstree.min.js', array( 'jquery' ), KOOKOAWPF_VERSION, true );
		wp_enqueue_script( 'jquery-ui-draggable' );
		wp_enqueue_script( 'jquery-ui-droppable' );
		wp_enqueue_style( 'kookoawpf-admin', KOOKOAWPF_PLUGIN_URL . 'assets/css/admin' . $suffix . '.css', array( 'kookoawpf-font' ), $css_ver );
		wp_enqueue_style( 'kookoawpf-tree', KOOKOAWPF_PLUGIN_URL . 'assets/css/tree' . $suffix . '.css', array( 'kookoawpf-jstree' ), $css_ver );
		wp_enqueue_script( 'kookoawpf-utils', KOOKOAWPF_PLUGIN_URL . 'assets/js/kookoawpf-utils' . $suffix . '.js', array(), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/kookoawpf-utils' . $suffix . '.js' ), true );
		wp_enqueue_script( 'kookoawpf-admin', KOOKOAWPF_PLUGIN_URL . 'assets/js/admin' . $suffix . '.js', array( 'jquery', 'kookoawpf-jstree', 'jquery-ui-draggable', 'jquery-ui-droppable', 'kookoawpf-utils' ), $js_ver, true );
		wp_enqueue_script( 'kookoawpf-custom-select', KOOKOAWPF_PLUGIN_URL . 'assets/js/custom-select' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/custom-select' . $suffix . '.js' ), true );
		wp_enqueue_style( 'kookoawpf-context-menu', KOOKOAWPF_PLUGIN_URL . 'assets/css/context-menu' . $suffix . '.css', array( 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/css/context-menu' . $suffix . '.css' ) );
		wp_enqueue_style( 'kookoawpf-settings', KOOKOAWPF_PLUGIN_URL . 'assets/css/settings' . $suffix . '.css', array( 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/css/settings' . $suffix . '.css' ) );
		wp_enqueue_style( 'kookoawpf-modals', KOOKOAWPF_PLUGIN_URL . 'assets/css/modals' . $suffix . '.css', array( 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/css/modals' . $suffix . '.css' ) );
		wp_enqueue_style( 'kookoawpf-bulk', KOOKOAWPF_PLUGIN_URL . 'assets/css/bulk' . $suffix . '.css', array( 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/css/bulk' . $suffix . '.css' ) );
		wp_enqueue_style( 'kookoawpf-breadcrumb', KOOKOAWPF_PLUGIN_URL . 'assets/css/breadcrumb' . $suffix . '.css', array( 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/css/breadcrumb' . $suffix . '.css' ) );
		wp_enqueue_style( 'kookoawpf-media-modal', KOOKOAWPF_PLUGIN_URL . 'assets/css/media-modal' . $suffix . '.css', array( 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/css/media-modal' . $suffix . '.css' ) );
		wp_enqueue_style( 'kookoawpf-smart-folder', KOOKOAWPF_PLUGIN_URL . 'assets/css/smart-folder' . $suffix . '.css', array( 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/css/smart-folder' . $suffix . '.css' ) );
		wp_enqueue_script( 'kookoawpf-context-menu', KOOKOAWPF_PLUGIN_URL . 'assets/js/context-menu' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/context-menu' . $suffix . '.js' ), true );
		wp_enqueue_script( 'kookoawpf-export-import', KOOKOAWPF_PLUGIN_URL . 'assets/js/export-import' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/export-import' . $suffix . '.js' ), true );
		wp_enqueue_script( 'kookoawpf-sidebar-ui', KOOKOAWPF_PLUGIN_URL . 'assets/js/sidebar-ui' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/sidebar-ui' . $suffix . '.js' ), true );
		wp_enqueue_script( 'kookoawpf-breadcrumb', KOOKOAWPF_PLUGIN_URL . 'assets/js/breadcrumb' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/breadcrumb' . $suffix . '.js' ), true );
		wp_enqueue_script( 'kookoawpf-folder-bulk', KOOKOAWPF_PLUGIN_URL . 'assets/js/folder-bulk' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/folder-bulk' . $suffix . '.js' ), true );
		wp_enqueue_script( 'kookoawpf-bulk-organize', KOOKOAWPF_PLUGIN_URL . 'assets/js/bulk-organize' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/bulk-organize' . $suffix . '.js' ), true );
		wp_enqueue_script( 'kookoawpf-dragdrop', KOOKOAWPF_PLUGIN_URL . 'assets/js/dragdrop' . $suffix . '.js', array( 'jquery', 'jquery-ui-draggable', 'jquery-ui-droppable', 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/dragdrop' . $suffix . '.js' ), true );
		wp_enqueue_script( 'kookoawpf-modals', KOOKOAWPF_PLUGIN_URL . 'assets/js/modals' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin', 'kookoawpf-context-menu' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/modals' . $suffix . '.js' ), true );
		wp_enqueue_script( 'kookoawpf-download', KOOKOAWPF_PLUGIN_URL . 'assets/js/download' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin', 'kookoawpf-context-menu' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/download' . $suffix . '.js' ), true );
		wp_enqueue_script( 'kookoawpf-dynamic', KOOKOAWPF_PLUGIN_URL . 'assets/js/dynamic' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin', 'kookoawpf-custom-select', 'kookoawpf-context-menu' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/dynamic' . $suffix . '.js' ), true );
		wp_enqueue_script( 'kookoawpf-media-modal', KOOKOAWPF_PLUGIN_URL . 'assets/js/media-modal' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin', 'kookoawpf-custom-select', 'kookoawpf-context-menu' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/media-modal' . $suffix . '.js' ), true );
		wp_enqueue_script( 'kookoawpf-media-modal-dialogs', KOOKOAWPF_PLUGIN_URL . 'assets/js/media-modal-dialogs' . $suffix . '.js', array( 'jquery', 'kookoawpf-utils', 'kookoawpf-custom-select', 'kookoawpf-media-modal' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/media-modal-dialogs' . $suffix . '.js' ), true );
		wp_enqueue_script( 'kookoawpf-media-modal-interactions', KOOKOAWPF_PLUGIN_URL . 'assets/js/media-modal-interactions' . $suffix . '.js', array( 'jquery', 'kookoawpf-media-modal' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/media-modal-interactions' . $suffix . '.js' ), true );
	}

	/**
	 * Enqueue assets on frontend when a page builder is active (Divi Visual Builder, etc.).
	 */
	public function enqueue_builder_assets(): void {
		if ( ! kookoawpf()->is_builder_editor() ) {
			return; }

		// Access Control gate.
		if ( ! kookoawpf_user_has_access() ) {
			return; }

		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
		$this->enqueue_builder_modal_assets( $suffix );
		wp_localize_script( 'kookoawpf-admin', 'kookoawpfData', KookoaWPF_Localize::instance()->get_localize_data( 'attachment', true ) );
	}

	/**
	 * Enqueue assets specifically for Elementor editor (called from Elementor hook).
	 */
	public function enqueue_elementor_assets(): void {
		if ( ! kookoawpf_user_has_access() ) {
			return; }

		// If already enqueued, skip.
		if ( wp_script_is( 'kookoawpf-admin', 'enqueued' ) ) {
			return; }

		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
		$this->enqueue_builder_modal_assets( $suffix );
		wp_localize_script( 'kookoawpf-admin', 'kookoawpfData', KookoaWPF_Localize::instance()->get_localize_data( 'attachment', true ) );
	}

	/**
	 * Enqueue CSS and JS for admin screens that display folders.
	 *
	 * @param string $hook The current admin page hook suffix.
	 */
	public function enqueue_admin_assets( $hook ): void {
		// Upgrade page — load only its own assets and skip everything else.
		if ( false !== strpos( $hook, 'kookoa-folders-upgrade' ) ) {
			$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
			wp_enqueue_style( 'kookoawpf-font', KOOKOAWPF_PLUGIN_URL . 'assets/css/font' . $suffix . '.css', array(), KOOKOAWPF_VERSION );
			wp_enqueue_style(
				'kookoawpf-upgrade',
				KOOKOAWPF_PLUGIN_URL . 'assets/css/upgrade' . $suffix . '.css',
				array( 'kookoawpf-font' ),
				kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/css/upgrade' . $suffix . '.css' )
			);
			wp_enqueue_script(
				'kookoawpf-upgrade',
				KOOKOAWPF_PLUGIN_URL . 'assets/js/upgrade' . $suffix . '.js',
				array(),
				kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/upgrade' . $suffix . '.js' ),
				true
			);
			return;
		}

		$screen      = get_current_screen();
		$is_settings = strpos( $hook, 'kookoa-folders' ) !== false;
		$ok          = $is_settings
			|| ( $screen && in_array( $screen->base, array( 'upload', 'edit', 'post', 'page' ), true ) )
			|| kookoawpf()->is_builder_editor();
		if ( ! $ok ) {
			return; }

		// Access Control gate — skip on settings page (admin must configure).
		if ( ! $is_settings && ! kookoawpf_user_has_access() ) {
			return; }

		$is_builder = kookoawpf()->is_builder_editor();
		$suffix     = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';

		wp_enqueue_style( 'kookoawpf-font', KOOKOAWPF_PLUGIN_URL . 'assets/css/font' . $suffix . '.css', array(), KOOKOAWPF_VERSION );

		if ( $is_builder ) {
			$this->enqueue_builder_modal_assets( $suffix );
		} else {
			$css_ver = kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/css/admin' . $suffix . '.css' );
			$js_ver  = kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/admin' . $suffix . '.js' );

			wp_enqueue_style( 'kookoawpf-jstree', KOOKOAWPF_PLUGIN_URL . 'assets/css/jstree.min.css', array(), KOOKOAWPF_VERSION );
			wp_enqueue_script( 'kookoawpf-jstree', KOOKOAWPF_PLUGIN_URL . 'assets/js/jstree.min.js', array( 'jquery' ), KOOKOAWPF_VERSION, true );
			wp_enqueue_script( 'jquery-ui-draggable' );
			wp_enqueue_script( 'jquery-ui-droppable' );
			wp_enqueue_script( 'jquery-ui-sortable' );

			wp_enqueue_style( 'kookoawpf-admin', KOOKOAWPF_PLUGIN_URL . 'assets/css/admin' . $suffix . '.css', array( 'kookoawpf-font' ), $css_ver );
			wp_enqueue_style( 'kookoawpf-tree', KOOKOAWPF_PLUGIN_URL . 'assets/css/tree' . $suffix . '.css', array( 'kookoawpf-jstree' ), $css_ver );
			wp_enqueue_script( 'kookoawpf-utils', KOOKOAWPF_PLUGIN_URL . 'assets/js/kookoawpf-utils' . $suffix . '.js', array(), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/kookoawpf-utils' . $suffix . '.js' ), true );
			wp_enqueue_script( 'kookoawpf-admin', KOOKOAWPF_PLUGIN_URL . 'assets/js/admin' . $suffix . '.js', array( 'jquery', 'kookoawpf-jstree', 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-sortable', 'kookoawpf-utils' ), $js_ver, true );
			wp_enqueue_script( 'kookoawpf-custom-select', KOOKOAWPF_PLUGIN_URL . 'assets/js/custom-select' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/custom-select' . $suffix . '.js' ), true );
			wp_enqueue_style( 'kookoawpf-context-menu', KOOKOAWPF_PLUGIN_URL . 'assets/css/context-menu' . $suffix . '.css', array( 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/css/context-menu' . $suffix . '.css' ) );
			wp_enqueue_style( 'kookoawpf-settings', KOOKOAWPF_PLUGIN_URL . 'assets/css/settings' . $suffix . '.css', array( 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/css/settings' . $suffix . '.css' ) );
			wp_enqueue_style( 'kookoawpf-modals', KOOKOAWPF_PLUGIN_URL . 'assets/css/modals' . $suffix . '.css', array( 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/css/modals' . $suffix . '.css' ) );
			wp_enqueue_style( 'kookoawpf-bulk', KOOKOAWPF_PLUGIN_URL . 'assets/css/bulk' . $suffix . '.css', array( 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/css/bulk' . $suffix . '.css' ) );
			wp_enqueue_style( 'kookoawpf-breadcrumb', KOOKOAWPF_PLUGIN_URL . 'assets/css/breadcrumb' . $suffix . '.css', array( 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/css/breadcrumb' . $suffix . '.css' ) );
			wp_enqueue_style( 'kookoawpf-media-modal', KOOKOAWPF_PLUGIN_URL . 'assets/css/media-modal' . $suffix . '.css', array( 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/css/media-modal' . $suffix . '.css' ) );
			wp_enqueue_style( 'kookoawpf-smart-folder', KOOKOAWPF_PLUGIN_URL . 'assets/css/smart-folder' . $suffix . '.css', array( 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/css/smart-folder' . $suffix . '.css' ) );
			wp_enqueue_script( 'kookoawpf-context-menu', KOOKOAWPF_PLUGIN_URL . 'assets/js/context-menu' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/context-menu' . $suffix . '.js' ), true );
			wp_enqueue_script( 'kookoawpf-export-import', KOOKOAWPF_PLUGIN_URL . 'assets/js/export-import' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/export-import' . $suffix . '.js' ), true );
			wp_enqueue_script( 'kookoawpf-sidebar-ui', KOOKOAWPF_PLUGIN_URL . 'assets/js/sidebar-ui' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/sidebar-ui' . $suffix . '.js' ), true );
			wp_enqueue_script( 'kookoawpf-breadcrumb', KOOKOAWPF_PLUGIN_URL . 'assets/js/breadcrumb' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/breadcrumb' . $suffix . '.js' ), true );
			wp_enqueue_script( 'kookoawpf-folder-bulk', KOOKOAWPF_PLUGIN_URL . 'assets/js/folder-bulk' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/folder-bulk' . $suffix . '.js' ), true );
			wp_enqueue_script( 'kookoawpf-bulk-organize', KOOKOAWPF_PLUGIN_URL . 'assets/js/bulk-organize' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/bulk-organize' . $suffix . '.js' ), true );
			wp_enqueue_script( 'kookoawpf-dragdrop', KOOKOAWPF_PLUGIN_URL . 'assets/js/dragdrop' . $suffix . '.js', array( 'jquery', 'jquery-ui-draggable', 'jquery-ui-droppable', 'kookoawpf-admin' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/dragdrop' . $suffix . '.js' ), true );
			wp_enqueue_script( 'kookoawpf-modals', KOOKOAWPF_PLUGIN_URL . 'assets/js/modals' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin', 'kookoawpf-context-menu' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/modals' . $suffix . '.js' ), true );
			wp_enqueue_script( 'kookoawpf-download', KOOKOAWPF_PLUGIN_URL . 'assets/js/download' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin', 'kookoawpf-context-menu' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/download' . $suffix . '.js' ), true );
			wp_enqueue_script( 'kookoawpf-dynamic', KOOKOAWPF_PLUGIN_URL . 'assets/js/dynamic' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin', 'kookoawpf-custom-select', 'kookoawpf-context-menu' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/dynamic' . $suffix . '.js' ), true );
			wp_enqueue_script( 'kookoawpf-media-modal', KOOKOAWPF_PLUGIN_URL . 'assets/js/media-modal' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin', 'kookoawpf-custom-select', 'kookoawpf-context-menu' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/media-modal' . $suffix . '.js' ), true );
			wp_enqueue_script( 'kookoawpf-media-modal-dialogs', KOOKOAWPF_PLUGIN_URL . 'assets/js/media-modal-dialogs' . $suffix . '.js', array( 'jquery', 'kookoawpf-utils', 'kookoawpf-custom-select', 'kookoawpf-media-modal' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/media-modal-dialogs' . $suffix . '.js' ), true );
			wp_enqueue_script( 'kookoawpf-media-modal-interactions', KOOKOAWPF_PLUGIN_URL . 'assets/js/media-modal-interactions' . $suffix . '.js', array( 'jquery', 'kookoawpf-media-modal' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/media-modal-interactions' . $suffix . '.js' ), true );

			// Mobile adaptation (inert on desktop — CSS media queries + JS matchMedia).
			$mob_css = KOOKOAWPF_PLUGIN_DIR . 'assets/css/mobile' . $suffix . '.css';
			$mob_js  = KOOKOAWPF_PLUGIN_DIR . 'assets/js/mobile' . $suffix . '.js';
			if ( file_exists( $mob_css ) ) {
				wp_enqueue_style( 'kookoawpf-mobile', KOOKOAWPF_PLUGIN_URL . 'assets/css/mobile' . $suffix . '.css', array( 'kookoawpf-admin' ), kookoawpf()->file_ver( $mob_css ) );
			}
			if ( file_exists( $mob_js ) ) {
				wp_enqueue_script( 'kookoawpf-mobile', KOOKOAWPF_PLUGIN_URL . 'assets/js/mobile' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin' ), kookoawpf()->file_ver( $mob_js ), true );
			}
		}

		$pt = '';
		if ( $screen ) {
			$pt = ( 'upload' === $screen->base ) ? 'attachment' : ( $screen->post_type ? $screen->post_type : '' ); }

		wp_localize_script( 'kookoawpf-admin', 'kookoawpfData', KookoaWPF_Localize::instance()->get_localize_data( $pt, $is_builder ) );

		// Render builder modal JSON (emoji/color pickers) as inline data attached to kookoawpf-admin.
		$builder_modal_json = KookoaWPF_Localize::instance()->get_builder_modal_json();
		if ( $builder_modal_json ) {
			wp_add_inline_script( 'kookoawpf-admin', 'window.kookoawpfBuilderModals = ' . $builder_modal_json . ';', 'before' );
		}

		// Settings page JS (extracted from inline <script> in settings.php).
		if ( $is_settings ) {
			wp_enqueue_script( 'kookoawpf-settings', KOOKOAWPF_PLUGIN_URL . 'assets/js/settings' . $suffix . '.js', array( 'jquery', 'kookoawpf-admin', 'kookoawpf-custom-select' ), kookoawpf()->file_ver( KOOKOAWPF_PLUGIN_DIR . 'assets/js/settings' . $suffix . '.js' ), true );
		}
	}
}
