import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import React from 'react';
import { Box, Static, Text } from 'ink';
import { ApprovalMode } from '@vybestack/llxprt-code-core';
import { StreamingState } from '../types.js';
import { useUIState } from '../contexts/UIStateContext.js';
import { useUIActions } from '../contexts/UIActionsContext.js';
import { StreamingContext } from '../contexts/StreamingContext.js';
import { OverflowProvider } from '../contexts/OverflowContext.js';
import { Colors } from '../colors.js';
// Components
import { Header } from '../components/Header.js';
import { Tips } from '../components/Tips.js';
import { HistoryItemDisplay } from '../components/HistoryItemDisplay.js';
import { ShowMoreLines } from '../components/ShowMoreLines.js';
import { Notifications } from '../components/Notifications.js';
import { TodoPanel } from '../components/TodoPanel.js';
import { Footer } from '../components/Footer.js';
import { DialogManager } from '../components/DialogManager.js';
import { BucketAuthConfirmation } from '../components/BucketAuthConfirmation.js';
import { Composer } from '../components/Composer.js';
import { LoadingIndicator } from '../components/LoadingIndicator.js';
import { AutoAcceptIndicator } from '../components/AutoAcceptIndicator.js';
import { ShellModeIndicator } from '../components/ShellModeIndicator.js';
import { ContextSummaryDisplay } from '../components/ContextSummaryDisplay.js';
import { DetailedMessagesDisplay } from '../components/DetailedMessagesDisplay.js';
import { ScrollableList } from '../components/shared/ScrollableList.js';
import { SCROLL_TO_ITEM_END } from '../components/shared/VirtualizedList.js';
function renderScrollableMainContentItem({ item, }) {
    return item.element;
}
function keyExtractorScrollableMainContentItem(item) {
    return item.key;
}
function estimateScrollableMainContentItemHeight(_index) {
    return 100;
}
export const DefaultAppLayout = ({ config, settings, startupWarnings, version, nightly, mainControlsRef, availableTerminalHeight, contextFileNames, updateInfo, }) => {
    const uiState = useUIState();
    const uiActions = useUIActions();
    const [, setSuggestionsVisible] = React.useState(false);
    const { terminalWidth, terminalHeight, mainAreaWidth, inputWidth, history, pendingHistoryItems, streamingState, quittingMessages, constrainHeight, showErrorDetails, showToolDescriptions, consoleMessages, slashCommands, staticKey, isInputActive, ctrlCPressedOnce, ctrlDPressedOnce, showEscapePrompt, ideContextState, llxprtMdFileCount, elapsedTime, currentLoadingPhrase, showAutoAcceptIndicator, shellModeActive, thought, branchName, debugMessage, errorCount, historyTokenCount, vimModeEnabled, vimMode, tokenMetrics, currentModel, availableTerminalHeight: uiAvailableTerminalHeight, } = uiState;
    // Use the UI state's availableTerminalHeight if constrainHeight is true
    // Otherwise, fall back to the prop (which is the same calculation)
    const effectiveAvailableHeight = constrainHeight
        ? uiAvailableTerminalHeight
        : availableTerminalHeight;
    const showTodoPanelSetting = settings.merged.ui?.showTodoPanel ?? true;
    const hideContextSummary = settings.merged.ui?.hideContextSummary ?? false;
    const { isNarrow } = uiState;
    const useAlternateBuffer = settings.merged.ui?.useAlternateBuffer === true &&
        !config.getScreenReader();
    const debugConsoleMaxHeight = Math.floor(Math.max(terminalHeight * 0.2, 5));
    const staticAreaMaxItemHeight = Math.max(terminalHeight * 4, 100);
    // Check if any dialog is visible
    const dialogsVisible = uiState.showWorkspaceMigrationDialog ||
        uiState.shouldShowIdePrompt ||
        uiState.showIdeRestartPrompt ||
        uiState.isFolderTrustDialogOpen ||
        uiState.isPermissionsDialogOpen ||
        uiState.shellConfirmationRequest ||
        uiState.confirmationRequest ||
        uiState.isThemeDialogOpen ||
        uiState.isSettingsDialogOpen ||
        uiState.isAuthenticating ||
        uiState.isAuthDialogOpen ||
        uiState.isOAuthCodeDialogOpen ||
        uiState.isEditorDialogOpen ||
        uiState.isProviderDialogOpen ||
        uiState.isProviderModelDialogOpen ||
        uiState.isLoadProfileDialogOpen ||
        uiState.isToolsDialogOpen ||
        uiState.showPrivacyNotice;
    if (quittingMessages) {
        return (_jsx(Box, { flexDirection: "column", marginBottom: 1, children: quittingMessages.map((item) => (_jsx(HistoryItemDisplay, { availableTerminalHeight: constrainHeight ? effectiveAvailableHeight : undefined, terminalWidth: terminalWidth, item: item, isPending: false, config: config, slashCommands: slashCommands, showTodoPanel: showTodoPanelSetting }, item.id))) }));
    }
    if (useAlternateBuffer) {
        const headerElement = (_jsxs(_Fragment, { children: [!(settings.merged.ui?.hideBanner || config.getScreenReader()) && (_jsx(Header, { terminalWidth: terminalWidth, version: version, nightly: nightly })), !(settings.merged.ui?.hideTips || config.getScreenReader()) && (_jsx(Tips, { config: config }))] }));
        const pendingElement = (_jsx(OverflowProvider, { children: _jsxs(Box, { ref: uiState.pendingHistoryItemRef, flexDirection: "column", children: [pendingHistoryItems.map((item, i) => (_jsx(HistoryItemDisplay, { availableTerminalHeight: constrainHeight ? effectiveAvailableHeight : undefined, terminalWidth: mainAreaWidth, item: { ...item, id: 0 }, isPending: true, config: config, isFocused: !uiState.isEditorDialogOpen, slashCommands: slashCommands, showTodoPanel: showTodoPanelSetting }, i))), _jsx(ShowMoreLines, { constrainHeight: constrainHeight })] }) }));
        const listItems = [
            {
                key: 'header',
                estimatedHeight: 100,
                element: _jsx(Box, { flexDirection: "column", children: headerElement }),
            },
            ...history.map((h) => ({
                key: `history-${h.id}`,
                estimatedHeight: 100,
                element: (_jsx(HistoryItemDisplay, { terminalWidth: mainAreaWidth, availableTerminalHeight: staticAreaMaxItemHeight, item: h, isPending: false, config: config, slashCommands: slashCommands, showTodoPanel: showTodoPanelSetting })),
            })),
            {
                key: 'pending',
                estimatedHeight: 100,
                element: pendingElement,
            },
        ];
        return (_jsx(StreamingContext.Provider, { value: streamingState, children: _jsxs(Box, { flexDirection: "column", width: terminalWidth, height: terminalHeight, flexShrink: 0, flexGrow: 0, overflow: "hidden", ref: uiState.rootUiRef, children: [_jsx(ScrollableList, { hasFocus: !dialogsVisible, data: listItems, renderItem: renderScrollableMainContentItem, keyExtractor: keyExtractorScrollableMainContentItem, estimatedItemHeight: estimateScrollableMainContentItemHeight, initialScrollIndex: SCROLL_TO_ITEM_END, initialScrollOffsetInIndex: SCROLL_TO_ITEM_END }), _jsxs(Box, { flexDirection: "column", ref: mainControlsRef, flexShrink: 0, flexGrow: 0, children: [_jsx(Notifications, { startupWarnings: startupWarnings, updateInfo: updateInfo, history: history }), showTodoPanelSetting && _jsx(TodoPanel, { width: inputWidth }), _jsx(BucketAuthConfirmation, { config: config, isFocused: !dialogsVisible }), dialogsVisible ? (_jsx(DialogManager, { config: config, settings: settings, addItem: uiActions.addItem, terminalWidth: terminalWidth })) : (_jsxs(_Fragment, { children: [_jsx(LoadingIndicator, { thought: streamingState === StreamingState.WaitingForConfirmation ||
                                            config.getAccessibility()?.disableLoadingPhrases ||
                                            config.getScreenReader()
                                            ? undefined
                                            : thought, currentLoadingPhrase: config.getAccessibility()?.disableLoadingPhrases ||
                                            config.getScreenReader()
                                            ? undefined
                                            : currentLoadingPhrase, elapsedTime: elapsedTime }), _jsxs(Box, { marginTop: 1, display: "flex", justifyContent: hideContextSummary ? 'flex-start' : 'space-between', width: "100%", children: [_jsxs(Box, { children: [process.env.GEMINI_SYSTEM_MD && (_jsxs(Text, { color: Colors.AccentRed, children: ["|\u2310\u25A0_\u25A0|", ' '] })), ctrlCPressedOnce ? (_jsx(Text, { color: Colors.AccentYellow, children: "Press Ctrl+C again to exit." })) : ctrlDPressedOnce ? (_jsx(Text, { color: Colors.AccentYellow, children: "Press Ctrl+D again to exit." })) : showEscapePrompt ? (_jsx(Text, { color: Colors.Gray, children: "Press Esc again to clear." })) : !hideContextSummary ? (_jsx(ContextSummaryDisplay, { ideContext: ideContextState, llxprtMdFileCount: llxprtMdFileCount, contextFileNames: contextFileNames, mcpServers: config.getMcpServers(), blockedMcpServers: config.getBlockedMcpServers(), showToolDescriptions: showToolDescriptions })) : null] }), _jsxs(Box, { paddingTop: isNarrow ? 1 : 0, marginLeft: hideContextSummary ? 1 : 2, children: [showAutoAcceptIndicator !== ApprovalMode.DEFAULT &&
                                                        !shellModeActive && (_jsx(AutoAcceptIndicator, { approvalMode: showAutoAcceptIndicator })), shellModeActive && _jsx(ShellModeIndicator, {})] })] }), showErrorDetails && (_jsx(OverflowProvider, { children: _jsxs(Box, { flexDirection: "column", children: [_jsx(DetailedMessagesDisplay, { messages: consoleMessages, maxHeight: constrainHeight ? debugConsoleMaxHeight : undefined, width: inputWidth }), _jsx(ShowMoreLines, { constrainHeight: constrainHeight })] }) })), isInputActive && (_jsx(Composer, { config: config, settings: settings, onSuggestionsVisibilityChange: setSuggestionsVisible }))] })), !settings.merged.ui?.hideFooter && (_jsx(Footer, { model: currentModel, targetDir: config.getTargetDir(), debugMode: config.getDebugMode(), branchName: branchName, debugMessage: debugMessage, errorCount: errorCount, showErrorDetails: showErrorDetails, showMemoryUsage: config.getDebugMode() ||
                                    settings.merged.ui?.showMemoryUsage ||
                                    false, historyTokenCount: historyTokenCount, nightly: nightly, vimMode: vimModeEnabled ? vimMode : undefined, contextLimit: config.getEphemeralSetting('context-limit'), isTrustedFolder: config.isTrustedFolder(), tokensPerMinute: tokenMetrics.tokensPerMinute, throttleWaitTimeMs: tokenMetrics.throttleWaitTimeMs, sessionTokenTotal: tokenMetrics.sessionTokenTotal, hideCWD: settings.merged.hideCWD, hideSandboxStatus: settings.merged.hideSandboxStatus, hideModelInfo: settings.merged.hideModelInfo }))] })] }) }));
    }
    return (_jsx(StreamingContext.Provider, { value: streamingState, children: _jsxs(Box, { flexDirection: "column", width: "90%", ref: uiState.rootUiRef, children: [_jsx(Static, { items: [
                        _jsxs(Box, { flexDirection: "column", children: [!(settings.merged.ui?.hideBanner || config.getScreenReader()) && (_jsx(Header, { terminalWidth: terminalWidth, version: version, nightly: nightly })), !(settings.merged.ui?.hideTips || config.getScreenReader()) && (_jsx(Tips, { config: config }))] }, "header"),
                        ...history.map((h) => (_jsx(HistoryItemDisplay, { terminalWidth: mainAreaWidth, availableTerminalHeight: staticAreaMaxItemHeight, item: h, isPending: false, config: config, slashCommands: slashCommands, showTodoPanel: showTodoPanelSetting }, h.id))),
                    ], children: (item) => item }, staticKey), _jsx(OverflowProvider, { children: _jsxs(Box, { ref: uiState.pendingHistoryItemRef, flexDirection: "column", children: [pendingHistoryItems.map((item, i) => (_jsx(HistoryItemDisplay, { availableTerminalHeight: constrainHeight ? effectiveAvailableHeight : undefined, terminalWidth: mainAreaWidth, item: { ...item, id: 0 }, isPending: true, config: config, isFocused: !uiState.isEditorDialogOpen, slashCommands: slashCommands, showTodoPanel: showTodoPanelSetting }, i))), _jsx(ShowMoreLines, { constrainHeight: constrainHeight })] }) }), _jsxs(Box, { flexDirection: "column", ref: mainControlsRef, children: [_jsx(Notifications, { startupWarnings: startupWarnings, updateInfo: updateInfo, history: history }), showTodoPanelSetting && _jsx(TodoPanel, { width: inputWidth }), _jsx(BucketAuthConfirmation, { config: config, isFocused: !dialogsVisible }), dialogsVisible ? (_jsx(DialogManager, { config: config, settings: settings, addItem: uiActions.addItem, terminalWidth: terminalWidth })) : (_jsxs(_Fragment, { children: [_jsx(LoadingIndicator, { thought: streamingState === StreamingState.WaitingForConfirmation ||
                                        config.getAccessibility()?.disableLoadingPhrases ||
                                        config.getScreenReader()
                                        ? undefined
                                        : thought, currentLoadingPhrase: config.getAccessibility()?.disableLoadingPhrases ||
                                        config.getScreenReader()
                                        ? undefined
                                        : currentLoadingPhrase, elapsedTime: elapsedTime }), _jsxs(Box, { marginTop: 1, display: "flex", justifyContent: hideContextSummary ? 'flex-start' : 'space-between', width: "100%", children: [_jsxs(Box, { children: [process.env.GEMINI_SYSTEM_MD && (_jsxs(Text, { color: Colors.AccentRed, children: ["|\u2310\u25A0_\u25A0|", ' '] })), ctrlCPressedOnce ? (_jsx(Text, { color: Colors.AccentYellow, children: "Press Ctrl+C again to exit." })) : ctrlDPressedOnce ? (_jsx(Text, { color: Colors.AccentYellow, children: "Press Ctrl+D again to exit." })) : showEscapePrompt ? (_jsx(Text, { color: Colors.Gray, children: "Press Esc again to clear." })) : !hideContextSummary ? (_jsx(ContextSummaryDisplay, { ideContext: ideContextState, llxprtMdFileCount: llxprtMdFileCount, contextFileNames: contextFileNames, mcpServers: config.getMcpServers(), blockedMcpServers: config.getBlockedMcpServers(), showToolDescriptions: showToolDescriptions })) : null] }), _jsxs(Box, { paddingTop: isNarrow ? 1 : 0, marginLeft: hideContextSummary ? 1 : 2, children: [showAutoAcceptIndicator !== ApprovalMode.DEFAULT &&
                                                    !shellModeActive && (_jsx(AutoAcceptIndicator, { approvalMode: showAutoAcceptIndicator })), shellModeActive && _jsx(ShellModeIndicator, {})] })] }), showErrorDetails && (_jsx(OverflowProvider, { children: _jsxs(Box, { flexDirection: "column", children: [_jsx(DetailedMessagesDisplay, { messages: consoleMessages, maxHeight: constrainHeight ? debugConsoleMaxHeight : undefined, width: inputWidth }), _jsx(ShowMoreLines, { constrainHeight: constrainHeight })] }) })), isInputActive && (_jsx(Composer, { config: config, settings: settings, onSuggestionsVisibilityChange: setSuggestionsVisible }))] })), !settings.merged.ui?.hideFooter && (_jsx(Footer, { model: currentModel, targetDir: config.getTargetDir(), debugMode: config.getDebugMode(), branchName: branchName, debugMessage: debugMessage, errorCount: errorCount, showErrorDetails: showErrorDetails, showMemoryUsage: config.getDebugMode() ||
                                settings.merged.ui?.showMemoryUsage ||
                                false, historyTokenCount: historyTokenCount, nightly: nightly, vimMode: vimModeEnabled ? vimMode : undefined, contextLimit: config.getEphemeralSetting('context-limit'), isTrustedFolder: config.isTrustedFolder(), tokensPerMinute: tokenMetrics.tokensPerMinute, throttleWaitTimeMs: tokenMetrics.throttleWaitTimeMs, sessionTokenTotal: tokenMetrics.sessionTokenTotal, hideCWD: settings.merged.hideCWD, hideSandboxStatus: settings.merged.hideSandboxStatus, hideModelInfo: settings.merged.hideModelInfo }))] })] }) }));
};
//# sourceMappingURL=DefaultAppLayout.js.map