import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 * @plan PLAN-20250909-TOKTRACK.P06
 * @plan PLAN-20250909-TOKTRACK.P16
 * @requirement REQ-INT-001.1
 */
/* eslint-disable react/prop-types */
import React, { useEffect, useState } from 'react';
import { Box, Text } from 'ink';
import { Colors, SemanticColors } from '../colors.js';
import { shortenPath, tildeifyPath, tokenLimit, } from '@vybestack/llxprt-code-core';
import { ConsoleSummaryDisplay } from './ConsoleSummaryDisplay.js';
import process from 'node:process';
import Gradient from 'ink-gradient';
import { getProviderManager } from '../../providers/providerManagerInstance.js';
import { DebugProfiler } from './DebugProfiler.js';
import { useResponsive } from '../hooks/useResponsive.js';
import { truncateMiddle } from '../utils/responsive.js';
// Responsive Memory Usage Display - Memoized to prevent re-renders
const ResponsiveMemoryDisplay = React.memo(({ compact, detailed }) => {
    // Initialize with immediate value to avoid empty render in tests
    const initialUsage = process.memoryUsage().rss;
    const initialPercentage = Math.round((initialUsage / (4.8 * 1024 * 1024 * 1024)) * 100);
    let initialText;
    if (detailed) {
        const usageGB = (initialUsage / (1024 * 1024 * 1024)).toFixed(1);
        initialText = `Memory: ${initialPercentage}% (${usageGB}GB/4.8GB)`;
    }
    else if (compact) {
        initialText = `Mem: ${initialPercentage}%`;
    }
    else {
        initialText = `Memory: ${initialPercentage}%`;
    }
    const [memoryUsage, setMemoryUsage] = useState(initialText);
    const [memoryUsageColor, setMemoryUsageColor] = useState(initialUsage >= 2 * 1024 * 1024 * 1024
        ? SemanticColors.status.error
        : SemanticColors.text.secondary);
    useEffect(() => {
        const updateMemory = () => {
            const usage = process.memoryUsage().rss;
            const totalMemory = 4.8 * 1024 * 1024 * 1024; // 4.8GB total
            const percentage = Math.round((usage / totalMemory) * 100);
            if (detailed) {
                const usageGB = (usage / (1024 * 1024 * 1024)).toFixed(1);
                const totalGB = (totalMemory / (1024 * 1024 * 1024)).toFixed(1);
                setMemoryUsage(`Memory: ${percentage}% (${usageGB}GB/${totalGB}GB)`);
            }
            else if (compact) {
                setMemoryUsage(`Mem: ${percentage}%`);
            }
            else {
                setMemoryUsage(`Memory: ${percentage}%`);
            }
            setMemoryUsageColor(usage >= 2 * 1024 * 1024 * 1024
                ? SemanticColors.status.error
                : SemanticColors.text.secondary);
        };
        const intervalId = setInterval(updateMemory, 2000);
        // Don't call updateMemory immediately since we have initial value
        return () => clearInterval(intervalId);
    }, [compact, detailed]);
    return _jsx(Text, { color: memoryUsageColor, children: memoryUsage });
});
ResponsiveMemoryDisplay.displayName = 'ResponsiveMemoryDisplay';
// Responsive Context Usage Display - Memoized to prevent re-renders
const ResponsiveContextDisplay = React.memo(({ historyTokenCount, model, contextLimit, compact, detailed }) => {
    const limit = tokenLimit(model, contextLimit);
    const percentage = historyTokenCount / limit;
    const remainingPercentage = (1 - percentage) * 100;
    // Use semantic colors based on how much context is left
    let color;
    if (remainingPercentage < 10) {
        color = SemanticColors.status.error;
    }
    else if (remainingPercentage < 25) {
        color = SemanticColors.status.warning;
    }
    else {
        color = SemanticColors.text.secondary;
    }
    let displayText;
    if (detailed) {
        displayText = `Context: ${historyTokenCount.toLocaleString()}/${limit.toLocaleString()} tokens`;
    }
    else if (compact) {
        displayText = `Ctx: ${(historyTokenCount / 1000).toFixed(1)}k/${(limit / 1000).toFixed(0)}k`;
    }
    else {
        displayText = `Context: ${(historyTokenCount / 1000).toFixed(1)}k/${(limit / 1000).toFixed(0)}k`;
    }
    return _jsx(Text, { color: color, children: displayText });
});
ResponsiveContextDisplay.displayName = 'ResponsiveContextDisplay';
// Debounced TPM Display - Updates less frequently to reduce flicker
const DebouncedTPMDisplay = React.memo(({ tokensPerMinute }) => {
    const [displayTPM, setDisplayTPM] = useState(tokensPerMinute);
    useEffect(() => {
        // Debounce TPM updates to reduce flicker
        const timeoutId = setTimeout(() => {
            setDisplayTPM(tokensPerMinute);
        }, 500); // 500ms debounce
        return () => clearTimeout(timeoutId);
    }, [tokensPerMinute]);
    if (displayTPM === undefined)
        return null;
    return (_jsx(Text, { color: SemanticColors.text.accent, children: displayTPM < 1000
            ? `TPM: ${displayTPM.toFixed(2)}`
            : `TPM: ${(displayTPM / 1000).toFixed(2)}k` }));
});
DebouncedTPMDisplay.displayName = 'DebouncedTPMDisplay';
// Debounced Wait Time Display
const DebouncedWaitDisplay = React.memo(({ throttleWaitTimeMs }) => {
    const [displayWait, setDisplayWait] = useState(throttleWaitTimeMs);
    useEffect(() => {
        // Debounce wait time updates
        const timeoutId = setTimeout(() => {
            setDisplayWait(throttleWaitTimeMs);
        }, 300); // 300ms debounce
        return () => clearTimeout(timeoutId);
    }, [throttleWaitTimeMs]);
    if (displayWait === undefined)
        return null;
    return (_jsx(Text, { color: SemanticColors.status.warning, children: displayWait < 1000
            ? `Wait: ${displayWait}ms`
            : displayWait < 60000
                ? `Wait: ${(displayWait / 1000).toFixed(1)}s`
                : `Wait: ${(displayWait / 60000).toFixed(1)}m` }));
});
DebouncedWaitDisplay.displayName = 'DebouncedWaitDisplay';
// Responsive Timestamp Display - Isolated component for clock updates
const ResponsiveTimestamp = React.memo(() => {
    // Initialize with immediate value to avoid empty render in tests
    const initialTime = new Date().toTimeString().slice(0, 8); // HH:MM:SS
    const [time, setTime] = useState(initialTime);
    useEffect(() => {
        const updateTime = () => {
            const now = new Date();
            setTime(now.toTimeString().slice(0, 8)); // HH:MM:SS
        };
        const intervalId = setInterval(updateTime, 1000);
        // Don't call updateTime immediately since we have initial value
        return () => clearInterval(intervalId);
    }, []);
    return _jsx(Text, { color: SemanticColors.text.secondary, children: time });
});
ResponsiveTimestamp.displayName = 'ResponsiveTimestamp';
export const Footer = React.memo(({ model, targetDir, branchName, debugMode, debugMessage, errorCount, showErrorDetails, showMemoryUsage, historyTokenCount, isPaidMode, nightly, vimMode, contextLimit, isTrustedFolder, tokensPerMinute, throttleWaitTimeMs, sessionTokenTotal, hideCWD = false, hideSandboxStatus = false, hideModelInfo = false, }) => {
    const { breakpoint } = useResponsive();
    // Define what to show at each breakpoint
    const showTimestamp = breakpoint === 'WIDE';
    const showModelName = breakpoint !== 'NARROW';
    const isCompact = breakpoint === 'NARROW';
    const isDetailed = breakpoint === 'WIDE';
    // Calculate max length for branch truncation based on breakpoint
    let maxBranchLength;
    if (isCompact) {
        maxBranchLength = 15;
    }
    else if (breakpoint === 'STANDARD') {
        maxBranchLength = 35;
    }
    else {
        maxBranchLength = 100; // Don't truncate at wide width
    }
    return (_jsxs(Box, { flexDirection: "column", width: "100%", children: [_jsxs(Box, { justifyContent: "space-between", width: "100%", alignItems: "center", children: [_jsxs(Box, { flexDirection: "row", alignItems: "center", children: [branchName && (_jsx(_Fragment, { children: nightly ? (_jsx(Gradient, { colors: Colors.GradientColors, children: _jsxs(Text, { children: ["(", branchName.length > maxBranchLength
                                                ? truncateMiddle(branchName, maxBranchLength)
                                                : branchName, "*)"] }) })) : (_jsxs(Text, { color: SemanticColors.text.accent, children: ["(", branchName.length > maxBranchLength
                                            ? truncateMiddle(branchName, maxBranchLength)
                                            : branchName, "*)"] })) })), isTrustedFolder === false && (_jsx(Text, { color: SemanticColors.status.warning, children: " (untrusted)" })), debugMode && (_jsxs(_Fragment, { children: [_jsx(DebugProfiler, {}), _jsx(Text, { color: SemanticColors.status.error, children: ' ' + (debugMessage || '--debug') })] })), vimMode && (_jsxs(Text, { color: SemanticColors.text.secondary, children: ["[", vimMode, "] "] }))] }), !hideModelInfo && (_jsxs(Box, { flexDirection: "row", alignItems: "center", children: [showMemoryUsage && (_jsxs(_Fragment, { children: [_jsx(ResponsiveMemoryDisplay, { compact: isCompact, detailed: isDetailed }), _jsx(Text, { color: SemanticColors.text.secondary, children: " | " })] })), _jsx(ResponsiveContextDisplay, { historyTokenCount: historyTokenCount, model: model, contextLimit: contextLimit, compact: isCompact, detailed: isDetailed }), tokensPerMinute !== undefined && (_jsxs(_Fragment, { children: [_jsx(Text, { color: SemanticColors.text.secondary, children: " | " }), _jsx(DebouncedTPMDisplay, { tokensPerMinute: tokensPerMinute })] })), throttleWaitTimeMs !== undefined && (_jsxs(_Fragment, { children: [_jsx(Text, { color: SemanticColors.text.secondary, children: " | " }), _jsx(DebouncedWaitDisplay, { throttleWaitTimeMs: throttleWaitTimeMs })] })), showTimestamp && (_jsxs(_Fragment, { children: [_jsx(Text, { color: SemanticColors.text.secondary, children: " | " }), _jsx(ResponsiveTimestamp, {})] }))] }))] }), _jsxs(Box, { justifyContent: "space-between", width: "100%", alignItems: "center", children: [!hideCWD && (_jsxs(Box, { flexDirection: "row", alignItems: "center", children: [nightly ? (_jsx(Gradient, { colors: Colors.GradientColors, children: _jsx(Text, { children: shortenPath(tildeifyPath(targetDir), isCompact ? 30 : 70) }) })) : (_jsx(Text, { color: SemanticColors.text.secondary, children: shortenPath(tildeifyPath(targetDir), isCompact ? 30 : 70) })), !isCompact && !hideSandboxStatus && (_jsx(Box, { marginLeft: 2, children: process.env.SANDBOX &&
                                    process.env.SANDBOX !== 'sandbox-exec' ? (_jsxs(Text, { color: SemanticColors.status.success, children: ["[", process.env.SANDBOX.replace(/^gemini-(?:cli-)?/, ''), "]"] })) : process.env.SANDBOX === 'sandbox-exec' ? (_jsxs(Text, { color: SemanticColors.status.warning, children: ["[macOS Seatbelt", ' ', _jsxs(Text, { color: SemanticColors.text.secondary, children: ["(", process.env.SEATBELT_PROFILE, ")"] }), "]"] })) : (_jsxs(Text, { color: SemanticColors.status.error, children: ["[no sandbox", ' ', _jsx(Text, { color: SemanticColors.text.secondary, children: "(see /docs)" }), "]"] })) }))] })), !hideModelInfo && (_jsxs(Box, { flexDirection: "row", alignItems: "center", children: [showModelName && (_jsx(Text, { color: SemanticColors.text.accent, children: model })), isPaidMode !== undefined &&
                                (() => {
                                    const providerManager = getProviderManager();
                                    const activeProvider = providerManager?.getActiveProvider?.();
                                    const isGeminiProvider = activeProvider?.name === 'gemini';
                                    if (isGeminiProvider) {
                                        return (_jsxs(_Fragment, { children: [showModelName && (_jsx(Text, { color: SemanticColors.text.secondary, children: " | " })), _jsx(Text, { color: isPaidMode
                                                        ? SemanticColors.status.warning
                                                        : SemanticColors.status.success, children: isPaidMode ? 'paid mode' : 'free mode' })] }));
                                    }
                                    return null;
                                })(), sessionTokenTotal !== undefined && (_jsxs(_Fragment, { children: [_jsx(Text, { color: SemanticColors.text.secondary, children: " | " }), _jsxs(Text, { color: SemanticColors.text.accent, children: ["Tokens: ", sessionTokenTotal.toLocaleString()] })] })), !showErrorDetails && errorCount > 0 && (_jsxs(_Fragment, { children: [_jsx(Text, { color: SemanticColors.text.secondary, children: " | " }), _jsx(ConsoleSummaryDisplay, { errorCount: errorCount })] }))] }))] })] }));
}, (prevProps, nextProps) => 
// Custom comparison function - ignore rapidly changing values
// Only re-render if important props change
prevProps.model === nextProps.model &&
    prevProps.targetDir === nextProps.targetDir &&
    prevProps.branchName === nextProps.branchName &&
    prevProps.debugMode === nextProps.debugMode &&
    prevProps.debugMessage === nextProps.debugMessage &&
    prevProps.errorCount === nextProps.errorCount &&
    prevProps.showErrorDetails === nextProps.showErrorDetails &&
    prevProps.showMemoryUsage === nextProps.showMemoryUsage &&
    prevProps.historyTokenCount === nextProps.historyTokenCount &&
    prevProps.isPaidMode === nextProps.isPaidMode &&
    prevProps.nightly === nextProps.nightly &&
    prevProps.vimMode === nextProps.vimMode &&
    prevProps.contextLimit === nextProps.contextLimit &&
    prevProps.isTrustedFolder === nextProps.isTrustedFolder);
Footer.displayName = 'Footer';
//# sourceMappingURL=Footer.js.map