import { jsx, jsxs } from "react/jsx-runtime";
import { ThemeProvider, styled } from "@mui/material/styles";
import apollo_core from "@uipath/apollo-core";
import react from "react";
import { createPortal } from "react-dom";
import { ApI18nProvider } from "../../../i18n/index.js";
import { apolloMaterialUiThemeDark, apolloMaterialUiThemeDarkHC, apolloMaterialUiThemeLight, apolloMaterialUiThemeLightHC } from "../../theme/index.js";
import { DragHandle } from "./components/common/drag-handle.js";
import { AutopilotChatDropzone } from "./components/dropzone/dropzone.js";
import { FullScreenLayout, StandardLayout } from "./components/layout/index.js";
import { AutopilotAttachmentsProvider } from "./providers/attachements-provider.js";
import { AutopilotChatScrollProvider } from "./providers/chat-scroll-provider.js";
import { AutopilotChatServiceProvider } from "./providers/chat-service.provider.js";
import { AutopilotChatStateProvider, useChatState } from "./providers/chat-state-provider.js";
import { AutopilotChatWidthProvider, useChatWidth } from "./providers/chat-width-provider.js";
import { AutopilotErrorProvider } from "./providers/error-provider.js";
import { AutopilotLoadingProvider } from "./providers/loading-provider.js";
import { LocaleProvider, useLocale } from "./providers/locale-provider.js";
import { AutopilotPickerProvider } from "./providers/picker-provider.js";
import { AutopilotResourceDataProvider } from "./providers/resource-data-provider.js";
import { AutopilotStreamingProvider } from "./providers/streaming-provider.js";
import { ThemeProvider as theme_provider_js_ThemeProvider } from "./providers/theme-provider.js";
import { AutopilotChatEvent, AutopilotChatMode, CHAT_CONTAINER_ANIMATION_DURATION, CHAT_WIDTH_FULL_SCREEN } from "./service/index.js";
const MUI_THEME_MAP = {
    light: apolloMaterialUiThemeLight,
    dark: apolloMaterialUiThemeDark,
    'light-hc': apolloMaterialUiThemeLightHC,
    'dark-hc': apolloMaterialUiThemeDarkHC
};
const ChatContainer = styled('div')(({ shouldAnimate, mode, width, fullHeight })=>({
        width: mode === AutopilotChatMode.FullScreen ? CHAT_WIDTH_FULL_SCREEN : width,
        display: 'flex',
        flexDirection: mode === AutopilotChatMode.FullScreen ? 'column' : 'row',
        height: fullHeight ? '100vh' : 'calc(100vh - 48px)',
        position: 'relative',
        boxSizing: 'border-box',
        border: `${apollo_core.Border.BorderThickS} solid var(--color-border-de-emp)`,
        borderTop: 'none',
        borderLeft: 'none',
        ...shouldAnimate && {
            transition: `width ${CHAT_CONTAINER_ANIMATION_DURATION}ms ease`
        },
        ...mode === AutopilotChatMode.Closed && {
            display: 'none'
        },
        ...mode === AutopilotChatMode.Embedded && {
            width: '100%',
            height: '100%',
            position: 'absolute',
            border: 'none'
        }
    }));
