import { jsx } from "react/jsx-runtime";
import react from "react";
import { AutopilotChatEvent } from "../service/index.js";
import { useChatService } from "./chat-service.provider.js";
const AutopilotStreamingContext = /*#__PURE__*/ react.createContext({
    streaming: false,
    setStreaming: ()=>{}
});
function AutopilotStreamingProvider({ children }) {
    const [streaming, setStreaming] = react.useState(false);
    const chatService = useChatService();
    react.useEffect(()=>{
        if (!chatService) return;
        const unsubscribeStopResponse = chatService.on(AutopilotChatEvent.StopResponse, ()=>{
            setStreaming(false);
        });
        const unsubscribeStreaming = chatService.on(AutopilotChatEvent.SendChunk, (message)=>{
            setStreaming(!message.done);
        });
        const unsubscribeResponse = chatService.on(AutopilotChatEvent.Response, (message)=>{
            message.stream && !message.done ? setStreaming(true) : setStreaming(false);
        });
        return ()=>{
            unsubscribeStreaming();
            unsubscribeStopResponse();
            unsubscribeResponse();
        };
    }, [
        chatService
    ]);
    return /*#__PURE__*/ jsx(AutopilotStreamingContext.Provider, {
        value: {
            streaming,
            setStreaming
        },
        children: children
    });
}
function useStreaming() {
    const context = react.useContext(AutopilotStreamingContext);
    if (!context) throw new Error('useStreaming must be used within a AutopilotStreamingProvider');
    return context;
}
export { AutopilotStreamingContext, AutopilotStreamingProvider, useStreaming };
