import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useCallback, useSyncExternalStore } from 'react';
import { Box, Text } from 'ink';
import { theme } from '../semantic-colors.js';
import { ContextUsageDisplay } from './ContextUsageDisplay.js';
import { useTerminalSize } from '../hooks/useTerminalSize.js';
import { AutoAcceptIndicator } from './AutoAcceptIndicator.js';
import { ShellModeIndicator } from './ShellModeIndicator.js';
import { isNarrowWidth } from '../utils/isNarrowWidth.js';
import { useStatusLine } from '../hooks/useStatusLine.js';
import { useUIState } from '../contexts/UIStateContext.js';
import { useConfig } from '../contexts/ConfigContext.js';
import { useVimMode } from '../contexts/VimModeContext.js';
import { ApprovalMode } from '@hoptrendy/hopcode-core';
import { t } from '../../i18n/index.js';
/**
 * Returns true while any dream task for the current project is in
 * 'pending' or 'running' state. Uses MemoryManager's subscribe/notify
 * mechanism so there is zero polling overhead.
 */
function useDreamRunning(projectRoot) {
    const config = useConfig();
    const subscribe = useCallback((onStoreChange) => config.getMemoryManager().subscribe(onStoreChange), [config]);
    const getSnapshot = useCallback(() => config
        .getMemoryManager()
        .listTasksByType('dream', projectRoot)
        .some((task) => task.status === 'pending' || task.status === 'running'), [config, projectRoot]);
    return useSyncExternalStore(subscribe, getSnapshot);
}
export const Footer = () => {
    const uiState = useUIState();
    const config = useConfig();
    const { vimEnabled, vimMode } = useVimMode();
    const { lines: statusLineLines } = useStatusLine();
    const dreamRunning = useDreamRunning(config.getProjectRoot());
    const { promptTokenCount, showAutoAcceptIndicator } = {
        promptTokenCount: uiState.sessionStats.lastPromptTokenCount,
        showAutoAcceptIndicator: uiState.showAutoAcceptIndicator,
    };
    const { columns: terminalWidth } = useTerminalSize();
    const isNarrow = isNarrowWidth(terminalWidth);
    // Determine sandbox info from environment
    const sandboxEnv = process.env['SANDBOX'];
    const sandboxInfo = sandboxEnv
        ? sandboxEnv === 'sandbox-exec'
            ? 'seatbelt'
            : sandboxEnv.startsWith('hopcode')
                ? 'docker'
                : sandboxEnv
        : null;
    // Check if debug mode is enabled
    const debugMode = config.getDebugMode();
    const contextWindowSize = config.getContentGeneratorConfig()?.contextWindowSize;
    // Determine if config is still initializing
    const isInitializing = uiState.isConfigInitialized === false;
    // Hide "? for shortcuts" when a custom status line is active (it already
    // occupies the footer, so the hint is redundant). Matches upstream behavior.
    const suppressHint = isInitializing || statusLineLines.length > 0;
    // Left bottom row: high-priority messages > approval mode > hint.
    const leftBottomContent = isInitializing ? (_jsx(Text, { color: theme.text.secondary, dimColor: true, children: t('Initializing...') })) : uiState.ctrlCPressedOnce ? (_jsx(Text, { color: theme.status.warning, children: t('Press Ctrl+C again to exit.') })) : uiState.ctrlDPressedOnce ? (_jsx(Text, { color: theme.status.warning, children: t('Press Ctrl+D again to exit.') })) : uiState.showEscapePrompt ? (_jsx(Text, { color: theme.text.secondary, children: t('Press Esc again to clear.') })) : uiState.rewindEscPending ? (_jsx(Text, { color: theme.text.secondary, children: t('Press Esc again to rewind conversation.') })) : vimEnabled && vimMode === 'INSERT' ? (_jsx(Text, { color: theme.text.secondary, children: "-- INSERT --" })) : uiState.shellModeActive ? (_jsx(ShellModeIndicator, {})) : showAutoAcceptIndicator !== undefined &&
        showAutoAcceptIndicator !== ApprovalMode.DEFAULT ? (_jsx(AutoAcceptIndicator, { approvalMode: showAutoAcceptIndicator })) : suppressHint ? null : (_jsx(Text, { color: theme.text.secondary, children: t('? for shortcuts') }));
    const rightItems = [];
    if (sandboxInfo) {
        rightItems.push({
            key: 'sandbox',
            node: _jsxs(Text, { color: theme.status.success, children: ["\uD83D\uDD12 ", sandboxInfo] }),
        });
    }
    if (debugMode) {
        rightItems.push({
            key: 'debug',
            node: _jsx(Text, { color: theme.status.warning, children: "Debug Mode" }),
        });
    }
    if (dreamRunning) {
        rightItems.push({
            key: 'dream',
            node: _jsx(Text, { color: theme.text.secondary, children: t('✦ dreaming') }),
        });
    }
    if (promptTokenCount > 0 && contextWindowSize) {
        rightItems.push({
            key: 'context',
            node: (_jsx(Text, { color: theme.text.accent, children: _jsx(ContextUsageDisplay, { promptTokenCount: promptTokenCount, terminalWidth: terminalWidth, contextWindowSize: contextWindowSize }) })),
        });
    }
    // Layout matches upstream: left column has status line (top) + hints/mode
    // (bottom), right section has indicators. Status line and hints coexist.
    return (_jsxs(Box, { flexDirection: isNarrow ? 'column' : 'row', justifyContent: isNarrow ? 'flex-start' : 'space-between', width: "100%", paddingX: 2, gap: isNarrow ? 0 : 1, children: [_jsxs(Box, { flexDirection: "column", flexShrink: isNarrow ? 0 : 1, children: [statusLineLines.length > 0 &&
                        !uiState.ctrlCPressedOnce &&
                        !uiState.ctrlDPressedOnce &&
                        statusLineLines.map((line, i) => (_jsx(Text, { dimColor: true, wrap: "truncate", children: line }, `status-line-${i}`))), _jsx(Text, { wrap: "truncate", children: leftBottomContent })] }), _jsx(Box, { flexShrink: 0, gap: 1, alignItems: "flex-start", children: rightItems.map(({ key, node }, index) => (_jsxs(Box, { alignItems: "center", children: [index > 0 && _jsx(Text, { color: theme.text.secondary, children: " | " }), node] }, key))) })] }));
};
//# sourceMappingURL=Footer.js.map