// Universal Scene Loader: Runs for both Admin and Embed document.addEventListener("DOMContentLoaded", function() { const checkPlanInterval = setInterval(() => { const plan = window.VISUALIZER_CONTEXT?.plan; if (plan) { clearInterval(checkPlanInterval); // --- 1. Load Scenes based on Plan --- // // ===================================== // APPLY UI THEME FROM SHOP SETTINGS // ===================================== if (window.VISUALIZER_CONTEXT?.mode === "frontend") { const theme = window.__SHOP_SETTINGS__?.uiTheme; if (theme) { const presetBtn = [...document.querySelectorAll(".ui-preset")] .find(btn => btn.textContent.trim().toLowerCase().includes(theme) ); if (presetBtn) { presetBtn.click(); console.log("[EMBED] UI theme applied:", theme); } } } // --- 1. Load Scenes based on Plan --- (function loadScenes(plan) { let sceneFile = "lib-scenes-trial.js"; // Default/Trial if (plan === "starter") sceneFile = "lib-scenes-starter.js"; if (plan === "pro") sceneFile = "lib-scenes-pro.js"; if (plan === "elite") sceneFile = "lib-scenes-elite.js"; // Don't reload if already loaded if (document.querySelector(`script[src="${sceneFile}"]`)) return; const s = document.createElement("script"); s.src = sceneFile; s.defer = true; s.onload = () => { console.log("[SCENES] Library loaded:", sceneFile); // Trigger the SceneMgr to refresh with the new library if (window.EnvironmentSystem?.SceneMgr?.refresh) { window.EnvironmentSystem.SceneMgr.refresh(); } }; document.head.appendChild(s); console.log("[SCENES] Injecting file for plan:", plan, "->", sceneFile); })(plan); // Initialize cache objects (moved OUTSIDE the loadModels function) window.__MODELS_CACHE__ = window.__MODELS_CACHE__ || {}; window.__BRAND_COLORS_CACHE__ = window.__BRAND_COLORS_CACHE__ || {}; window.__RIMS_CACHE__ = window.__RIMS_CACHE__ || {}; window.__GRADIENTS_CACHE__ = window.__GRADIENTS_CACHE__ || {}; window.__GRAPHICS_CACHE__ = window.__GRAPHICS_CACHE__ || {}; // --- 2. Load Car Models based on Plan --- (function loadModels(plan) { // Check cache first if (window.__MODELS_CACHE__[plan]) { window.CarModels = window.__MODELS_CACHE__[plan]; console.log('[MODELS] Using cached models for plan:', plan); window.dispatchEvent(new Event("modelsLibraryReady")); if (typeof window.rebuildCarLibraryUI === "function") { window.rebuildCarLibraryUI(); } return; } let modelFile = "models-trial.js"; if (plan === "starter") modelFile = "models-starter.js"; if (plan === "pro") modelFile = "models-pro.js"; if (plan === "elite") modelFile = "models-elite.js"; // Check if script already exists if (document.querySelector(`script[src="${modelFile}"]`)) return; const m = document.createElement("script"); m.src = modelFile; m.defer = true; m.onload = () => { // Cache the loaded models if (window.CarModels) { window.__MODELS_CACHE__[plan] = JSON.parse(JSON.stringify(window.CarModels)); } if (typeof window.rebuildCarLibraryUI === "function") { window.rebuildCarLibraryUI(); } console.log("[MODELS] Library loaded and cached:", modelFile); window.dispatchEvent(new Event("modelsLibraryReady")); }; document.head.appendChild(m); console.log("[MODELS] Injecting file for plan:", plan, "->", modelFile); })(plan); // --- 3. Load Brand Colors based on Plan --- (function loadBrandColors(plan) { // Check cache first if (window.__BRAND_COLORS_CACHE__ && window.__BRAND_COLORS_CACHE__[plan]) { window.BrandColors = window.__BRAND_COLORS_CACHE__[plan]; console.log('[BRAND] Using cached brand colors for plan:', plan); // Trigger rebuild of brand UI if function exists if (typeof window.rebuildBrandLibraryUI === 'function') { window.rebuildBrandLibraryUI(); } return; } let colorFile = "lib-colors-trial.js"; // Default/Trial if (plan === "starter") colorFile = "lib-colors-starter.js"; if (plan === "pro") colorFile = "lib-colors-pro.js"; if (plan === "elite") colorFile = "lib-colors-elite.js"; // Check if script already exists if (document.querySelector(`script[src="${colorFile}"]`)) return; const c = document.createElement("script"); c.src = colorFile; c.defer = true; c.onload = () => { // Initialize cache if it doesn't exist if (!window.__BRAND_COLORS_CACHE__) { window.__BRAND_COLORS_CACHE__ = {}; } // Cache the loaded brand colors if (window.BrandColors) { window.__BRAND_COLORS_CACHE__[plan] = JSON.parse(JSON.stringify(window.BrandColors)); } // Rebuild brand library UI if function exists if (typeof window.rebuildBrandLibraryUI === 'function') { window.rebuildBrandLibraryUI(); } console.log("[BRAND] Library loaded and cached:", colorFile); window.dispatchEvent(new Event("brandLibraryReady")); }; document.head.appendChild(c); console.log("[BRAND] Injecting file for plan:", plan, "->", colorFile); })(plan); // --- 4. Load Rims based on Plan --- (function loadRims(plan) { // Check cache first if (window.__RIMS_CACHE__ && window.__RIMS_CACHE__[plan]) { window.RimLibrary = window.__RIMS_CACHE__[plan]; console.log('[RIMS] Using cached rim library for plan:', plan); // Trigger rebuild of rim UI if function exists if (typeof window.rebuildRimLibraryUI === 'function') { window.rebuildRimLibraryUI(); } return; } let rimFile = "lib-rims-trial.js"; // Default/Trial if (plan === "starter") rimFile = "lib-rims-starter.js"; if (plan === "pro") rimFile = "lib-rims-pro.js"; if (plan === "elite") rimFile = "lib-rims-elite.js"; // Check if script already exists if (document.querySelector(`script[src="${rimFile}"]`)) return; const r = document.createElement("script"); r.src = rimFile; r.defer = true; r.onload = () => { // Initialize cache if it doesn't exist if (!window.__RIMS_CACHE__) { window.__RIMS_CACHE__ = {}; } // Cache the loaded rims if (window.RimLibrary) { window.__RIMS_CACHE__[plan] = JSON.parse(JSON.stringify(window.RimLibrary)); } // Rebuild rim library UI if function exists if (typeof window.rebuildRimLibraryUI === 'function') { window.rebuildRimLibraryUI(); } console.log("[RIMS] Library loaded and cached:", rimFile); window.dispatchEvent(new Event("rimLibraryReady")); }; document.head.appendChild(r); console.log("[RIMS] Injecting file for plan:", plan, "->", rimFile); })(plan); // --- 5. Load Gradients based on Plan --- (function loadGradients(plan) { // Check cache first if (window.__GRADIENTS_CACHE__ && window.__GRADIENTS_CACHE__[plan]) { window.GradientLibrary = window.__GRADIENTS_CACHE__[plan]; console.log('[GRADIENTS] Using cached gradient library for plan:', plan); // Trigger rebuild of gradient UI if function exists if (typeof window.rebuildGradientLibraryUI === 'function') { window.rebuildGradientLibraryUI(); } return; } let gradientFile = "lib-gradients-trial.js"; // Default/Trial if (plan === "starter") gradientFile = "lib-gradients-starter.js"; if (plan === "pro") gradientFile = "lib-gradients-pro.js"; if (plan === "elite") gradientFile = "lib-gradients-elite.js"; // Check if script already exists if (document.querySelector(`script[src="${gradientFile}"]`)) return; const g = document.createElement("script"); g.src = gradientFile; g.defer = true; g.onload = () => { // Initialize cache if it doesn't exist if (!window.__GRADIENTS_CACHE__) { window.__GRADIENTS_CACHE__ = {}; } // Cache the loaded gradients if (window.GradientLibrary) { window.__GRADIENTS_CACHE__[plan] = JSON.parse(JSON.stringify(window.GradientLibrary)); } // Rebuild gradient library UI if function exists if (typeof window.rebuildGradientLibraryUI === 'function') { window.rebuildGradientLibraryUI(); } console.log("[GRADIENTS] Library loaded and cached:", gradientFile); window.dispatchEvent(new Event("gradientLibraryReady")); }; document.head.appendChild(g); console.log("[GRADIENTS] Injecting file for plan:", plan, "->", gradientFile); })(plan); // --- 6. Load Graphics based on Plan --- (function loadGraphics(plan) { // Check cache first if (window.__GRAPHICS_CACHE__ && window.__GRAPHICS_CACHE__[plan]) { window.GraphicsLibrary = window.__GRAPHICS_CACHE__[plan]; console.log('[GRAPHICS] Using cached graphics library for plan:', plan); // Trigger rebuild of graphics UI if function exists if (typeof window.rebuildGraphicsLibraryUI === 'function') { window.rebuildGraphicsLibraryUI(); } return; } let graphicsFile = "lib-graphics-trial.js"; // Default/Trial if (plan === "starter") graphicsFile = "lib-graphics-starter.js"; if (plan === "pro") graphicsFile = "lib-graphics-pro.js"; if (plan === "elite") graphicsFile = "lib-graphics-elite.js"; // Check if script already exists if (document.querySelector(`script[src="${graphicsFile}"]`)) return; const gr = document.createElement("script"); gr.src = graphicsFile; gr.defer = true; gr.onload = () => { // Initialize cache if it doesn't exist if (!window.__GRAPHICS_CACHE__) { window.__GRAPHICS_CACHE__ = {}; } // Cache the loaded graphics if (window.GraphicsLibrary) { window.__GRAPHICS_CACHE__[plan] = JSON.parse(JSON.stringify(window.GraphicsLibrary)); } // Rebuild graphics library UI if function exists if (typeof window.rebuildGraphicsLibraryUI === 'function') { window.rebuildGraphicsLibraryUI(); } console.log("[GRAPHICS] Library loaded and cached:", graphicsFile); window.dispatchEvent(new Event("graphicsLibraryReady")); }; document.head.appendChild(gr); console.log("[GRAPHICS] Injecting file for plan:", plan, "->", graphicsFile); })(plan); } }, 100); });