<?php
/**
 * Modular admin script loader.
 *
 * Modules are declared once, in load order, as handle => [file, deps].
 * A module is enqueued only when its file is actually present in the build,
 * and a missing module is dropped from every other module's dependency list.
 *
 * That is what keeps the free WordPress.org package honest: the paid modules
 * live in assets/js/modules/premium/*__premium_only.js, Freemius strips those
 * files from the free package, and the loader then simply has nothing to
 * enqueue — no payment check, no disabled UI, no dead handles.
 *
 * @package Schemafy
 * @since 2.0.0
 */

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

class Schemafy_Modular_Scripts {

    /**
     * Admin modules in load order: handle => array( file, deps ).
     *
     * Deps list plugin handles only; 'jquery' is added to every module.
     *
     * @var array
     */
    private static $modules = array(
        'schemafy-utilities'           => array( 'modules/utilities/notifications.js',        array() ),
        'schemafy-core'                => array( 'modules/core/init.js',                      array( 'schemafy-utilities' ) ),
        'schemafy-dashboard'           => array( 'modules/dashboard/dashboard.js',            array( 'schemafy-utilities', 'schemafy-core' ) ),
        'schemafy-validation'          => array( 'modules/editor/validation.js',              array( 'schemafy-utilities' ) ),
        'schemafy-editor'              => array( 'modules/editor/manual-editor.js',           array( 'schemafy-utilities', 'schemafy-validation' ) ),
        'schemafy-operations'          => array( 'modules/operations/schema-crud.js',         array( 'schemafy-utilities' ) ),
        'schemafy-url-manager'         => array( 'modules/operations/url-management.js',      array( 'schemafy-utilities', 'schemafy-operations' ) ),
        'schemafy-bulk'                => array( 'modules/bulk/bulk-manager.js',              array( 'schemafy-utilities', 'schemafy-operations' ) ),
        'schemafy-filters'             => array( 'modules/bulk/filters.js',                   array( 'schemafy-utilities', 'schemafy-bulk' ) ),
        'schemafy-builder'             => array( 'modules/builder/smart-builder.js',          array( 'schemafy-utilities', 'schemafy-operations' ) ),
        'schemafy-sitemap'             => array( 'modules/sitemap/sitemap.js',                array( 'schemafy-utilities' ) ),
        'schemafy-csv'                 => array( 'modules/import-export/csv-handler.js',      array( 'schemafy-utilities', 'schemafy-bulk' ) ),
        'schemafy-admin-modular'       => array( 'modules/admin-modular.js',                  array( 'schemafy-utilities', 'schemafy-core', 'schemafy-dashboard', 'schemafy-validation', 'schemafy-editor', 'schemafy-operations', 'schemafy-url-manager', 'schemafy-bulk', 'schemafy-filters', 'schemafy-builder', 'schemafy-sitemap', 'schemafy-csv' ) ),

        // Paid tools — absent from the free package (see class docblock).
        'schemafy-quick-actions'       => array( 'modules/premium/quick-actions__premium_only.js',   array( 'schemafy-utilities', 'schemafy-bulk', 'schemafy-admin-modular' ) ),
        'schemafy-ai-generator'        => array( 'modules/premium/ai-generator__premium_only.js',    array( 'schemafy-utilities', 'schemafy-validation', 'schemafy-editor', 'schemafy-dashboard', 'schemafy-admin-modular' ) ),
        'schemafy-auto-generator-tab'  => array( 'modules/premium/auto-generator__premium_only.js',  array( 'schemafy-utilities', 'schemafy-operations', 'schemafy-dashboard', 'schemafy-admin-modular' ) ),
    );

    /**
     * Enqueue every module present in this build, in declaration order.
     *
     * @return void
     */
    public static function enqueue_modular_scripts() {
        $enqueued = array();

        foreach ( self::$modules as $handle => $module ) {
            list( $file, $deps ) = $module;

            if ( ! file_exists( SCHEMAFY_PLUGIN_PATH . 'assets/js/' . $file ) ) {
                continue;
            }

            wp_enqueue_script(
                $handle,
                SCHEMAFY_PLUGIN_URL . 'assets/js/' . $file,
                array_merge( array( 'jquery' ), array_intersect( $deps, $enqueued ) ),
                SCHEMAFY_VERSION,
                true
            );

            $enqueued[] = $handle;
        }

        // Legacy alias: other scripts declare a dependency on this handle to
        // mean "after the whole admin bundle".
        wp_register_script( 'schemafy-schema-admin-v2', false, $enqueued, SCHEMAFY_VERSION, true );
        wp_enqueue_script( 'schemafy-schema-admin-v2' );
    }

}
