import { jsx } from "react/jsx-runtime";
import react from "react";
import { AutopilotChatEvent, AutopilotChatInterceptableEvent, AutopilotChatInternalEvent, AutopilotChatRole } from "../service/index.js";
import { useChatService } from "./chat-service.provider.js";
const AutopilotLoadingContext = /*#__PURE__*/ react.createContext({
    waitingResponse: false,
    isLoadingMoreMessages: false,
    setWaitingResponse: ()=>{},
    showLoading: false,
    skeletonLoader: false
});
function AutopilotLoadingProvider({ children }) {
    const [waitingResponse, setWaitingResponse] = react.useState(false);
    const [isLoadingMoreMessages, setIsLoadingMoreMessages] = react.useState(false);
    const [showLoading, setShowLoading] = react.useState(false);
    const [skeletonLoader, setSkeletonLoader] = react.useState(false);
    const disableDefaultShowLoadingBehaviorRef = react.useRef(false);
    const disableDefaultSetWaitingBehaviorRef = react.useRef(false);
    const chatService = useChatService();
    react.useEffect(()=>{
        if (!chatService) return;
        const unsubscribeShowLoadingState = chatService.__internalService__.on(AutopilotChatInternalEvent.ShowLoadingState, (showLoadingState)=>{
            setSkeletonLoader(showLoadingState);
        });
        const unsubscribeShouldShowLoadingMoreMessages = chatService.__internalService__.on(AutopilotChatInternalEvent.ShouldShowLoadingMoreMessages, (value)=>{
            if (!value) setIsLoadingMoreMessages(false);
        });
        const unsubscribeSetIsLoadingMoreMessages = chatService.__internalService__.on(AutopilotChatInternalEvent.SetIsLoadingMoreMessages, (value)=>{
            setIsLoadingMoreMessages(value);
        });
        const unsubscribeSetShowLoading = chatService.__internalService__.on(AutopilotChatInternalEvent.SetShowLoading, (value)=>{
            disableDefaultShowLoadingBehaviorRef.current = true;
            setShowLoading(value);
        });
        const unsubscribeSetWaiting = chatService.__internalService__.on(AutopilotChatInternalEvent.SetWaiting, (value)=>{
            disableDefaultSetWaitingBehaviorRef.current = true;
            setWaitingResponse(value);
        });
        const unsubscribeSetConversation = chatService.on(AutopilotChatEvent.SetConversation, (conversation)=>{
            const lastMessage = conversation?.[conversation.length - 1];
            if (lastMessage?.role === AutopilotChatRole.Assistant) {
                if (!disableDefaultSetWaitingBehaviorRef.current) setWaitingResponse(!!lastMessage.shouldWaitForMoreMessages);
                if (!disableDefaultShowLoadingBehaviorRef.current) setShowLoading(false);
            }
        });
        const unsubscribeRequest = chatService.intercept(AutopilotChatInterceptableEvent.Request, ()=>{
            setWaitingResponse(true);
            setShowLoading(true);
        });
        const unsubscribeResponse = chatService.on(AutopilotChatEvent.Response, (message)=>{
            if (!disableDefaultSetWaitingBehaviorRef.current) setWaitingResponse(!!message.shouldWaitForMoreMessages);
            if (!disableDefaultShowLoadingBehaviorRef.current) setShowLoading(false);
        });
        const unsubscribeStopResponse = chatService.on(AutopilotChatEvent.StopResponse, ()=>{
            setWaitingResponse(false);
            setShowLoading(false);
        });
        const unsubscribeNewChat = chatService.on(AutopilotChatEvent.NewChat, ()=>{
            setWaitingResponse(false);
            setShowLoading(false);
        });
        return ()=>{
            unsubscribeShowLoadingState();
            unsubscribeRequest();
            unsubscribeResponse();
            unsubscribeStopResponse();
            unsubscribeNewChat();
            unsubscribeSetIsLoadingMoreMessages();
            unsubscribeShouldShowLoadingMoreMessages();
            unsubscribeSetShowLoading();
            unsubscribeSetWaiting();
            unsubscribeSetConversation();
        };
    }, [
        chatService
    ]);
    return /*#__PURE__*/ jsx(AutopilotLoadingContext.Provider, {
        value: {
            waitingResponse,
            setWaitingResponse,
            isLoadingMoreMessages,
            showLoading,
            skeletonLoader
        },
        children: children
    });
}
function useLoading() {
    const context = react.useContext(AutopilotLoadingContext);
    if (!context) throw new Error('useLoading must be used within a AutopilotLoadingProvider');
    return context;
}
export { AutopilotLoadingContext, AutopilotLoadingProvider, useLoading };
