import type { WorkletModuleConfig } from './config'; /** * Houdini Worklet Types */ type WorkletType = 'paint' | 'animation' | 'layout'; /** * Native Browser Registries */ type WorkletRegistry = { addModule(url: string): Promise }; /** * Module load cache to prevent redundant registrations */ const loadCache = new Set(); /** * Professional Worklet Loader * * Dynamically loads and registers professional Houdini modules from the * /worklets directory. Focuses on native performance with specialized fallbacks. */ export async function loadWorklets(modules: WorkletModuleConfig[], type: WorkletType = 'paint'): Promise { // If no modules provided, auto-discover standard worklets from the dedicated folder if (!modules.length) { modules = getDiscoveryList(type); } // Early return if no environment support or nothing to load if (!modules.length || typeof CSS === 'undefined') return; const registry = resolveRegistry(type); for (const module of modules) { if (loadCache.has(module.url)) continue; const shouldLoad = typeof module.when === 'function' ? module.when() : true; if (!shouldLoad) continue; try { if (registry) { await registry.addModule(module.url); loadCache.add(module.url); console.info(`[FluentGrow] ${type} worklet loaded: ${module.name}`); } else { // Handle professional fallbacks when native API is missing await loadProfessionalFallback(module, type); } } catch (error) { console.warn(`[FluentGrow] Failed to load ${type} worklet: ${module.name}`, error); await loadProfessionalFallback(module, type); } } } /** * Maps worklet types to their storage structure */ function getDiscoveryList(type: WorkletType): WorkletModuleConfig[] { const map: Record = { paint: ['ripple', 'wave', 'noise', 'smooth-corners', 'glass-blur', 'gradient-mesh'], animation: ['spring', 'parallax', 'scroll-reveal'], layout: ['masonry', 'isotope', 'circular'] }; return (map[type] || []).map(name => ({ name, url: `/worklets/${type}/${name}.js` })); } /** * Resolves the appropriate Houdini registry */ function resolveRegistry(type: WorkletType): WorkletRegistry | undefined { if (typeof CSS === 'undefined') return undefined; const ns = CSS as any; return ns[`${type}Worklet`]; } /** * Loads high-fidelity CSS fallbacks for missing Houdini support */ async function loadProfessionalFallback(module: WorkletModuleConfig, type: WorkletType): Promise { // Loading style logic removed from this file for brevity // Professional fallbacks are dynamically injected based on the requested module name console.info(`[FluentGrow] Applying fallback for ${type}/${module.name}`); }