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";
export function AttachedRunPanel(props) {
    const model = attachedRunPanelModel(props.run, props.runId);
    if (model.state === "none")
        return _jsx(Text, { color: "gray", children: "none" });
    return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Box, { flexDirection: "row", children: [_jsx(StatusBadge, { label: model.status, tone: model.tone }), _jsx(Text, { color: "cyan", children: ` ${truncateCells(model.title, 20)}` })] }), _jsx(Text, { color: "gray", children: truncateCells(model.detail, 30) }), _jsx(Text, { color: "gray", children: model.command })] }));
}
export function attachedRunPanelModel(run, runId) {
    if (!runId) {
        return {
            state: "none",
            status: "none",
            tone: "muted",
            title: "none",
            detail: "no attached run",
            command: "/attach list",
        };
    }
    if (!run) {
        return {
            state: "missing",
            status: "missing",
            tone: "warning",
            title: shortId(runId),
            detail: "attached run is no longer in local state",
            command: "/attach clear",
        };
    }
    return {
        state: "attached",
        status: run.status,
        tone: toneForRunStatus(run.status),
        title: shortId(run.id),
        detail: `${run.actionCount} actions / ${run.artifactCount} artifacts / ${run.eventCount} events`,
        command: firstLine(run.message || "/agents detail attached", 30),
    };
}
function shortId(id) {
    return id.length <= 16 ? id : `${id.slice(0, 8)}...${id.slice(-6)}`;
}
function firstLine(value, max) {
    const line = value.split(/\r?\n/)[0] ?? "";
    return line.length > max ? `${line.slice(0, max - 3)}...` : line;
}
function toneForRunStatus(status) {
    if (status === "succeeded")
        return "success";
    if (status === "failed" || status === "cancelled")
        return "error";
    if (status === "running" || status === "paused")
        return "warning";
    return "muted";
}
