import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from "ink";
import { Divider } from "./design/Divider.js";
import { StatusBadge } from "./design/StatusBadge.js";
import { truncateCells } from "./design/textLayout.js";
import { toneColor } from "./design/terminalTheme.js";
export function MessageSelector(props) {
    const width = Math.max(42, props.width);
    return (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsx(Divider, { width: Math.max(12, width - 2), title: props.model.title, tone: "muted" }), _jsx(Text, { color: "gray", children: truncateCells(props.model.summary, Math.max(20, width - 4)) }), props.model.rows.length === 0 ? (_jsx(Text, { color: "gray", children: "No user messages to select" })) : props.model.rows.map((row) => (_jsxs(Box, { flexDirection: "row", children: [_jsxs(Text, { color: toneColor(row.selected ? "brand" : "muted"), children: [row.marker, " "] }), _jsx(StatusBadge, { label: row.label, tone: row.tone }), _jsx(Text, { color: "gray", children: " " }), _jsx(Text, { color: toneColor(row.tone), children: truncateCells(row.text, Math.max(16, width - 36)) }), row.note ? _jsx(Text, { color: "gray", children: truncateCells(` ${row.note}`, 22) }) : null] }, row.key))), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: "gray", children: truncateCells(props.model.footer, Math.max(20, width - 4)) }) })] }));
}
export function messageSelectorModel(records, options = {}) {
    const visibleCount = Math.max(1, options.visibleCount ?? 7);
    const selectable = records.filter(isSelectableUserRecord);
    const selectedIndex = selectedMessageIndex(selectable, options.selectedId);
    const start = Math.max(0, Math.min(selectedIndex - Math.floor(visibleCount / 2), selectable.length - visibleCount));
    const visible = selectable.slice(start, start + visibleCount);
    const hiddenBefore = start;
    const hiddenAfter = Math.max(0, selectable.length - start - visible.length);
    return {
        title: options.title ?? "message selector",
        summary: messageSelectorSummary(selectable.length, hiddenBefore, hiddenAfter),
        rows: visible.map((record) => selectorRow(record, records, record.id === selectable[selectedIndex]?.id)),
        footer: selectable.length > 0
            ? "planned: choose a user turn for rewind or summarization"
            : "send a user prompt before message-level restore is useful",
    };
}
export function isSelectableUserRecord(record) {
    return record.role === "user" && record.text.trim().length > 0;
}
function selectedMessageIndex(records, selectedId) {
    if (records.length === 0)
        return 0;
    const byId = selectedId ? records.findIndex((record) => record.id === selectedId) : -1;
    return byId >= 0 ? byId : records.length - 1;
}
function selectorRow(record, allRecords, selected) {
    const transcriptIndex = allRecords.findIndex((entry) => entry.id === record.id);
    const messagesAfter = transcriptIndex >= 0 ? allRecords.length - transcriptIndex - 1 : 0;
    return {
        key: record.id,
        marker: selected ? ">" : " ",
        label: selected ? "target" : "user",
        tone: selected ? "brand" : "muted",
        text: firstLine(record.text, 120),
        note: [
            messagesAfter > 0 ? `+${messagesAfter} after` : "latest",
            relativeRecordTime(record.createdAtMs),
        ].filter(Boolean).join(" | "),
        selected,
    };
}
function messageSelectorSummary(total, hiddenBefore, hiddenAfter) {
    if (total === 0)
        return "0 selectable user messages";
    const hidden = [
        hiddenBefore > 0 ? `${hiddenBefore} older` : "",
        hiddenAfter > 0 ? `${hiddenAfter} newer` : "",
    ].filter(Boolean).join(" / ");
    return hidden
        ? `${total} selectable user messages / hidden ${hidden}`
        : `${total} selectable user message${total === 1 ? "" : "s"}`;
}
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 relativeRecordTime(createdAtMs) {
    if (!Number.isFinite(createdAtMs) || createdAtMs <= 0)
        return "";
    const seconds = Math.max(0, Math.floor((Date.now() - createdAtMs) / 1000));
    if (seconds < 60)
        return `${seconds}s ago`;
    const minutes = Math.floor(seconds / 60);
    if (minutes < 60)
        return `${minutes}m ago`;
    const hours = Math.floor(minutes / 60);
    return `${hours}h ago`;
}
