(() => { try { console.log('AEVOICE Widget: Initialization started'); const s = document.currentScript; const agentId = s?.dataset?.agentId || s?.getAttribute('data-agent-id'); const clientId = s?.dataset?.clientId || s?.getAttribute('data-client-id') || ''; const cfg = (window.aevoiceConfig || {}); console.log('AEVOICE Widget: Configuration loaded', { agentId, clientId, config: cfg }); if (!agentId) { console.warn('AEVOICE: data-agent-id missing'); return; } const HOST = (s?.dataset?.host) || new URL(s.src || window.location.href, window.location.href).origin; const PAGE_PATH = '/WidgetHost'; const pos = (cfg.position || 'bottom-right'); const btnColor = cfg.buttonColor || '#0e4166'; const panelW = Number(cfg.panelWidth || 380); const panelH = Number(cfg.panelHeight || 560); const offsetX = Number(cfg.offsetX || 0); const offsetY = Number(cfg.offsetY || 0); const z = String(cfg.zIndex || 2147483647); const openOnLoad = !!cfg.openOnLoad; const buttonShape = cfg.buttonShape || 'pill'; // Button container const container = document.createElement('div'); container.id = 'aevoice-widget-button'; container.style.position = 'fixed'; container.style.zIndex = z; const setPos = () => { const base = 24; const y = base + offsetY; const x = base + offsetX; if (pos.includes('bottom')) container.style.bottom = y + 'px'; else container.style.top = y + 'px'; if (pos.includes('right')) container.style.right = x + 'px'; else container.style.left = x + 'px'; }; setPos(); const button = document.createElement('button'); button.style.minWidth = '52px'; button.style.height = '52px'; button.style.padding = '0 14px'; button.style.borderRadius = '999px'; button.style.border = 'none'; button.style.background = btnColor; button.style.color = '#fff'; button.style.font = '600 14px system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial'; button.style.cursor = 'pointer'; button.style.boxShadow = '0 10px 20px rgba(0,0,0,0.15)'; button.textContent = (cfg.buttonText || 'Chat'); if (buttonShape === 'circle') { button.style.minWidth = '52px'; button.style.width = '52px'; button.style.borderRadius = '50%'; button.textContent = ''; } container.appendChild(button); // Panel const panel = document.createElement('div'); panel.id = 'aevoice-widget-panel'; panel.style.position = 'fixed'; panel.style.width = panelW + 'px'; panel.style.height = panelH + 'px'; panel.style.maxWidth = '95vw'; panel.style.maxHeight = '85vh'; panel.style.borderRadius = '16px'; panel.style.overflow = 'hidden'; panel.style.boxShadow = '0 20px 40px rgba(0,0,0,0.2)'; panel.style.background = '#fff'; panel.style.display = 'none'; panel.style.zIndex = z; if (pos.includes('bottom')) panel.style.bottom = (88 + offsetY) + 'px'; else panel.style.top = (88 + offsetY) + 'px'; if (pos.includes('right')) panel.style.right = (24 + offsetX) + 'px'; else panel.style.left = (24 + offsetX) + 'px'; const iframe = document.createElement('iframe'); const cfgStr = btoa(unescape(encodeURIComponent(JSON.stringify(cfg)))); const url = HOST + PAGE_PATH + '?agent_id=' + encodeURIComponent(agentId) + '&client_id=' + encodeURIComponent(clientId) + '&config=' + encodeURIComponent(cfgStr); iframe.src = url; iframe.style.width = '100%'; iframe.style.height = '100%'; iframe.style.border = '0'; iframe.allow = 'microphone; clipboard-read; clipboard-write;'; panel.appendChild(iframe); const attachSel = cfg.attachToSelector; const parent = attachSel ? document.querySelector(attachSel) : null; if (parent) { parent.style.position = parent.style.position || 'relative'; container.style.position = 'absolute'; panel.style.position = 'absolute'; parent.appendChild(panel); parent.appendChild(container); } else { document.body.appendChild(panel); document.body.appendChild(container); } let open = false; const toggle = () => { open = !open; panel.style.display = open ? 'block' : 'none'; }; button.addEventListener('click', toggle); if (openOnLoad) { toggle(); } if (cfg.proactiveGreeting && Number(cfg.showAfterSeconds) >= 0) { setTimeout(() => { if (!open) toggle(); }, (cfg.showAfterSeconds || 5) * 1000); } console.log('AEVOICE Widget: Successfully initialized', { agentId, clientId }); } catch (e) { console.error('AEVOICE widget failed', e); } })();