import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { Byline } from "./design/Byline.js";
import { Divider } from "./design/Divider.js";
import { KeyboardShortcutHint } from "./design/KeyboardShortcutHint.js";
import { StatusBadge } from "./design/StatusBadge.js";
import { truncateCells } from "./design/textLayout.js";
import { toneColor } from "./design/terminalTheme.js";
export function SessionPreview(props) {
    return (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsx(Divider, { width: Math.max(12, props.width - 2), title: props.model.title, tone: "muted" }), _jsx(Text, { color: "gray", children: truncateCells(props.model.summary, Math.max(20, props.width - 4)) }), props.model.rows.length === 0 ? (_jsx(Text, { color: "gray", children: "No transcript preview" })) : props.model.rows.map((row) => (_jsxs(Box, { flexDirection: "row", children: [_jsxs(Text, { color: toneColor(row.marker === ">" ? "brand" : "muted"), children: [row.marker, " "] }), _jsx(StatusBadge, { label: row.role, tone: row.tone }), _jsx(Text, { color: "gray", children: " " }), _jsx(Text, { color: toneColor(row.tone), children: truncateCells(row.text, Math.max(16, props.width - 34)) }), row.note ? _jsx(Text, { color: "gray", children: truncateCells(` ${row.note}`, 18) }) : null] }, row.key))), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: "gray", children: _jsxs(Byline, { children: [_jsx(KeyboardShortcutHint, { shortcut: "/resume", action: "continue" }), _jsx(Text, { children: props.model.footer })] }) }) })] }));
}
export function sessionPreviewModel(records, title = "preview", limit = 5) {
    const rows = records.slice(-Math.max(1, limit)).map((record, index, visible) => ({
        key: record.id,
        role: record.role,
        tone: previewToneForRole(record.role),
        marker: index === visible.length - 1 ? ">" : " ",
        text: firstLine(record.text, 120),
        note: record.runId ? `run=${shortId(record.runId)}` : "",
    }));
    const counts = previewRoleCounts(records);
    return {
        title,
        summary: [
            `${records.length} messages`,
            `${counts.user} user`,
            `${counts.assistant} assistant`,
            counts.tool > 0 ? `${counts.tool} tool` : "",
            counts.error > 0 ? `${counts.error} error` : "",
        ].filter(Boolean).join(" | "),
        rows,
        footer: rows.length > 0 ? "latest message is selected" : "empty session",
    };
}
export function previewRoleCounts(records) {
    const counts = { user: 0, assistant: 0, tool: 0, error: 0, other: 0 };
    for (const record of records) {
        if (record.role === "user")
            counts.user += 1;
        else if (record.role === "assistant")
            counts.assistant += 1;
        else if (record.role === "tool")
            counts.tool += 1;
        else if (record.role === "error")
            counts.error += 1;
        else
            counts.other += 1;
    }
    return counts;
}
function previewToneForRole(role) {
    if (role === "assistant")
        return "success";
    if (role === "user")
        return "brand";
    if (role === "tool")
        return "warning";
    if (role === "error")
        return "error";
    return "muted";
}
function firstLine(value, max) {
    const line = value.split(/\r?\n/)[0]?.trim() ?? "";
    return line.length > max ? `${line.slice(0, Math.max(0, max - 3))}...` : line;
}
function shortId(value) {
    return value.length <= 12 ? value : `${value.slice(0, 8)}...`;
}
