import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { StatusBadge } from "./design/StatusBadge.js";
import { truncateCells } from "./design/textLayout.js";
import { toneColor } from "./design/terminalTheme.js";
export function agentProgressLineModel(input) {
    const index = input.index ?? 0;
    const total = input.total ?? 1;
    const connector = total <= 1 ? "  " : index === total - 1 ? "`-" : "|-";
    return {
        key: input.key,
        agent: input.agent,
        description: input.description,
        status: input.status,
        tone: agentProgressTone(input.status),
        connector,
        selected: input.selected ?? index === 0,
        viewed: input.viewed ?? false,
        meta: agentProgressMeta(input),
        activity: input.activity ?? agentProgressActivity(input.status),
    };
}
export function AgentProgressLine(props) {
    const width = props.width ?? 96;
    const prefix = props.model.selected ? "> " : "  ";
    const viewed = props.model.viewed ? "*" : "o";
    const label = `${prefix}${props.model.connector} ${viewed} ${props.model.agent}`;
    const labelWidth = Math.max(14, Math.min(28, Math.floor(width * 0.28)));
    const bodyWidth = Math.max(16, width - labelWidth - 20);
    return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Box, { flexDirection: "row", children: [_jsx(Text, { color: toneColor(props.model.selected ? "brand" : "muted"), children: truncateCells(label.padEnd(labelWidth), labelWidth) }), _jsx(StatusBadge, { label: props.model.status, tone: props.model.tone }), _jsx(Text, { color: "gray", children: " " }), _jsx(Text, { color: toneColor(props.model.tone), children: truncateCells(props.model.description, bodyWidth) })] }), _jsx(Box, { paddingLeft: Math.min(labelWidth, 28), children: _jsx(Text, { color: "gray", children: truncateCells([props.model.activity, props.model.meta].filter(Boolean).join(" | "), Math.max(18, width - labelWidth - 2)) }) })] }));
}
function agentProgressMeta(input) {
    return [
        input.elapsedMs !== undefined ? formatDuration(input.elapsedMs) : "",
        input.toolUseCount !== undefined ? `${input.toolUseCount} tool${input.toolUseCount === 1 ? "" : "s"}` : "",
        input.tokenCount !== undefined ? `${formatNumber(input.tokenCount)} tokens` : "",
        input.queuedCount !== undefined && input.queuedCount > 0 ? `${input.queuedCount} queued` : "",
    ].filter(Boolean).join(" | ");
}
function agentProgressActivity(status) {
    if (status === "running" || status === "queued" || status === "runnable")
        return "ready for agent work";
    if (status === "succeeded" || status === "ok")
        return "completed";
    if (status === "failed" || status === "cancelled")
        return "needs attention";
    if (status === "paused" || status === "max_steps")
        return "waiting";
    return "idle";
}
function agentProgressTone(status) {
    if (status === "succeeded" || status === "ok" || status === "runnable")
        return "success";
    if (status === "failed" || status === "cancelled")
        return "error";
    if (status === "running" || status === "queued" || status === "paused" || status === "max_steps")
        return "warning";
    return "muted";
}
function formatNumber(value) {
    return Math.trunc(value).toLocaleString("en-US");
}
function formatDuration(ms) {
    const totalSeconds = Math.max(0, Math.floor(ms / 1000));
    const minutes = Math.floor(totalSeconds / 60);
    const seconds = totalSeconds % 60;
    if (minutes <= 0)
        return `${seconds}s`;
    return `${minutes}m ${seconds}s`;
}
