"use strict";
var __webpack_require__ = {};
(()=>{
    __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__, {
    LoadingMessage: ()=>LoadingMessage
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const react_namespaceObject = require("@lingui/react");
const material_namespaceObject = require("@mui/material");
const external_react_namespaceObject = require("react");
const index_cjs_namespaceObject = require("../../../../ap-typography/index.cjs");
const chat_service_provider_cjs_namespaceObject = require("../../../providers/chat-service.provider.cjs");
const chat_state_provider_cjs_namespaceObject = require("../../../providers/chat-state-provider.cjs");
const external_service_index_cjs_namespaceObject = require("../../../service/index.cjs");
const SECONDS = 1000;
const FADE_DURATION = 0.5 * SECONDS;
const DEFAULT_MESSAGE_DURATION = 5 * SECONDS;
const defaultMessages = [
    'autopilot-chat-generating-response'
];
const LoadingMessage = ()=>{
    const { _ } = (0, react_namespaceObject.useLingui)();
    const chatService = (0, chat_service_provider_cjs_namespaceObject.useChatService)();
    const [messageIdx, setMessageIdx] = (0, external_react_namespaceObject.useState)(0);
    const [isVisible, setIsVisible] = (0, external_react_namespaceObject.useState)(true);
    const [messages, setMessages] = (0, external_react_namespaceObject.useState)(chatService.getDefaultLoadingMessages() ?? defaultMessages);
    const [messageDuration, setMessageDuration] = (0, external_react_namespaceObject.useState)(chatService.getLoadingMessageDuration() ?? DEFAULT_MESSAGE_DURATION);
    const { spacing } = (0, chat_state_provider_cjs_namespaceObject.useChatState)();
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (!chatService) return;
        let setDefaultLoadingMessagesTimeout;
        let setLoadingMessageTimeout;
        const unsubscribeSetDefaultLoadingMessages = chatService.on(external_service_index_cjs_namespaceObject.AutopilotChatEvent.SetDefaultLoadingMessages, (data)=>{
            setIsVisible(false);
            setDefaultLoadingMessagesTimeout = setTimeout(()=>{
                setMessageIdx(0);
                setMessages(data.messages);
                setMessageDuration(data.duration ?? DEFAULT_MESSAGE_DURATION);
                setIsVisible(true);
            }, FADE_DURATION);
        });
        const unsubscribeSetLoadingMessage = chatService.on(external_service_index_cjs_namespaceObject.AutopilotChatEvent.SetLoadingMessage, (message)=>{
            setIsVisible(false);
            setLoadingMessageTimeout = setTimeout(()=>{
                setMessageIdx(0);
                setMessages([
                    message
                ]);
                setIsVisible(true);
            }, FADE_DURATION);
        });
        return ()=>{
            unsubscribeSetDefaultLoadingMessages();
            unsubscribeSetLoadingMessage();
            clearTimeout(setDefaultLoadingMessagesTimeout);
            clearTimeout(setLoadingMessageTimeout);
        };
    }, [
        chatService
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (1 === messages.length) return;
        let childTimeout;
        const parentTimeout = setTimeout(()=>{
            setIsVisible(false);
            childTimeout = setTimeout(()=>{
                setMessageIdx((prev)=>prev === messages.length - 1 ? 0 : prev + 1);
                setIsVisible(true);
            }, FADE_DURATION);
        }, messageDuration);
        return ()=>{
            clearTimeout(parentTimeout);
            clearTimeout(childTimeout);
        };
    }, [
        messageIdx,
        messageDuration,
        messages
    ]);
    const currentMessage = messages[messageIdx];
    const translatedMessage = 'autopilot-chat-generating-response' === currentMessage ? _({
        id: 'autopilot-chat.message.generating-response'
    }) : currentMessage;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.Fade, {
        in: isVisible,
        timeout: FADE_DURATION,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
            variant: spacing.primaryFontToken,
            color: 'var(--color-foreground)',
            "aria-live": "polite",
            children: translatedMessage
        })
    });
};
exports.LoadingMessage = __webpack_exports__.LoadingMessage;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "LoadingMessage"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
