import { jsx } from "react/jsx-runtime";
import react from "react";
import { AutopilotChatEvent } from "../service/index.js";
import { useChatService } from "./chat-service.provider.js";
const AutopilotErrorContext = /*#__PURE__*/ react.createContext({
    error: void 0,
    setError: ()=>{},
    clearError: ()=>{}
});
function AutopilotErrorProvider({ children }) {
    const chatService = useChatService();
    const [error, setErrorState] = react.useState(chatService?.getError() ?? void 0);
    react.useEffect(()=>{
        if (!chatService) return;
        const unsubscribe = chatService.on(AutopilotChatEvent.Error, (errorData)=>{
            setErrorState(errorData);
        });
        const unsubscribeNewChat = chatService.on(AutopilotChatEvent.NewChat, ()=>{
            setErrorState(void 0);
        });
        return ()=>{
            unsubscribe();
            unsubscribeNewChat();
        };
    }, [
        chatService
    ]);
    return /*#__PURE__*/ jsx(AutopilotErrorContext.Provider, {
        value: {
            error,
            setError: chatService?.setError,
            clearError: chatService?.clearError
        },
        children: children
    });
}
function useError() {
    const context = react.useContext(AutopilotErrorContext);
    if (!context) throw new Error('useError must be used within a AutopilotErrorProvider');
    return context;
}
export { AutopilotErrorContext, AutopilotErrorProvider, useError };
