"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChatContainer = ChatContainer;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const cn_1 = require("../../../utils/cn.js");
const Disclaimer_1 = require("../../atoms/Disclaimer/index.js");
const Header_1 = require("../../organisms/Header/index.js");
const PromptInput_1 = require("../../organisms/PromptInput/index.js");
const ChatContent_1 = require("../../templates/ChatContent/index.js");
const History_1 = require("../../templates/History/index.js");
const chatContainerQa_1 = require("./chatContainerQa.js");
const i18n_1 = require("./i18n/index.js");
const useChatContainer_1 = require("./useChatContainer.js");
require("./ChatContainer.css");
const b = (0, cn_1.block)('chat-container');
function mergePromptInputSuggestionsProps(fromProps, textsSuggestTitle) {
    const hasSuggestionsFromProps = Boolean(fromProps);
    const hasSuggestTitleFromTexts = textsSuggestTitle !== undefined;
    if (!hasSuggestionsFromProps && !hasSuggestTitleFromTexts) {
        return undefined;
    }
    return Object.assign(Object.assign({}, fromProps), { suggestTitle: textsSuggestTitle !== null && textsSuggestTitle !== void 0 ? textsSuggestTitle : fromProps === null || fromProps === void 0 ? void 0 : fromProps.suggestTitle });
}
function buildFinalPromptInputProps(args) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
    const { promptInputProps, onSendMessage, onCancel, status, contextItems, showContextIndicator, contextIndicatorProps, texts: textsArg, promptInputKey, qaMap, } = args;
    const texts = textsArg !== null && textsArg !== void 0 ? textsArg : {};
    const _t = (_a = promptInputProps === null || promptInputProps === void 0 ? void 0 : promptInputProps.bodyProps) !== null && _a !== void 0 ? _a : {}, { autoFocusOnNewChat: _autoFocusOnNewChat, autoFocusOnChatSelect: _autoFocusOnChatSelect } = _t, restBodyProps = tslib_1.__rest(_t, ["autoFocusOnNewChat", "autoFocusOnChatSelect"]);
    const suggestionsProps = mergePromptInputSuggestionsProps(promptInputProps === null || promptInputProps === void 0 ? void 0 : promptInputProps.suggestionsProps, texts.promptSuggestTitle);
    return Object.assign(Object.assign({}, promptInputProps), { qa: (_b = (0, chatContainerQa_1.resolveChatContainerQa)(qaMap, 'promptInput', 'prompt-input')) !== null && _b !== void 0 ? _b : promptInputProps === null || promptInputProps === void 0 ? void 0 : promptInputProps.qa, onSend: onSendMessage, onCancel,
        status,
        suggestionsProps, headerProps: Object.assign(Object.assign({}, promptInputProps === null || promptInputProps === void 0 ? void 0 : promptInputProps.headerProps), { contextItems, showContextIndicator: showContextIndicator !== null && showContextIndicator !== void 0 ? showContextIndicator : (_c = promptInputProps === null || promptInputProps === void 0 ? void 0 : promptInputProps.headerProps) === null || _c === void 0 ? void 0 : _c.showContextIndicator, contextIndicatorProps: contextIndicatorProps !== null && contextIndicatorProps !== void 0 ? contextIndicatorProps : (_d = promptInputProps === null || promptInputProps === void 0 ? void 0 : promptInputProps.headerProps) === null || _d === void 0 ? void 0 : _d.contextIndicatorProps, qa: (_e = (0, chatContainerQa_1.resolveChatContainerQa)(qaMap, 'promptInputHeader', 'prompt-input-header')) !== null && _e !== void 0 ? _e : (_f = promptInputProps === null || promptInputProps === void 0 ? void 0 : promptInputProps.headerProps) === null || _f === void 0 ? void 0 : _f.qa }), bodyProps: Object.assign(Object.assign({}, restBodyProps), { placeholder: texts.promptPlaceholder || (restBodyProps === null || restBodyProps === void 0 ? void 0 : restBodyProps.placeholder) || (0, i18n_1.i18n)('prompt-placeholder'), autoFocus: promptInputKey > 0 || (restBodyProps === null || restBodyProps === void 0 ? void 0 : restBodyProps.autoFocus), qa: (_g = (0, chatContainerQa_1.resolveChatContainerQa)(qaMap, 'promptInputBody', 'prompt-input-body')) !== null && _g !== void 0 ? _g : restBodyProps === null || restBodyProps === void 0 ? void 0 : restBodyProps.qa }), footerProps: Object.assign(Object.assign({}, promptInputProps === null || promptInputProps === void 0 ? void 0 : promptInputProps.footerProps), { submitButtonTooltipSend: (_h = texts === null || texts === void 0 ? void 0 : texts.submitSendTooltip) !== null && _h !== void 0 ? _h : (_j = promptInputProps === null || promptInputProps === void 0 ? void 0 : promptInputProps.footerProps) === null || _j === void 0 ? void 0 : _j.submitButtonTooltipSend, submitButtonTooltipCancel: (_k = texts === null || texts === void 0 ? void 0 : texts.submitCancelTooltip) !== null && _k !== void 0 ? _k : (_l = promptInputProps === null || promptInputProps === void 0 ? void 0 : promptInputProps.footerProps) === null || _l === void 0 ? void 0 : _l.submitButtonTooltipCancel, submitButtonCancelableText: (_m = texts === null || texts === void 0 ? void 0 : texts.submitButtonCancelableText) !== null && _m !== void 0 ? _m : (_o = promptInputProps === null || promptInputProps === void 0 ? void 0 : promptInputProps.footerProps) === null || _o === void 0 ? void 0 : _o.submitButtonCancelableText, qa: (_p = (0, chatContainerQa_1.resolveChatContainerQa)(qaMap, 'promptInputFooter', 'prompt-input-footer')) !== null && _p !== void 0 ? _p : (_q = promptInputProps === null || promptInputProps === void 0 ? void 0 : promptInputProps.footerProps) === null || _q === void 0 ? void 0 : _q.qa, submitButtonQa: (_r = (0, chatContainerQa_1.resolveChatContainerQa)(qaMap, 'submitButton', 'submit-button')) !== null && _r !== void 0 ? _r : (_s = promptInputProps === null || promptInputProps === void 0 ? void 0 : promptInputProps.footerProps) === null || _s === void 0 ? void 0 : _s.submitButtonQa }) });
}
/**
 * ChatContainer - fully assembled chat component, the main exportable component of the library.
 * Integrates Header, ChatContent, History and manages their state.
 *
 * @param props - component props
 * @returns React component
 */
