<?php

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

class AIEO_Module_Manifest {

    const FLAG_OPTION = 'aieo_use_context_router';

    
    public static function is_enabled() {
        return (bool) get_option(self::FLAG_OPTION, 0);
    }

    
    public static function resolve_context() {
        static $cached = null;
        if ($cached !== null) { return $cached; }

        
        
        if (defined('REST_REQUEST') && REST_REQUEST) { return $cached = 'rest_api'; }
        if (defined('DOING_CRON') && DOING_CRON)     { return $cached = 'cron'; }
        if (defined('WP_CLI') && WP_CLI)             { return $cached = 'cron'; }

        
        if (!empty($_GET['wc-ajax']) || !empty($_REQUEST['wc-ajax'])) {
            return $cached = 'frontend_ajax';
        }

        
        
        
        if (wp_doing_ajax()) {
            return $cached = 'frontend_ajax';
        }

        
        if (is_admin()) { return $cached = 'admin'; }

        
        
        
        
        
        
        
        
        
        $uri = isset($_SERVER['REQUEST_URI']) ? (string) sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '/';
        $path = strtok($uri, '?');           
        $path = '/' . trim((string) $path, '/'); 

        
        
        
        $slug_map = array(
            '/cart'        => 'cart',
            '/checkout'    => 'checkout',
            '/my-account'  => 'account',
            '/account'     => 'account',
            '/shop'        => 'archive',
        );
        foreach ($slug_map as $slug => $label) {
            if ($path === $slug || strpos($path, $slug . '/') === 0) {
                return $cached = $label;
            }
        }

        
        if (strpos($path, '/product/') === 0) {
            return $cached = 'product';
        }

        
        foreach (array('/product-category/', '/product-tag/', '/product-brand/', '/product-need/', '/product-supplier/', '/marca/', '/brand/') as $prefix) {
            if (strpos($path, $prefix) === 0) {
                return $cached = 'archive';
            }
        }

        if ($path === '/') { return $cached = 'homepage'; }

        return $cached = 'other_frontend';
    }

    
    public static function reset_context_cache() {
        
        
    }

    
    public static function manifest() {
        return array(
            
            'AIEO_DMM_Sticky_ATC'            => array('product', 'frontend_ajax'),
            'AIEO_DMM_Complementary'         => array('product', 'cart', 'frontend_ajax'),
            'AIEO_DMM_Variations_Gallery'    => array('product', 'frontend_ajax'),
            'AIEO_DMM_Product_Video'         => array('product'),
            'AIEO_DMM_Product_Meta'          => array('product'),
            'AIEO_DMM_Trust_Badges'          => array('product', 'cart', 'checkout'),
            'AIEO_DMM_Add_To_Cart_Text'      => array('product', 'archive', 'frontend_ajax'),
            'AIEO_DMM_Product_Labels'        => array('product', 'archive'),

            
            'AIEO_DMM_Side_Cart'             => array('cart', 'product', 'archive', 'frontend_ajax'),
            'AIEO_DMM_Min_Order'             => array('cart', 'checkout', 'frontend_ajax'),
            'AIEO_DMM_Payment_Gating'        => array('checkout', 'frontend_ajax'),
            'AIEO_DMM_Payment_Logos'         => array('checkout'),
            'AIEO_DMM_Checkout_Modal'        => array('checkout', 'cart'),
            'AIEO_DMM_Checkout_Invoicing'    => array('checkout', 'frontend_ajax'),
            'AIEO_DMM_Checkout_Product_Images'=> array('checkout'),
            'AIEO_DMM_Address_Autocomplete'  => array('checkout', 'frontend_ajax'),

            
            'AIEO_DMM_Account_Handling'      => array('account', 'frontend_ajax'),

            
            'AIEO_DMM_Free_Shipping_Display' => array('cart', 'product', 'archive', 'frontend_ajax'),
            'AIEO_DMM_Wishlist_Display'      => array('product', 'archive', 'account', 'frontend_ajax'),
            'AIEO_DMM_Ratings_Display'       => array('product', 'archive', 'account', 'frontend_ajax'),
            'AIEO_DMM_Waitlist_Display'      => array('product', 'archive', 'frontend_ajax'),
            'AIEO_DMM_Web_Push_Display'      => array('always_frontend'),

            
            'AIEO_DMM_Navigator'             => array('always_frontend'),

            
            'AIEO_DMM_Swatches'              => array('product', 'archive', 'frontend_ajax'),

            
            
            
            'AIEO_DMM_Role_Pricing'          => array('product', 'archive', 'cart', 'checkout', 'frontend_ajax', 'rest_api'),
            'AIEO_DMM_Brand_Discounts'       => array('product', 'archive', 'cart', 'checkout', 'frontend_ajax', 'rest_api'),
            'AIEO_DMM_Brand_Gifts'           => array('cart', 'checkout', 'frontend_ajax'),
        );
    }

    
    public static function should_load($module_class, $context = null) {
        if (!self::is_enabled()) {
            
            return true;
        }
        $manifest = self::manifest();
        if (!isset($manifest[$module_class])) {
            
            return true;
        }
        $contexts = (array) $manifest[$module_class];
        if (in_array('always_frontend', $contexts, true)) {
            
            $ctx = $context ?: self::resolve_context();
            return !in_array($ctx, array('admin', 'admin_ajax', 'rest_api', 'cron'), true);
        }
        return in_array($context ?: self::resolve_context(), $contexts, true);
    }
}
