import { BuzzTrailWidget } from './widget/Widget'; import { applyTheme } from '@buzztrail/ui-themes'; declare const BUILD_THEME: string | undefined; declare global { interface Window { BuzzTrail?: BuzzTrailWidget; BuzzTrailWidget?: typeof BuzzTrailWidget; } } function waitForDOM(): Promise { return new Promise((resolve) => { if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', () => resolve()); } else { resolve(); } }); } async function bootstrap(): Promise { if (window.BuzzTrail) { return; } // Wait for DOM to be ready await waitForDOM(); // Additional safety check for document.body if (!document.body) { console.warn('[Chatbot] Document body not available, retrying in 100ms...'); setTimeout(() => { void bootstrap(); }, 100); return; } const widget = new BuzzTrailWidget(); window.BuzzTrail = widget; // Expose the class globally for manual instantiation window.BuzzTrailWidget = BuzzTrailWidget; const buildTheme = (typeof BUILD_THEME !== 'undefined' && BUILD_THEME) ? BUILD_THEME : undefined; try { const currentScript = document.currentScript; if (currentScript) { const ds = currentScript.dataset as Record; const themeName = ds.btTheme ?? buildTheme ?? 'sendcloud'; applyTheme(themeName); const personaId = ds.btPersonaId ?? undefined; const replicaId = ds.btReplicaId ?? undefined; const maxSeconds = ds.btMaxSeconds ? Number(ds.btMaxSeconds) : undefined; const mode = (ds.btMode as 'chat-first' | 'video-first' | undefined) ?? undefined; const chatSessionId = ds.btChatSession ?? 'default'; const fullScreenOnVideo = ds.btFullscreen ? ds.btFullscreen !== 'false' : true; const chatEnabled = ds.btChatEnabled ? ds.btChatEnabled !== 'false' : true; const apiBase = ds.btApiBase ?? ''; const headers = ds.btHeaders ? JSON.parse(ds.btHeaders) as Record : undefined; // Visibility configuration const showChat = ds.btShowChat ? ds.btShowChat !== 'false' : undefined; const showVideo = ds.btShowVideo ? ds.btShowVideo !== 'false' : undefined; // Size and positioning configuration const size = ds.btSize as 'minimal' | 'medium' | 'large' | undefined; const startSize = ds.btStartSize as 'minimal' | 'medium' | 'large' | undefined; const sizeOverride = ds.btSizeOverride as 'minimal' | 'medium' | 'large' | undefined; const sizeRules = ds.btSizeRules ? JSON.parse(ds.btSizeRules) as Record : undefined; const autoSize = ds.btAutoSize ? ds.btAutoSize === 'true' : undefined; const enableDrag = ds.btEnableDrag ? ds.btEnableDrag !== 'false' : undefined; const enableResize = ds.btEnableResize ? ds.btEnableResize !== 'false' : undefined; widget.init({ personaId, replicaId, maxCallSeconds: maxSeconds ?? 15, mode: mode ?? 'video-first', theme: themeName, chatSessionId, fullScreenOnVideo, chatEnabled, apiBase, headers, showChat, showVideo, size, startSize, sizeOverride, sizeRules, autoSize, enableDrag, enableResize }); widget.mount(); } else { const themeName = buildTheme ?? 'sendcloud'; applyTheme(themeName); widget.init({ mode: 'video-first', theme: themeName }); widget.mount(); } } catch (error) { console.error('[Chatbot] Error during initialization:', error); const themeName = buildTheme ?? 'sendcloud'; applyTheme(themeName); widget.init({ mode: 'video-first', theme: themeName }); widget.mount(); } } // Start the bootstrap process bootstrap().catch(error => { console.error('[Chatbot] Failed to bootstrap:', error); }); console.log('Chatbot script loaded with theme sendcloud');