"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = (module)=>{
        var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    AutopilotLoadingProvider: ()=>AutopilotLoadingProvider,
    AutopilotLoadingContext: ()=>AutopilotLoadingContext,
    useLoading: ()=>useLoading
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const index_cjs_namespaceObject = require("../service/index.cjs");
const external_chat_service_provider_cjs_namespaceObject = require("./chat-service.provider.cjs");
const AutopilotLoadingContext = /*#__PURE__*/ external_react_default().createContext({
    waitingResponse: false,
    isLoadingMoreMessages: false,
    setWaitingResponse: ()=>{},
    showLoading: false,
    skeletonLoader: false
});
function AutopilotLoadingProvider({ children }) {
    const [waitingResponse, setWaitingResponse] = external_react_default().useState(false);
    const [isLoadingMoreMessages, setIsLoadingMoreMessages] = external_react_default().useState(false);
    const [showLoading, setShowLoading] = external_react_default().useState(false);
    const [skeletonLoader, setSkeletonLoader] = external_react_default().useState(false);
    const disableDefaultShowLoadingBehaviorRef = external_react_default().useRef(false);
    const disableDefaultSetWaitingBehaviorRef = external_react_default().useRef(false);
    const chatService = (0, external_chat_service_provider_cjs_namespaceObject.useChatService)();
    external_react_default().useEffect(()=>{
        if (!chatService) return;
        const unsubscribeShowLoadingState = chatService.__internalService__.on(index_cjs_namespaceObject.AutopilotChatInternalEvent.ShowLoadingState, (showLoadingState)=>{
            setSkeletonLoader(showLoadingState);
        });
        const unsubscribeShouldShowLoadingMoreMessages = chatService.__internalService__.on(index_cjs_namespaceObject.AutopilotChatInternalEvent.ShouldShowLoadingMoreMessages, (value)=>{
            if (!value) setIsLoadingMoreMessages(false);
        });
        const unsubscribeSetIsLoadingMoreMessages = chatService.__internalService__.on(index_cjs_namespaceObject.AutopilotChatInternalEvent.SetIsLoadingMoreMessages, (value)=>{
            setIsLoadingMoreMessages(value);
        });
        const unsubscribeSetShowLoading = chatService.__internalService__.on(index_cjs_namespaceObject.AutopilotChatInternalEvent.SetShowLoading, (value)=>{
            disableDefaultShowLoadingBehaviorRef.current = true;
            setShowLoading(value);
        });
        const unsubscribeSetWaiting = chatService.__internalService__.on(index_cjs_namespaceObject.AutopilotChatInternalEvent.SetWaiting, (value)=>{
            disableDefaultSetWaitingBehaviorRef.current = true;
            setWaitingResponse(value);
        });
        const unsubscribeSetConversation = chatService.on(index_cjs_namespaceObject.AutopilotChatEvent.SetConversation, (conversation)=>{
            const lastMessage = conversation?.[conversation.length - 1];
            if (lastMessage?.role === index_cjs_namespaceObject.AutopilotChatRole.Assistant) {
                if (!disableDefaultSetWaitingBehaviorRef.current) setWaitingResponse(!!lastMessage.shouldWaitForMoreMessages);
                if (!disableDefaultShowLoadingBehaviorRef.current) setShowLoading(false);
            }
        });
        const unsubscribeRequest = chatService.intercept(index_cjs_namespaceObject.AutopilotChatInterceptableEvent.Request, ()=>{
            setWaitingResponse(true);
            setShowLoading(true);
        });
        const unsubscribeResponse = chatService.on(index_cjs_namespaceObject.AutopilotChatEvent.Response, (message)=>{
            if (!disableDefaultSetWaitingBehaviorRef.current) setWaitingResponse(!!message.shouldWaitForMoreMessages);
            if (!disableDefaultShowLoadingBehaviorRef.current) setShowLoading(false);
        });
        const unsubscribeStopResponse = chatService.on(index_cjs_namespaceObject.AutopilotChatEvent.StopResponse, ()=>{
            setWaitingResponse(false);
            setShowLoading(false);
        });
        const unsubscribeNewChat = chatService.on(index_cjs_namespaceObject.AutopilotChatEvent.NewChat, ()=>{
            setWaitingResponse(false);
            setShowLoading(false);
        });
        return ()=>{
            unsubscribeShowLoadingState();
            unsubscribeRequest();
            unsubscribeResponse();
            unsubscribeStopResponse();
            unsubscribeNewChat();
            unsubscribeSetIsLoadingMoreMessages();
            unsubscribeShouldShowLoadingMoreMessages();
            unsubscribeSetShowLoading();
            unsubscribeSetWaiting();
            unsubscribeSetConversation();
        };
    }, [
        chatService
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(AutopilotLoadingContext.Provider, {
        value: {
            waitingResponse,
            setWaitingResponse,
            isLoadingMoreMessages,
            showLoading,
            skeletonLoader
        },
        children: children
    });
}
function useLoading() {
    const context = external_react_default().useContext(AutopilotLoadingContext);
    if (!context) throw new Error('useLoading must be used within a AutopilotLoadingProvider');
    return context;
}
exports.AutopilotLoadingContext = __webpack_exports__.AutopilotLoadingContext;
exports.AutopilotLoadingProvider = __webpack_exports__.AutopilotLoadingProvider;
exports.useLoading = __webpack_exports__.useLoading;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AutopilotLoadingContext",
    "AutopilotLoadingProvider",
    "useLoading"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
