import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { firstLine } from "../commands/format.js";
import { StatusBadge } from "./design/StatusBadge.js";
import { truncateCells } from "./design/textLayout.js";
export function CompactRunList(props) {
    const rows = compactRunListRows(props.runs, props.limit);
    if (rows.length === 0)
        return _jsx(Text, { color: "gray", children: "none" });
    return (_jsx(Box, { flexDirection: "column", children: rows.map((row) => (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsxs(Box, { flexDirection: "row", children: [_jsx(StatusBadge, { label: row.status, tone: row.tone }), _jsx(Text, { color: "gray", children: ` ${truncateCells(row.summary, 20)}` })] }), _jsx(Text, { color: "gray", children: truncateCells(`  ${row.detail}`, 30) })] }, row.key))) }));
}
export function compactRunListRows(runs, limit = 5) {
    return runs.slice(0, limit).map((run) => ({
        key: run.id,
        status: run.status,
        tone: toneForRunStatus(run.status),
        summary: firstLine(run.message || "recent run", 28),
        detail: [
            `${run.actionCount} actions`,
            `${run.artifactCount} artifacts`,
            cacheNote(run),
        ].filter(Boolean).join(" | "),
    }));
}
function toneForRunStatus(status) {
    if (status === "succeeded")
        return "success";
    if (status === "failed" || status === "cancelled")
        return "error";
    if (status === "running" || status === "paused")
        return "warning";
    return "muted";
}
function cacheNote(run) {
    const hits = run.cacheHitTokens ?? 0;
    const misses = run.cacheMissTokens ?? 0;
    if (hits === 0 && misses === 0)
        return "";
    const total = hits + misses;
    return `cache=${total > 0 ? Math.round((hits / total) * 100) : 0}%`;
}