function ChatContainer(props) {
    var _a;
    const { chats = [], messages = [], onSendMessage, onDeleteChat, onCancel, onRetry, status = 'ready', error = null, showActionsOnHover = false, contextItems = [], showContextIndicator, contextIndicatorProps, transformOptions, shouldParseIncompleteMarkdown, messageListConfig, headerProps = {}, contentProps = {}, emptyContainerProps = {}, promptInputProps = {}, disclaimerProps = {}, historyProps = {}, welcomeConfig, texts = {}, hideTitleOnEmptyChat = false, className, headerClassName, contentClassName, footerClassName, qa, } = props;
    const hookState = (0, useChatContainer_1.useChatContainer)(props);
    const qaMap = (0, react_1.useMemo)(() => (0, chatContainerQa_1.normalizeChatContainerQa)(qa), [qa]);
    // Collect i18n texts with overrides
    const headerTitle = (0, react_1.useMemo)(() => {
        var _a;
        return texts.headerTitle ||
            headerProps.title ||
            ((_a = hookState.activeChat) === null || _a === void 0 ? void 0 : _a.name) ||
            (0, i18n_1.i18n)('header-default-title');
    }, [texts.headerTitle, headerProps.title, (_a = hookState.activeChat) === null || _a === void 0 ? void 0 : _a.name]);
    // Determine if chat is empty
    const isChatEmpty = hookState.chatContentView === 'empty';
    // Calculate showTitle based on hideTitleOnEmptyChat option
    const showTitle = (0, react_1.useMemo)(() => {
        // If explicit showTitle is provided in headerProps, use it
        if (headerProps.showTitle !== undefined) {
            return headerProps.showTitle;
        }
        // If hideTitleOnEmptyChat is enabled, show title only when chat has messages
        if (hideTitleOnEmptyChat && isChatEmpty) {
            return false;
        }
        return true;
    }, [hideTitleOnEmptyChat, isChatEmpty, headerProps.showTitle]);
    // Build props for Header
    const finalHeaderProps = (0, react_1.useMemo)(() => {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
        const actionQa = Object.assign({}, headerProps.actionQa);
        const setHeaderActionQa = (action, key, suffix) => {
            const resolved = (0, chatContainerQa_1.resolveChatContainerQa)(qaMap, key, suffix);
            if (resolved !== undefined) {
                actionQa[action] = resolved;
            }
        };
        setHeaderActionQa(Header_1.HeaderAction.NewChat, 'headerNewChat', 'header-action-newChat');
        setHeaderActionQa(Header_1.HeaderAction.History, 'headerHistory', 'header-action-history');
        setHeaderActionQa(Header_1.HeaderAction.Folding, 'headerFolding', 'header-action-folding');
        setHeaderActionQa(Header_1.HeaderAction.Close, 'headerClose', 'header-action-close');
        const actionTooltipTexts = {
            [Header_1.HeaderAction.NewChat]: (_a = texts.headerNewChatTooltip) !== null && _a !== void 0 ? _a : (_b = headerProps.actionTooltipTexts) === null || _b === void 0 ? void 0 : _b[Header_1.HeaderAction.NewChat],
            [Header_1.HeaderAction.History]: (_c = texts.headerHistoryTooltip) !== null && _c !== void 0 ? _c : (_d = headerProps.actionTooltipTexts) === null || _d === void 0 ? void 0 : _d[Header_1.HeaderAction.History],
            [Header_1.HeaderAction.Close]: (_e = texts.headerCloseTooltip) !== null && _e !== void 0 ? _e : (_f = headerProps.actionTooltipTexts) === null || _f === void 0 ? void 0 : _f[Header_1.HeaderAction.Close],
            [Header_1.HeaderAction.Folding]: {
                collapsed: (_g = texts.headerFoldingCollapsedTooltip) !== null && _g !== void 0 ? _g : (_j = (_h = headerProps.actionTooltipTexts) === null || _h === void 0 ? void 0 : _h[Header_1.HeaderAction.Folding]) === null || _j === void 0 ? void 0 : _j.collapsed,
                opened: (_k = texts.headerFoldingOpenedTooltip) !== null && _k !== void 0 ? _k : (_m = (_l = headerProps.actionTooltipTexts) === null || _l === void 0 ? void 0 : _l[Header_1.HeaderAction.Folding]) === null || _m === void 0 ? void 0 : _m.opened,
            },
        };
        return Object.assign(Object.assign({}, headerProps), { title: headerTitle, showTitle, baseActions: hookState.baseActions, handleNewChat: hookState.handleNewChat, handleHistoryToggle: hookState.handleHistoryToggle, handleFolding: hookState.handleFolding, handleClose: hookState.handleClose, historyButtonRef: hookState.historyButtonRef, qa: (_o = (0, chatContainerQa_1.resolveChatContainerQa)(qaMap, 'header', 'header')) !== null && _o !== void 0 ? _o : headerProps.qa, actionQa,
            actionTooltipTexts });
    }, [
        headerTitle,
        showTitle,
        hookState.baseActions,
        hookState.handleNewChat,
        hookState.handleHistoryToggle,
        hookState.handleFolding,
        hookState.handleClose,
        hookState.historyButtonRef,
        headerProps,
        qaMap,
        texts.headerNewChatTooltip,
        texts.headerHistoryTooltip,
        texts.headerCloseTooltip,
        texts.headerFoldingCollapsedTooltip,
        texts.headerFoldingOpenedTooltip,
    ]);
    // Build props for EmptyContainer
    const finalEmptyContainerProps = (0, react_1.useMemo)(() => {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
        const { showDefaultTitle = true, showDefaultDescription = true } = welcomeConfig || {};
        return Object.assign(Object.assign({}, emptyContainerProps), { qa: (_a = (0, chatContainerQa_1.resolveChatContainerQa)(qaMap, 'emptyState', 'empty-state')) !== null && _a !== void 0 ? _a : emptyContainerProps.qa, image: welcomeConfig === null || welcomeConfig === void 0 ? void 0 : welcomeConfig.image, title: (_d = (_c = (_b = texts.emptyStateTitle) !== null && _b !== void 0 ? _b : welcomeConfig === null || welcomeConfig === void 0 ? void 0 : welcomeConfig.title) !== null && _c !== void 0 ? _c : emptyContainerProps.title) !== null && _d !== void 0 ? _d : (showDefaultTitle ? (0, i18n_1.i18n)('empty-state-title') : undefined), description: (_g = (_f = (_e = texts.emptyStateDescription) !== null && _e !== void 0 ? _e : welcomeConfig === null || welcomeConfig === void 0 ? void 0 : welcomeConfig.description) !== null && _f !== void 0 ? _f : emptyContainerProps.description) !== null && _g !== void 0 ? _g : (showDefaultDescription ? (0, i18n_1.i18n)('empty-state-description') : undefined), suggestionTitle: (_j = (_h = texts.emptyStateSuggestionsTitle) !== null && _h !== void 0 ? _h : welcomeConfig === null || welcomeConfig === void 0 ? void 0 : welcomeConfig.suggestionTitle) !== null && _j !== void 0 ? _j : emptyContainerProps.suggestionTitle, suggestions: welcomeConfig === null || welcomeConfig === void 0 ? void 0 : welcomeConfig.suggestions, alignment: welcomeConfig === null || welcomeConfig === void 0 ? void 0 : welcomeConfig.alignment, layout: welcomeConfig === null || welcomeConfig === void 0 ? void 0 : welcomeConfig.layout, wrapText: welcomeConfig === null || welcomeConfig === void 0 ? void 0 : welcomeConfig.wrapText, showMore: welcomeConfig === null || welcomeConfig === void 0 ? void 0 : welcomeConfig.showMore, showMoreText: (_m = (_l = (_k = texts.emptyStateShowMoreText) !== null && _k !== void 0 ? _k : welcomeConfig === null || welcomeConfig === void 0 ? void 0 : welcomeConfig.showMoreText) !== null && _l !== void 0 ? _l : emptyContainerProps.showMoreText) !== null && _m !== void 0 ? _m : (0, i18n_1.i18n)('empty-state-show-more'), onSuggestionClick: async (clickedTitle) => {
                await onSendMessage({ content: clickedTitle });
            } });
    }, [
        welcomeConfig,
        emptyContainerProps,
        onSendMessage,
        qaMap,
        texts.emptyStateTitle,
        texts.emptyStateDescription,
        texts.emptyStateSuggestionsTitle,
        texts.emptyStateShowMoreText,
    ]);
    // Build props for MessageList
    const messageListProps = (0, react_1.useMemo)(() => {
        var _a, _b, _c, _d;
        return (Object.assign(Object.assign({}, messageListConfig), { messages,
            status, errorMessage: texts.errorText
                ? Object.assign(Object.assign({}, ((_a = messageListConfig === null || messageListConfig === void 0 ? void 0 : messageListConfig.errorMessage) !== null && _a !== void 0 ? _a : {})), { text: texts.errorText }) : (messageListConfig === null || messageListConfig === void 0 ? void 0 : messageListConfig.errorMessage) || (error ? { text: error.message } : undefined), onRetry,
            showActionsOnHover,
            transformOptions,
            shouldParseIncompleteMarkdown, qa: (_b = (0, chatContainerQa_1.resolveChatContainerQa)(qaMap, 'messageList', 'message-list')) !== null && _b !== void 0 ? _b : messageListConfig === null || messageListConfig === void 0 ? void 0 : messageListConfig.qa, actionPopupProps: Object.assign(Object.assign({}, messageListConfig === null || messageListConfig === void 0 ? void 0 : messageListConfig.actionPopupProps), { qa: (_c = (0, chatContainerQa_1.resolveChatContainerQa)(qaMap, 'actionPopup', 'action-popup')) !== null && _c !== void 0 ? _c : (_d = messageListConfig === null || messageListConfig === void 0 ? void 0 : messageListConfig.actionPopupProps) === null || _d === void 0 ? void 0 : _d.qa }) }));
    }, [
        messages,
        status,
        error,
        onRetry,
        showActionsOnHover,
        transformOptions,
        shouldParseIncompleteMarkdown,
        messageListConfig,
        qaMap,
        texts.errorText,
    ]);
    // Build props for PromptInput
    const finalPromptInputProps = (0, react_1.useMemo)(() => buildFinalPromptInputProps({
        promptInputProps,
        onSendMessage,
        onCancel,
        status,
        contextItems,
        showContextIndicator,
        contextIndicatorProps,
        texts,
        promptInputKey: hookState.promptInputKey,
        qaMap,
    }), [
        onSendMessage,
        onCancel,
        status,
        contextItems,
        showContextIndicator,
        contextIndicatorProps,
        promptInputProps,
        hookState.promptInputKey,
        qaMap,
        texts.promptPlaceholder,
        texts.promptSuggestTitle,
        texts.submitSendTooltip,
        texts.submitCancelTooltip,
        texts.submitButtonCancelableText,
    ]);
    // Build props for Disclaimer
    const finalDisclaimerProps = (0, react_1.useMemo)(() => {
        var _a, _b, _c;
        const disclaimerText = (_b = (_a = texts.disclaimerText) !== null && _a !== void 0 ? _a : disclaimerProps.text) !== null && _b !== void 0 ? _b : (0, i18n_1.i18n)('disclaimer-text');
        return Object.assign(Object.assign({}, disclaimerProps), { text: disclaimerText, qa: (_c = (0, chatContainerQa_1.resolveChatContainerQa)(qaMap, 'disclaimer', 'disclaimer')) !== null && _c !== void 0 ? _c : disclaimerProps.qa });
    }, [disclaimerProps, qaMap, texts.disclaimerText]);
    // Build props for ChatContent
    const finalContentProps = (0, react_1.useMemo)(() => {
        var _a;
        return (Object.assign(Object.assign({}, contentProps), { qa: (_a = (0, chatContainerQa_1.resolveChatContainerQa)(qaMap, 'content', 'content')) !== null && _a !== void 0 ? _a : contentProps.qa, view: hookState.chatContentView, emptyContainerProps: finalEmptyContainerProps, messageListProps }));
    }, [
        hookState.chatContentView,
        finalEmptyContainerProps,
        messageListProps,
        contentProps,
        qaMap,
    ]);
    // Build props for History
    const finalHistoryProps = (0, react_1.useMemo)(() => {
        var _a, _b, _c, _d, _e, _f;
        return (Object.assign(Object.assign({}, historyProps), { qa: (_a = (0, chatContainerQa_1.resolveChatContainerQa)(qaMap, 'history', 'history')) !== null && _a !== void 0 ? _a : historyProps.qa, chats, selectedChat: hookState.activeChat, onSelectChat: hookState.handleSelectChat, onDeleteChat, open: hookState.isHistoryOpen, onOpenChange: (open) => {
                var _a;
                (_a = historyProps.onOpenChange) === null || _a === void 0 ? void 0 : _a.call(historyProps, open);
                hookState.handleHistoryOpenChange(open);
            }, anchorElement: hookState.historyButtonRef.current, emptyPlaceholder: (_c = (_b = texts.historyEmptyPlaceholder) !== null && _b !== void 0 ? _b : historyProps.emptyPlaceholder) !== null && _c !== void 0 ? _c : (0, i18n_1.i18n)('history-empty'), emptyFilteredPlaceholder: (_e = (_d = texts.historyEmptyFilteredPlaceholder) !== null && _d !== void 0 ? _d : historyProps.emptyFilteredPlaceholder) !== null && _e !== void 0 ? _e : (0, i18n_1.i18n)('history-empty-filtered'), searchPlaceholder: (_f = texts.historySearchPlaceholder) !== null && _f !== void 0 ? _f : historyProps.searchPlaceholder }));
    }, [
        chats,
        hookState.activeChat,
        hookState.handleSelectChat,
        onDeleteChat,
        hookState.isHistoryOpen,
        hookState.handleHistoryOpenChange,
        hookState.historyButtonRef,
        historyProps,
        qaMap,
        texts.historyEmptyPlaceholder,
        texts.historyEmptyFilteredPlaceholder,
        texts.historySearchPlaceholder,
    ]);
    const showFooter = finalPromptInputProps || finalDisclaimerProps;
    return ((0, jsx_runtime_1.jsxs)("div", { className: b(null, className), "data-qa": (0, chatContainerQa_1.resolveChatContainerRootQa)(qaMap), children: [(0, jsx_runtime_1.jsx)("div", { className: b('header', headerClassName), children: (0, jsx_runtime_1.jsx)(Header_1.Header, Object.assign({}, finalHeaderProps)) }), (0, jsx_runtime_1.jsx)("div", { className: b('content', { view: hookState.chatContentView }, contentClassName), children: (0, jsx_runtime_1.jsx)(ChatContent_1.ChatContent, Object.assign({}, finalContentProps)) }), showFooter && ((0, jsx_runtime_1.jsxs)("div", { className: b('footer', { view: hookState.chatContentView }, footerClassName), children: [finalPromptInputProps && ((0, jsx_runtime_1.jsx)(PromptInput_1.PromptInput, Object.assign({}, finalPromptInputProps), hookState.promptInputKey)), finalDisclaimerProps && (0, jsx_runtime_1.jsx)(Disclaimer_1.Disclaimer, Object.assign({}, finalDisclaimerProps))] })), (0, jsx_runtime_1.jsx)(History_1.History, Object.assign({}, finalHistoryProps))] }));
}
//# sourceMappingURL=ChatContainer.js.map
