"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useChatContainer = useChatContainer;
const react_1 = require("react");
const Header_1 = require("../../organisms/Header/index.js");
/**
 * Hook for managing ChatContainer state
 *
 * @param props - ChatContainer component props
 * @returns object with state and handlers
 */
function useChatContainer(props) {
    var _a, _b, _c, _d;
    const { messages = [], activeChat, onCreateChat, onClose, onFold, onSelectChat, showHistory = true, showNewChat = true, showFolding = false, showClose = false, promptInputProps, headerProps, } = props;
    const onHistoryToggle = headerProps === null || headerProps === void 0 ? void 0 : headerProps.onHistoryToggle;
    const autoFocusOnNewChat = (_b = (_a = promptInputProps === null || promptInputProps === void 0 ? void 0 : promptInputProps.bodyProps) === null || _a === void 0 ? void 0 : _a.autoFocusOnNewChat) !== null && _b !== void 0 ? _b : true;
    const autoFocusOnChatSelect = (_d = (_c = promptInputProps === null || promptInputProps === void 0 ? void 0 : promptInputProps.bodyProps) === null || _c === void 0 ? void 0 : _c.autoFocusOnChatSelect) !== null && _d !== void 0 ? _d : true;
    // Refs for History integration with Header
    const historyButtonRef = (0, react_1.useRef)(null);
    const [isHistoryOpen, setIsHistoryOpenState] = (0, react_1.useState)(false);
    // Mirror of `isHistoryOpen` for synchronous reads between renders (e.g. back-to-back toggle clicks)
    const isHistoryOpenRef = (0, react_1.useRef)(false);
    const setIsHistoryOpen = (0, react_1.useCallback)((open) => {
        isHistoryOpenRef.current = open;
        setIsHistoryOpenState(open);
    }, []);
    // Key incremented on each new chat to remount PromptInput with autofocus
    const [promptInputKey, setPromptInputKey] = (0, react_1.useState)(0);
    // Determine view for ChatContent (empty or chat)
    const chatContentView = (0, react_1.useMemo)(() => {
        // If there is an active chat (selected from history), show chat view
        // even if there are no messages yet
        if (activeChat) {
            return 'chat';
        }
        // If no active chat, determine by the presence of messages
        return messages.length === 0 ? 'empty' : 'chat';
    }, [messages.length, activeChat]);
    // Handler for creating new chat
    const handleNewChat = (0, react_1.useCallback)(() => {
        onCreateChat === null || onCreateChat === void 0 ? void 0 : onCreateChat();
        if (autoFocusOnNewChat) {
            setPromptInputKey((prev) => prev + 1);
        }
    }, [onCreateChat, autoFocusOnNewChat]);
    // Handler for toggling history
    const handleHistoryToggle = (0, react_1.useCallback)(() => {
        const next = !isHistoryOpenRef.current;
        setIsHistoryOpen(next);
        onHistoryToggle === null || onHistoryToggle === void 0 ? void 0 : onHistoryToggle(next);
    }, [setIsHistoryOpen, onHistoryToggle]);
    const handleFolding = (0, react_1.useCallback)((value) => {
        onFold === null || onFold === void 0 ? void 0 : onFold(value);
    }, [onFold]);
    // Handler for closing
    const handleClose = (0, react_1.useCallback)(() => {
        onClose === null || onClose === void 0 ? void 0 : onClose();
    }, [onClose]);
    // Handler for selecting chat from history
    const handleSelectChat = (0, react_1.useCallback)((chat) => {
        if (chat) {
            onSelectChat === null || onSelectChat === void 0 ? void 0 : onSelectChat(chat);
            setIsHistoryOpen(false);
            if (autoFocusOnChatSelect) {
                setPromptInputKey((prev) => prev + 1);
            }
        }
    }, [onSelectChat, autoFocusOnChatSelect]);
    // Handler for history popup state changes
    const handleHistoryOpenChange = (0, react_1.useCallback)((open) => {
        setIsHistoryOpen(open);
    }, []);
    // Build baseActions for Header
    const baseActions = (0, react_1.useMemo)(() => {
        const actions = [];
        if (showNewChat && chatContentView !== 'empty') {
            actions.push(Header_1.HeaderAction.NewChat);
        }
        if (showHistory) {
            actions.push(Header_1.HeaderAction.History);
        }
        if (showFolding) {
            actions.push(Header_1.HeaderAction.Folding);
        }
        if (showClose) {
            actions.push(Header_1.HeaderAction.Close);
        }
        return actions;
    }, [showNewChat, showHistory, showFolding, showClose, chatContentView]);
    return {
        // State
        chatContentView,
        isHistoryOpen,
        activeChat,
        promptInputKey,
        // Refs
        historyButtonRef,
        // Handlers
        handleNewChat,
        handleHistoryToggle,
        handleFolding,
        handleClose,
        handleSelectChat,
        handleHistoryOpenChange,
        // Configuration
        baseActions,
    };
}
//# sourceMappingURL=useChatContainer.js.map