const ApI18nWithLocale = /*#__PURE__*/ react.memo(({ children })=>{
    const { locale } = useLocale();
    return /*#__PURE__*/ jsx(ApI18nProvider, {
        component: "material/components/ap-chat",
        locale: locale,
        children: children
    });
});
const AutopilotChatContent = /*#__PURE__*/ react.memo(()=>{
    const { width, shouldAnimate } = useChatWidth();
    const { historyOpen, settingsOpen, disabledFeatures, chatMode, readOnly } = useChatState();
    return /*#__PURE__*/ jsxs(ChatContainer, {
        shouldAnimate: shouldAnimate,
        mode: chatMode,
        width: width,
        fullHeight: false === disabledFeatures.fullHeight,
        children: [
            chatMode === AutopilotChatMode.SideBySide && /*#__PURE__*/ jsx(DragHandle, {}),
            chatMode === AutopilotChatMode.FullScreen ? /*#__PURE__*/ jsx(FullScreenLayout, {
                historyOpen: historyOpen,
                settingsOpen: settingsOpen,
                historyDisabled: disabledFeatures.history ?? false,
                settingsDisabled: disabledFeatures.settings ?? false,
                headerSeparatorDisabled: disabledFeatures.headerSeparator ?? false,
                mode: chatMode,
                readOnly: readOnly
            }) : /*#__PURE__*/ jsx(StandardLayout, {
                historyOpen: historyOpen,
                settingsOpen: settingsOpen,
                historyDisabled: disabledFeatures.history ?? false,
                settingsDisabled: disabledFeatures.settings ?? false,
                headerDisabled: disabledFeatures.header ?? false,
                headerSeparatorDisabled: disabledFeatures.headerSeparator ?? false,
                mode: chatMode,
                readOnly: readOnly
            })
        ]
    });
});
function ApChat({ chatServiceInstance, locale: initialLocale = 'en', theme: initialTheme = 'light', portalContainer, enableInternalThemeProvider = false, disableEmbeddedPortal = false }) {
    const [embeddedContainer, setEmbeddedContainer] = react.useState(null);
    const [currentTheme, setCurrentTheme] = react.useState(initialTheme);
    react.useEffect(()=>{
        chatServiceInstance.setLocale(initialLocale);
    }, [
        initialLocale,
        chatServiceInstance
    ]);
    react.useEffect(()=>{
        chatServiceInstance.setTheme(initialTheme);
        setCurrentTheme(initialTheme);
    }, [
        initialTheme,
        chatServiceInstance
    ]);
    react.useEffect(()=>{
        const updateEmbeddedContainer = ()=>{
            const config = chatServiceInstance.getConfig();
            config.mode === AutopilotChatMode.Embedded && config.embeddedContainer ? setEmbeddedContainer(config.embeddedContainer) : setEmbeddedContainer(null);
        };
        updateEmbeddedContainer();
        const unsubscribe = chatServiceInstance.on(AutopilotChatEvent.ModeChange, ()=>{
            updateEmbeddedContainer();
        });
        return ()=>{
            unsubscribe();
        };
    }, [
        chatServiceInstance
    ]);
    const muiTheme = enableInternalThemeProvider ? MUI_THEME_MAP[currentTheme] : null;
    const chatProviders = /*#__PURE__*/ jsx(AutopilotChatServiceProvider, {
        chatServiceInstance: chatServiceInstance,
        children: /*#__PURE__*/ jsx(theme_provider_js_ThemeProvider, {
            children: /*#__PURE__*/ jsx(LocaleProvider, {
                children: /*#__PURE__*/ jsx(ApI18nWithLocale, {
                    children: /*#__PURE__*/ jsx(AutopilotStreamingProvider, {
                        children: /*#__PURE__*/ jsx(AutopilotChatScrollProvider, {
                            children: /*#__PURE__*/ jsx(AutopilotChatStateProvider, {
                                portalContainer: portalContainer,
                                children: /*#__PURE__*/ jsx(AutopilotErrorProvider, {
                                    children: /*#__PURE__*/ jsx(AutopilotLoadingProvider, {
                                        children: /*#__PURE__*/ jsx(AutopilotAttachmentsProvider, {
                                            children: /*#__PURE__*/ jsx(AutopilotPickerProvider, {
                                                children: /*#__PURE__*/ jsx(AutopilotResourceDataProvider, {
                                                    children: /*#__PURE__*/ jsx(AutopilotChatWidthProvider, {
                                                        children: /*#__PURE__*/ jsx(AutopilotChatDropzone, {
                                                            children: /*#__PURE__*/ jsx(AutopilotChatContent, {})
                                                        })
                                                    })
                                                })
                                            })
                                        })
                                    })
                                })
                            })
                        })
                    })
                })
            })
        })
    });
    const chatContent = enableInternalThemeProvider && muiTheme ? /*#__PURE__*/ jsx(ThemeProvider, {
        theme: muiTheme,
        children: chatProviders
    }) : chatProviders;
    if (embeddedContainer && !disableEmbeddedPortal) return /*#__PURE__*/ createPortal(chatContent, embeddedContainer);
    return chatContent;
}
export { ApChat };
