import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * BackgroundTaskCard -- compact card displayed below the input box for
 * tasks running in the background (launched via Ctrl+B).
 *
 * Mirrors Claude Code's background task UX:
 *   - Running tasks show a spinner + agent name + elapsed time
 *   - Completed tasks show a checkmark + elapsed time
 *   - Failed tasks show a cross + elapsed time
 *   - Ctrl+O expands/collapses task output
 *   - Down arrow from empty input navigates to background tasks
 *
 * All colors from useTheme() semantic tokens.
 */
import { useState, useEffect } from 'react';
import { Box, Text } from 'ink';
import { useTheme } from '../contexts/theme-context.js';
import { OaSpinner } from './spinner.js';
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function formatElapsed(ms) {
    const totalSeconds = Math.floor(ms / 1000);
    if (totalSeconds < 60)
        return `${totalSeconds}s`;
    const minutes = Math.floor(totalSeconds / 60);
    const seconds = totalSeconds % 60;
    return `${minutes}m ${seconds}s`;
}
/** Maximum lines of output to render when expanded. */
const MAX_EXPANDED_LINES = 20;
// ---------------------------------------------------------------------------
// Component
// ---------------------------------------------------------------------------
export function BackgroundTaskCard({ task, isFocused, onToggleExpand, }) {
    const { colors } = useTheme();
    // Live elapsed timer for running tasks
    const [elapsed, setElapsed] = useState(task.elapsedMs);
    useEffect(() => {
        if (task.status !== 'running') {
            setElapsed(task.elapsedMs);
            return;
        }
        // Tick every second while running
        const id = setInterval(() => {
            setElapsed(Date.now() - task.startedAt);
        }, 1000);
        return () => clearInterval(id);
    }, [task.status, task.startedAt, task.elapsedMs]);
    // Status icon
    const statusIcon = task.status === 'running' ? (_jsx(OaSpinner, { active: true, label: "" })) : task.status === 'completed' ? (_jsx(Text, { color: colors.status.done, bold: true, children: '✓' })) : (_jsx(Text, { color: colors.status.error, bold: true, children: '✗' }));
    // Status label for the subtitle line
    const statusLabel = task.status === 'running'
        ? 'Backgrounded agent'
        : task.status === 'completed'
            ? 'Done'
            : 'Failed';
    // Hint text
    const hintText = task.expanded
        ? 'ctrl+o to collapse'
        : '\u2193 to manage \u00b7 ctrl+o to expand';
    // Focus indicator: left border highlight
    const focusBorderColor = isFocused ? colors.border.focus : colors.border.dim;
    // Split output for expansion
    const outputLines = task.output ? task.output.split('\n') : [];
    const visibleLines = outputLines.slice(0, MAX_EXPANDED_LINES);
    const hiddenCount = outputLines.length - visibleLines.length;
    // Suppress unused var warning -- onToggleExpand is provided for keyboard
    // handler integration; the card itself doesn't render a clickable element
    // in a TUI context. The parent layout wires Ctrl+O to call it.
    void onToggleExpand;
    return (_jsxs(Box, { flexDirection: "column", paddingLeft: 1, children: [_jsxs(Box, { children: [_jsx(Text, { color: focusBorderColor, children: isFocused ? '\u2502 ' : '  ' }), statusIcon, _jsxs(Text, { color: colors.text.accent, bold: true, children: [' ', task.name] }), _jsxs(Text, { color: colors.text.secondary, children: [' \u2014 ', formatElapsed(elapsed)] })] }), _jsx(Box, { paddingLeft: isFocused ? 4 : 4, children: _jsxs(Text, { color: colors.text.secondary, children: ['\u2514 ', statusLabel, ' (', hintText, ')'] }) }), task.expanded && outputLines.length > 0 && (_jsxs(Box, { paddingLeft: 6, flexDirection: "column", marginTop: 0, children: [visibleLines.map((line, i) => (_jsx(Text, { color: colors.text.primary, children: line }, i))), hiddenCount > 0 && (_jsxs(Text, { color: colors.text.secondary, children: ["... (", hiddenCount, " more line", hiddenCount !== 1 ? 's' : '', ")"] }))] })), !task.expanded && task.summary && task.status !== 'running' && (_jsx(Box, { paddingLeft: 6, children: _jsx(Text, { color: colors.text.secondary, dimColor: true, children: task.summary.length > 60 ? task.summary.slice(0, 60) + '...' : task.summary }) }))] }));
}
//# sourceMappingURL=background-task-card.js.map