import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React from "react";
import { Text, Box, useInput } from "ink";
import { useTheme } from "../theme/theme.js";
const MAX_VISIBLE = 5;
export function getFooterStatusLayoutDecision({ columns, backgroundTaskCount, updatePending, }) {
    const hasBackgroundTasks = backgroundTaskCount > 0;
    const hasUpdateNotice = updatePending;
    const visibleCount = [hasBackgroundTasks, hasUpdateNotice].filter(Boolean).length;
    return {
        hasBackgroundTasks,
        hasUpdateNotice,
        stack: visibleCount > 1 && columns < 100,
        compactBackgroundTasks: visibleCount > 1 && columns < 120,
    };
}
function truncateCommand(command, maxLen) {
    if (command.length <= maxLen)
        return command;
    return command.slice(0, maxLen - 1) + "\u2026";
}
export function BackgroundTasksBar({ tasks, focused, expanded, selectedIndex, onExpand, onCollapse, onKill, onExit, onNavigate, compact = false, }) {
    const theme = useTheme();
    // Keyboard: collapsed+focused — Enter opens, Esc/↑ exits
    useInput((_input, key) => {
        if (!expanded) {
            if (key.return) {
                onExpand();
            }
            else if (key.escape || key.upArrow) {
                onExit();
            }
            return;
        }
        // Expanded mode
        if (key.escape) {
            onCollapse();
            return;
        }
        if (key.upArrow) {
            if (selectedIndex <= 0) {
                onCollapse();
            }
            else {
                onNavigate(selectedIndex - 1);
            }
            return;
        }
        if (key.downArrow) {
            const maxIdx = Math.min(tasks.length, MAX_VISIBLE) - 1;
            if (selectedIndex < maxIdx) {
                onNavigate(selectedIndex + 1);
            }
            return;
        }
        if (_input === "k" || _input === "K") {
            const task = tasks[selectedIndex];
            if (task) {
                onKill(task.id);
            }
        }
    }, { isActive: focused });
    if (tasks.length === 0)
        return null;
    const count = tasks.length;
    const label = `Background task${count !== 1 ? "s" : ""}`;
    const collapsedTextColor = focused ? theme.commandColor : theme.textDim;
    // Collapsed: single summary line
    if (!expanded) {
        return (_jsxs(Box, { paddingLeft: 1, paddingRight: 1, children: [_jsx(Text, { color: collapsedTextColor, children: "● " }), _jsxs(Text, { color: collapsedTextColor, children: ["(", count, ")"] }), _jsxs(Text, { color: collapsedTextColor, children: [" ", compact ? "bg tasks" : label] }), focused && !compact && (_jsxs(Text, { color: theme.textDim, children: [" \u00B7 ", _jsx(Text, { color: theme.commandColor, children: "Enter" }), " to view"] }))] }));
    }
    // Expanded: show up to MAX_VISIBLE tasks
    const visible = tasks.slice(0, MAX_VISIBLE);
    const hidden = count - visible.length;
    return (_jsxs(Box, { flexDirection: "column", paddingLeft: 1, paddingRight: 1, children: [_jsxs(Text, { color: theme.textDim, children: ["-- ", label.toLowerCase(), " (", count, ") --"] }), visible.map((task, i) => {
                const isSelected = i === selectedIndex;
                const textColor = isSelected ? theme.commandColor : theme.textDim;
                const cmd = truncateCommand(task.command, 50);
                const isRunning = task.exitCode === null;
                const dot = isRunning ? "\u25CF" : "\u25CB";
                const statusLabel = isRunning ? "running" : `exit ${task.exitCode}`;
                return (_jsxs(Box, { flexDirection: "row", backgroundColor: isSelected ? theme.border : undefined, children: [_jsx(Box, { width: 9, flexShrink: 0, children: _jsx(Text, { color: textColor, children: task.id }) }), _jsx(Box, { flexGrow: 1, paddingLeft: 1, children: _jsx(Text, { color: textColor, wrap: "truncate", children: cmd }) }), _jsx(Box, { paddingLeft: 2, flexShrink: 0, children: _jsxs(Text, { color: textColor, children: [dot, " ", statusLabel] }) })] }, task.id));
            }), hidden > 0 && _jsxs(Text, { color: theme.textDim, children: ["+", hidden, " more"] }), _jsxs(Text, { color: theme.textDim, children: [_jsx(Text, { color: theme.primary, children: "\u2191\u2193" }), " navigate · ", _jsx(Text, { color: theme.primary, children: "K" }), " kill · ", _jsx(Text, { color: theme.primary, children: "Esc" }), " back"] })] }));
}
//# sourceMappingURL=BackgroundTasksBar.js.map