import { UIDLAIAssistantChat } from '@teleporthq/teleport-types' function styleObjToString(styles: Record): string { return JSON.stringify(styles) } function positionStyles(position: string): Record { switch (position) { case 'bottom-left': return { position: 'fixed', bottom: '24px', left: '24px', zIndex: '9999' } case 'top-right': return { position: 'fixed', top: '24px', right: '24px', zIndex: '9999' } case 'top-left': return { position: 'fixed', top: '24px', left: '24px', zIndex: '9999' } default: return { position: 'fixed', bottom: '24px', right: '24px', zIndex: '9999' } } } function windowPositionStyles(position: string): Record { const base: Record = { position: 'fixed', width: '380px', maxWidth: 'calc(100vw - 32px)', height: '520px', maxHeight: 'calc(100vh - 120px)', zIndex: '9998', display: 'flex', flexDirection: 'column', overflow: 'hidden', } switch (position) { case 'bottom-left': return { ...base, bottom: '96px', left: '24px' } case 'top-right': return { ...base, top: '96px', right: '24px' } case 'top-left': return { ...base, top: '96px', left: '24px' } default: return { ...base, bottom: '96px', right: '24px' } } } export function generateWidgetCode(chat: UIDLAIAssistantChat): string { const settings = chat.chatSettings const pos = settings.bubblePosition || 'bottom-right' const bubblePos = positionStyles(pos) const windowPos = windowPositionStyles(pos) const needsUseEffect = !!settings.custom?.scripts return `${ needsUseEffect ? "import { useEffect } from 'react';\n" : '' }import useAIChat from '../../hooks/useAIChat'; import AIChatBubble from './AIChatBubble'; import AIChatWindow from './AIChatWindow'; var BUBBLE_POSITION_STYLES = ${styleObjToString(bubblePos)}; var BUBBLE_CUSTOM_STYLES = ${styleObjToString(settings.bubbleStyles || {})}; var WINDOW_POSITION_STYLES = ${styleObjToString(windowPos)}; var WINDOW_CUSTOM_STYLES = ${styleObjToString(settings.window?.windowStyles || {})}; var CHAT_NAME = ${JSON.stringify(settings.chatName || 'AI Assistant')}; ${settings.custom?.styles ? `var CUSTOM_STYLES = ${JSON.stringify(settings.custom.styles)};` : ''} export default function AIChatWidget() { var chat = useAIChat(); ${ settings.custom?.scripts ? ` useEffect(function() { try { (new Function(${JSON.stringify(settings.custom.scripts)}))(); } catch(_e) {} }, []); ` : '' } return ( <> ${ settings.custom?.styles ? `