import { jsx } from "react/jsx-runtime";
import apollo_core, { FontVariantToken } from "@uipath/apollo-core";
import react from "react";
import { AutopilotChatEvent, AutopilotChatInternalEvent, AutopilotChatMode, CHAT_COMPACT_MODE_INPUT_MAX_ROWS, CHAT_COMPACT_MODE_INPUT_MIN_ROWS, CHAT_COMPACT_MODE_MESSAGE_GROUP_GAP, CHAT_COMPACT_MODE_MESSAGE_SPACING, CHAT_INPUT_MAX_ROWS, CHAT_INPUT_MIN_ROWS, CHAT_MESSAGE_GROUP_GAP, CHAT_MESSAGE_SPACING, CHAT_SUGGESTION_SPACING } from "../service/index.js";
import { useChatService } from "./chat-service.provider.js";
const AutopilotChatStateContext = /*#__PURE__*/ react.createContext(null);
const calculateSpacing = (chatSpacing)=>{
    const compactMode = chatSpacing?.compactMode ?? false;
    const markdownTokens = {
        li: compactMode ? FontVariantToken.fontSizeS : FontVariantToken.fontSizeM,
        p: compactMode ? FontVariantToken.fontSizeS : FontVariantToken.fontSizeM,
        h1: compactMode ? FontVariantToken.fontSizeH4Bold : FontVariantToken.fontSizeH3Bold,
        h2: compactMode ? FontVariantToken.fontSizeH4Bold : FontVariantToken.fontSizeH3Bold,
        h3: compactMode ? FontVariantToken.fontSizeH4Bold : FontVariantToken.fontSizeH3Bold,
        h4: compactMode ? FontVariantToken.fontSizeMBold : FontVariantToken.fontSizeLBold,
        h5: compactMode ? FontVariantToken.fontSizeMBold : FontVariantToken.fontSizeLBold,
        h6: compactMode ? FontVariantToken.fontSizeMBold : FontVariantToken.fontSizeLBold,
        th: compactMode ? FontVariantToken.fontSizeS : FontVariantToken.fontSizeM,
        td: compactMode ? FontVariantToken.fontSizeS : FontVariantToken.fontSizeM,
        em: compactMode ? FontVariantToken.fontSizeS : FontVariantToken.fontSizeM,
        del: compactMode ? FontVariantToken.fontSizeS : FontVariantToken.fontSizeM,
        strong: compactMode ? FontVariantToken.fontSizeS : FontVariantToken.fontSizeM,
        link: compactMode ? FontVariantToken.fontSizeS : FontVariantToken.fontSizeM,
        citation: compactMode ? FontVariantToken.fontSizeXs : FontVariantToken.fontSizeM
    };
    const promptBox = {
        minRows: compactMode ? CHAT_COMPACT_MODE_INPUT_MIN_ROWS : CHAT_INPUT_MIN_ROWS,
        maxRows: compactMode ? CHAT_COMPACT_MODE_INPUT_MAX_ROWS : CHAT_INPUT_MAX_ROWS
    };
    const messageSpacing = compactMode ? CHAT_COMPACT_MODE_MESSAGE_SPACING : CHAT_MESSAGE_SPACING;
    const messageGroupGap = compactMode ? CHAT_COMPACT_MODE_MESSAGE_GROUP_GAP : CHAT_MESSAGE_GROUP_GAP;
    const primaryFontToken = compactMode ? FontVariantToken.fontSizeS : FontVariantToken.fontSizeM;
    const primaryBoldFontToken = compactMode ? FontVariantToken.fontSizeSBold : FontVariantToken.fontSizeMBold;
    const titleFontToken = FontVariantToken.fontSizeH4;
    const suggestionSpacing = CHAT_SUGGESTION_SPACING;
    const suggestionFontToken = compactMode ? FontVariantToken.fontSizeS : FontVariantToken.fontSizeM;
    const suggestionPadding = compactMode ? `${apollo_core.Spacing.SpacingMicro} ${apollo_core.Spacing.SpacingXs}` : `${apollo_core.Spacing.SpacingXs} ${apollo_core.Spacing.SpacingBase}`;
    return {
        compactMode,
        promptBox: {
            minRows: chatSpacing?.promptBox?.minRows ?? promptBox.minRows,
            maxRows: chatSpacing?.promptBox?.maxRows ?? promptBox.maxRows
        },
        primaryFontToken: chatSpacing?.primaryFontToken ?? primaryFontToken,
        primaryBoldFontToken: chatSpacing?.primaryBoldFontToken ?? primaryBoldFontToken,
        titleFontToken: chatSpacing?.titleFontToken ?? titleFontToken,
        suggestionSpacing: chatSpacing?.suggestionSpacing ?? suggestionSpacing,
        suggestionFontToken: chatSpacing?.suggestionFontToken ?? suggestionFontToken,
        suggestionPadding: chatSpacing?.suggestionPadding ?? suggestionPadding,
        messageSpacing: chatSpacing?.messageSpacing ?? messageSpacing,
        messageGroupGap: chatSpacing?.messageGroupGap ?? messageGroupGap,
        markdownTokens: {
            li: chatSpacing?.markdownTokens?.li ?? markdownTokens.li,
            p: chatSpacing?.markdownTokens?.p ?? markdownTokens.p,
            h1: chatSpacing?.markdownTokens?.h1 ?? markdownTokens.h1,
            h2: chatSpacing?.markdownTokens?.h2 ?? markdownTokens.h2,
            h3: chatSpacing?.markdownTokens?.h3 ?? markdownTokens.h3,
            h4: chatSpacing?.markdownTokens?.h4 ?? markdownTokens.h4,
            h5: chatSpacing?.markdownTokens?.h5 ?? markdownTokens.h5,
            h6: chatSpacing?.markdownTokens?.h6 ?? markdownTokens.h6,
            th: chatSpacing?.markdownTokens?.th ?? markdownTokens.th,
            td: chatSpacing?.markdownTokens?.td ?? markdownTokens.td,
            em: chatSpacing?.markdownTokens?.em ?? markdownTokens.em,
            del: chatSpacing?.markdownTokens?.del ?? markdownTokens.del,
            strong: chatSpacing?.markdownTokens?.strong ?? markdownTokens.strong,
            link: chatSpacing?.markdownTokens?.link ?? markdownTokens.link,
            citation: chatSpacing?.markdownTokens?.citation ?? markdownTokens.citation
        }
    };
};
const AutopilotChatStateProvider = ({ children, portalContainer })=>{
    const chatService = useChatService();
    const chatInternalService = chatService.__internalService__;
    const [allowedAttachments, setAllowedAttachments] = react.useState(chatService?.getConfig()?.allowedAttachments ?? {
        multiple: false,
        types: {},
        maxSize: 0,
        maxCount: 0
    });
    const [historyAnchorElement, setHistoryAnchorElement] = react.useState(null);
    const [fullScreenContainer, setFullScreenContainer] = react.useState(null);
    const [historyOpen, setHistoryOpen] = react.useState(chatService?.historyOpen ?? false);
    const [settingsOpen, setSettingsOpen] = react.useState(chatService?.settingsOpen ?? false);
    const [chatMode, setChatMode] = react.useState(chatService?.getConfig()?.mode ?? AutopilotChatMode.SideBySide);
    const [disabledFeatures, setDisabledFeatures] = react.useState(chatService?.getConfig()?.disabledFeatures ?? {});
    const [overrideLabels, setOverrideLabels] = react.useState(chatService?.getConfig()?.overrideLabels ?? {});
    const [firstRunExperience, setFirstRunExperience] = react.useState(chatService?.getConfig()?.firstRunExperience ?? {
        title: '',
        description: '',
        suggestions: []
    });
    const [hasMessages, setHasMessages] = react.useState(false);
    const [readOnly, setReadOnly] = react.useState(chatService?.getReadOnly() ?? false);
    const [spacing, setSpacing] = react.useState(calculateSpacing(chatService?.getConfig()?.spacing));
    const [theming, setTheming] = react.useState(chatService?.getConfig()?.theming);
    react.useEffect(()=>{
        if (!chatService) return;
        const unsubscribeHistoryToggle = chatInternalService.on(AutopilotChatInternalEvent.ToggleHistory, (isOpen)=>{
            setHistoryOpen(isOpen);
        });
        const unsubscribeSettingsToggle = chatInternalService.on(AutopilotChatInternalEvent.ToggleSettings, (isOpen)=>{
            setSettingsOpen(isOpen);
        });
        const unsubscribeModeChange = chatService.on(AutopilotChatEvent.ModeChange, (mode)=>{
            setChatMode(mode);
        });
        const unsubscribeDisabledFeatures = chatService.on(AutopilotChatEvent.SetDisabledFeatures, (features)=>{
            setDisabledFeatures(features);
        });
        const unsubscribeOverrideLabels = chatService.on(AutopilotChatEvent.SetOverrideLabels, (labels)=>{
            setOverrideLabels(labels);
        });
        const unsubscribeFirstRunExperience = chatService.on(AutopilotChatEvent.SetFirstRunExperience, (experience)=>{
            setFirstRunExperience(experience);
        });
        const unsubscribeAllowedAttachments = chatInternalService.on(AutopilotChatInternalEvent.SetAllowedAttachments, (allowed)=>{
            setAllowedAttachments(allowed);
        });
        const unsubscribeReadOnly = chatService.on(AutopilotChatEvent.SetReadOnly, (isReadOnly)=>{
            setReadOnly(isReadOnly);
        });
        const unsubscribeSpacing = chatInternalService.on(AutopilotChatInternalEvent.SetSpacing, (spacingConfig)=>{
            setSpacing(calculateSpacing(spacingConfig));
        });
        const unsubscribeTheming = chatInternalService.on(AutopilotChatInternalEvent.SetTheming, (themingConfig)=>{
            setTheming(themingConfig);
        });
        return ()=>{
            unsubscribeHistoryToggle();
            unsubscribeSettingsToggle();
            unsubscribeModeChange();
            unsubscribeDisabledFeatures();
            unsubscribeOverrideLabels();
            unsubscribeFirstRunExperience();
            unsubscribeReadOnly();
            unsubscribeAllowedAttachments();
            unsubscribeSpacing();
            unsubscribeTheming();
        };
    }, [
        chatService,
        chatInternalService
    ]);
    const value = react.useMemo(()=>({
            historyOpen,
            settingsOpen,
            chatMode,
            disabledFeatures,
            overrideLabels,
            firstRunExperience,
            allowedAttachments,
            historyAnchorElement,
            setHistoryAnchorElement,
            fullScreenContainer,
            setFullScreenContainer,
            hasMessages,
            setHasMessages,
            spacing,
            theming,
            portalContainer,
            readOnly
        }), [
        historyOpen,
        settingsOpen,
        chatMode,
        disabledFeatures,
        overrideLabels,
        firstRunExperience,
        allowedAttachments,
        historyAnchorElement,
        setHistoryAnchorElement,
        fullScreenContainer,
        setFullScreenContainer,
        hasMessages,
        setHasMessages,
        spacing,
        theming,
        portalContainer,
        readOnly
    ]);
    return /*#__PURE__*/ jsx(AutopilotChatStateContext.Provider, {
        value: value,
        children: children
    });
};
const useChatState = ()=>{
    const context = react.useContext(AutopilotChatStateContext);
    if (!context) throw new Error('useChatState must be used within a AutopilotChatStateProvider');
    return context;
};
export { AutopilotChatStateProvider, useChatState };
