/**
 * Dynamic icon loader utility for react-icons
 * Supports loading icons from various react-icons sets (io5, fa, md, lu, etc.)
 */
'use client';
import React from 'react';
/**
 * Icon set module paths for dynamic imports
 */
const ICON_SET_MODULES = {
    io5: 'react-icons/io5',
    io: 'react-icons/io',
    fa: 'react-icons/fa',
    fa6: 'react-icons/fa6',
    md: 'react-icons/md',
    lu: 'react-icons/lu',
    bi: 'react-icons/bi',
    bs: 'react-icons/bs',
    ai: 'react-icons/ai',
    fi: 'react-icons/fi',
    gi: 'react-icons/gi',
    go: 'react-icons/go',
    gr: 'react-icons/gr',
    hi: 'react-icons/hi',
    hi2: 'react-icons/hi2',
    im: 'react-icons/im',
    ri: 'react-icons/ri',
    si: 'react-icons/si',
    sl: 'react-icons/sl',
    tb: 'react-icons/tb',
    tfi: 'react-icons/tfi',
    ti: 'react-icons/ti',
    vsc: 'react-icons/vsc',
    wi: 'react-icons/wi',
    cg: 'react-icons/cg',
    ci: 'react-icons/ci',
    rx: 'react-icons/rx',
    pi: 'react-icons/pi',
    lia: 'react-icons/lia',
};
/**
 * Cache for loaded icon modules to avoid re-importing
 */
const icon_module_cache = {};
/**
 * Cache for individual icons
 */
const icon_cache = {};
/**
 * Get cache key for an icon
 */
function get_cache_key(icon_name, icon_set) {
    return `${icon_set}:${icon_name}`;
}
/**
 * Load an icon dynamically from a react-icons set
 *
 * @param icon_name - The icon component name (e.g., 'IoCheckmarkCircle', 'FaCheck')
 * @param icon_set - The icon set identifier (e.g., 'io5', 'fa', 'md')
 * @returns Promise resolving to the icon component, or null if not found
 *
 * @example
 * const Icon = await load_icon('IoCheckmarkCircle', 'io5');
 * if (Icon) {
 *   return <Icon className="h-5 w-5" />;
 * }
 */
export async function load_icon(icon_name, icon_set = 'io5') {
    const cache_key = get_cache_key(icon_name, icon_set);
    // Return from cache if available
    if (icon_cache[cache_key]) {
        return icon_cache[cache_key];
    }
    // Check if icon set is supported
    const module_path = ICON_SET_MODULES[icon_set.toLowerCase()];
    if (!module_path) {
        console.warn(`[icon_loader] Unknown icon set: ${icon_set}`);
        return null;
    }
    try {
        // Check module cache first
        if (!icon_module_cache[icon_set]) {
            // Dynamic import of the icon set module
            const module = await import(/* webpackIgnore: true */ module_path);
            icon_module_cache[icon_set] = module;
        }
        const module = icon_module_cache[icon_set];
        const IconComponent = module[icon_name];
        if (!IconComponent) {
            console.warn(`[icon_loader] Icon not found: ${icon_name} in ${icon_set}`);
            return null;
        }
        // Cache the icon
        icon_cache[cache_key] = IconComponent;
        return IconComponent;
    }
    catch (error) {
        console.error(`[icon_loader] Failed to load icon: ${icon_name} from ${icon_set}`, error);
        return null;
    }
}
/**
 * Load multiple icons at once
 *
 * @param icons - Array of { name, set } objects
 * @returns Promise resolving to a map of cache_key -> component
 */
export async function load_icons(icons) {
    const results = {};
    await Promise.all(icons.map(async ({ name, set = 'io5' }) => {
        const cache_key = get_cache_key(name, set);
        results[cache_key] = await load_icon(name, set);
    }));
    return results;
}
/**
 * Hook to load an icon with React state management
 * Returns the icon component when loaded, or null while loading
 *
 * @param icon_name - The icon component name
 * @param icon_set - The icon set identifier (default: 'io5')
 * @returns The icon component or null
 *
 * @example
 * function MyComponent() {
 *   const CheckIcon = use_icon('IoCheckmarkCircle', 'io5');
 *   return CheckIcon ? <CheckIcon className="h-5 w-5" /> : null;
 * }
 */
export function use_icon(icon_name, icon_set = 'io5') {
    const [Icon, setIcon] = React.useState(null);
    React.useEffect(() => {
        if (!icon_name) {
            setIcon(null);
            return;
        }
        let cancelled = false;
        load_icon(icon_name, icon_set).then((loaded_icon) => {
            if (!cancelled) {
                setIcon(() => loaded_icon);
            }
        });
        return () => {
            cancelled = true;
        };
    }, [icon_name, icon_set]);
    return Icon;
}
/**
 * Preload icons into cache for faster access later
 * Call this early in your app to preload commonly used icons
 *
 * @param icons - Array of { name, set } objects to preload
 */
export async function preload_icons(icons) {
    await load_icons(icons);
}
/**
 * Clear the icon cache (useful for testing or memory management)
 */
export function clear_icon_cache() {
    Object.keys(icon_cache).forEach((key) => {
        delete icon_cache[key];
    });
    Object.keys(icon_module_cache).forEach((key) => {
        delete icon_module_cache[key];
    });
}
/**
 * Get list of supported icon sets
 */
export function get_supported_icon_sets() {
    return Object.keys(ICON_SET_MODULES);
}
//# sourceMappingURL=icon_loader.js.map