import { Box, Spacer, Text } from "@earendil-works/pi-tui";
import { VERBATIM_COMPACTION_PREFIX } from "../../../core/messages.js";
import { theme } from "../theme/theme.js";
import { parenthesizedKeyHint } from "./keybinding-hints.js";
/** Renders the durable verbatim compaction boundary without markdown reflow. */
export class CompactionBoundaryMessageComponent extends Box {
    constructor(result) {
        super(1, 1, (text) => theme.bg("customMessageBg", text));
        this.expanded = false;
        this.view = "compactedText" in result
            ? { text: result.compactedText, stats: result.stats, rung: result.rung }
            : result;
        this.updateDisplay();
    }
    setExpanded(expanded) { this.expanded = expanded; this.updateDisplay(); }
    invalidate() { super.invalidate(); this.updateDisplay(); }
    updateDisplay() {
        this.clear();
        const tokenStr = this.view.stats.tokensBefore.toLocaleString();
        const label = theme.fg("customMessageLabel", theme.bold("✻ Context compacted"));
        this.addChild(new Text(label, 0, 0));
        this.addChild(new Spacer(1));
        if (this.expanded) {
            this.addChild(new Text(theme.bold(theme.fg("customMessageText", `Compacted from ${tokenStr} tokens`)), 0, 0));
            this.addChild(new Spacer(1));
            const rendered = this.view.text.split("\n").map((line) => /^\(filtered \d+ lines\)$/.test(line) ? theme.fg("dim", line) : theme.fg("customMessageText", line)).join("\n");
            this.addChild(new Text(rendered, 0, 0));
            return;
        }
        const hint = parenthesizedKeyHint("app.tools.expand", "to expand");
        this.addChild(new Text(theme.fg("customMessageText", `Compacted from ${tokenStr} tokens`) + (hint ? ` ${hint}` : ""), 0, 0));
    }
}
export function compactionBoundaryFromMessage(message, expanded) {
    const details = message.details;
    const content = Array.isArray(message.content)
        ? message.content.filter((block) => block.type === "text").map((block) => block.text).join("\n")
        : message.content;
    const component = new CompactionBoundaryMessageComponent({
        text: content.startsWith(VERBATIM_COMPACTION_PREFIX) ? content.slice(VERBATIM_COMPACTION_PREFIX.length) : content,
        stats: details.stats,
        rung: details.rung,
    });
    component.setExpanded(expanded);
    return component;
}
//# sourceMappingURL=compaction-boundary-message.js.map