
import { useEffect } from 'react';

interface MessageHandlerProps {
  onNavigate: (url: string) => void;
  onSummary: (summary: string) => void;
  onTitleChange: (title: string) => void;
}

const MessageHandler = ({ onNavigate, onSummary, onTitleChange }: MessageHandlerProps) => {
  useEffect(() => {
    const handleMessage = (event: MessageEvent) => {
      console.log('Received message:', event.data);
      
      if (event.data?.type === 'navigate' && event.data.url) {
        onNavigate(event.data.url);
      } else if (event.data?.type === 'summary') {
        onSummary(event.data.summary);
      } else if (event.data?.type === 'title-change') {
        onTitleChange(event.data.title);
      }
    };

    window.addEventListener('message', handleMessage);
    return () => window.removeEventListener('message', handleMessage);
  }, [onNavigate, onSummary, onTitleChange]);

  return null;
};

export default MessageHandler;
