import { jsx } from "react/jsx-runtime";
import { useLingui } from "@lingui/react";
import { Fade } from "@mui/material";
import { useEffect, useState } from "react";
import { ApTypography } from "../../../../ap-typography/index.js";
import { useChatService } from "../../../providers/chat-service.provider.js";
import { useChatState } from "../../../providers/chat-state-provider.js";
import { AutopilotChatEvent } from "../../../service/index.js";
const SECONDS = 1000;
const FADE_DURATION = 0.5 * SECONDS;
const DEFAULT_MESSAGE_DURATION = 5 * SECONDS;
const defaultMessages = [
    'autopilot-chat-generating-response'
];
const LoadingMessage = ()=>{
    const { _ } = useLingui();
    const chatService = useChatService();
    const [messageIdx, setMessageIdx] = useState(0);
    const [isVisible, setIsVisible] = useState(true);
    const [messages, setMessages] = useState(chatService.getDefaultLoadingMessages() ?? defaultMessages);
    const [messageDuration, setMessageDuration] = useState(chatService.getLoadingMessageDuration() ?? DEFAULT_MESSAGE_DURATION);
    const { spacing } = useChatState();
    useEffect(()=>{
        if (!chatService) return;
        let setDefaultLoadingMessagesTimeout;
        let setLoadingMessageTimeout;
        const unsubscribeSetDefaultLoadingMessages = chatService.on(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(AutopilotChatEvent.SetLoadingMessage, (message)=>{
            setIsVisible(false);
            setLoadingMessageTimeout = setTimeout(()=>{
                setMessageIdx(0);
                setMessages([
                    message
                ]);
                setIsVisible(true);
            }, FADE_DURATION);
        });
        return ()=>{
            unsubscribeSetDefaultLoadingMessages();
            unsubscribeSetLoadingMessage();
            clearTimeout(setDefaultLoadingMessagesTimeout);
            clearTimeout(setLoadingMessageTimeout);
        };
    }, [
        chatService
    ]);
    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__*/ jsx(Fade, {
        in: isVisible,
        timeout: FADE_DURATION,
        children: /*#__PURE__*/ jsx(ApTypography, {
            variant: spacing.primaryFontToken,
            color: 'var(--color-foreground)',
            "aria-live": "polite",
            children: translatedMessage
        })
    });
};
export { LoadingMessage };
