export type WidgetConfig = { botId: string; apiKey: string; serverUrl: string; position?: 'bottom-right' | 'bottom-left'; shadow: boolean; }; export function parseConfig(): WidgetConfig | null { const script = (document.currentScript as HTMLScriptElement | null) ?? (document.querySelector( 'script[data-bot-id][data-api-key]', ) as HTMLScriptElement | null); if (!script) return null; const botId = script.getAttribute('data-bot-id'); const apiKey = script.getAttribute('data-api-key'); const serverUrl = script.getAttribute('data-server-url') ?? deriveServerFromSrc(script.src); const position = (script.getAttribute('data-position') as | 'bottom-right' | 'bottom-left' | null) ?? 'bottom-right'; const shadow = script.getAttribute('data-shadow') === 'true'; if (!botId || !apiKey) return null; return { botId, apiKey, serverUrl, position, shadow }; } function deriveServerFromSrc(src: string): string { try { return new URL(src).origin; } catch { return 'https://messenger-bot.mind4solutions.cloud'; } }