export interface PostMessageEvent { type: string; payload?: any; } export class PostMessageAPI { private targetOrigin: string; constructor(targetOrigin: string = '*') { this.targetOrigin = targetOrigin; } // Send events to parent window send(type: string, payload?: any): void { if (typeof window !== 'undefined' && window.parent) { window.parent.postMessage( { type, payload, source: 'virtual-banker-widget', }, this.targetOrigin ); } } // Listen for messages from parent window on(type: string, callback: (payload?: any) => void): () => void { const handler = (event: MessageEvent) => { if (event.data && event.data.type === type && event.data.source === 'virtual-banker-host') { callback(event.data.payload); } }; window.addEventListener('message', handler); // Return unsubscribe function return () => { window.removeEventListener('message', handler); }; } // Widget events ready(): void { this.send('ready'); } sessionStarted(sessionId: string): void { this.send('session_started', { sessionId }); } actionRequested(action: string, params: any): void { this.send('action_requested', { action, params }); } actionCompleted(action: string, result: any): void { this.send('action_completed', { action, result }); } handoffToHuman(reason: string): void { this.send('handoff_to_human', { reason }); } }