import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { Byline } from "./design/Byline.js";
import { KeyboardShortcutHint } from "./design/KeyboardShortcutHint.js";
import { Pane } from "./design/Pane.js";
import { ProgressBar } from "./design/ProgressBar.js";
import { StatusBadge } from "./design/StatusBadge.js";
import { truncateCells } from "./design/textLayout.js";
import { toneColor } from "./design/terminalTheme.js";
export function CompactSummary(props) {
    const width = props.width ?? 96;
    const barWidth = Math.max(12, Math.min(32, width - 44));
    return (_jsxs(Pane, { width: width, title: "Compact summary", tone: "brand", children: [_jsxs(Box, { flexDirection: "row", children: [_jsx(Text, { color: "cyan", children: truncateCells(props.model.sessionId, Math.max(12, width - 40)) }), _jsx(Text, { color: "gray", children: " " }), _jsx(StatusBadge, { label: `${props.model.summarizedRecords} summarized`, tone: props.model.summarizedRecords > 0 ? "success" : "muted" })] }), _jsxs(Box, { flexDirection: "row", marginTop: 1, children: [_jsx(Text, { color: "gray", children: "records " }), _jsx(ProgressBar, { ratio: props.model.ratio, width: barWidth, filledTone: "success", showPercent: true }), _jsx(Text, { color: "gray", children: ` total ${props.model.totalRecords} | tail ${props.model.tailRecords}` })] }), _jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsx(Text, { color: "gray", children: "summary" }), props.model.summaryLines.length > 0 ? props.model.summaryLines.map((line, index) => (_jsx(Text, { color: "gray", children: truncateCells(line, Math.max(20, width - 4)) }, `summary-${index}`))) : (_jsx(Text, { color: "gray", children: "not enough transcript records to compact" }))] }), _jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsx(Text, { color: "gray", children: "tail" }), props.model.tailRows.length > 0 ? props.model.tailRows.map((row) => (_jsxs(Box, { flexDirection: "row", children: [_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, width - 18)) })] }, row.key))) : (_jsx(Text, { color: "gray", children: "No retained tail messages" }))] }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: "gray", children: _jsxs(Byline, { children: [_jsx(KeyboardShortcutHint, { shortcut: "/resume", action: "continue" }), _jsx(KeyboardShortcutHint, { shortcut: "/sessions", action: "browse" }), _jsx(Text, { children: props.model.footer })] }) }) })] }));
}
export function compactSummaryModel(summary, tailLimit = 6) {
    const total = Math.max(0, summary.totalRecords);
    const summarized = Math.max(0, summary.summarizedRecords);
    return {
        sessionId: summary.sessionId,
        totalRecords: total,
        summarizedRecords: summarized,
        tailRecords: summary.tailRecords.length,
        ratio: total === 0 ? 0 : summarized / total,
        summaryLines: compactSummaryLines(summary.summary),
        tailRows: summary.tailRecords.slice(-Math.max(1, tailLimit)).map((record) => ({
            key: record.id,
            role: record.role,
            tone: compactToneForRole(record.role),
            text: firstLine(record.text, 180),
        })),
        footer: summarized > 0 ? "compact preview is local until used by a future prompt" : "nothing compacted yet",
    };
}
export function compactSummaryLines(value, limit = 5) {
    return value
        .split(/\r?\n/)
        .map((line) => line.trim())
        .filter(Boolean)
        .slice(0, Math.max(1, limit));
}
function compactToneForRole(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;
}
