import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
function firstLine(text) {
    const idx = text.indexOf("\n");
    if (idx === -1)
        return text;
    return text.slice(0, idx);
}
export class QueuedMessagesComponent {
    theme;
    messages;
    constructor(theme, messages) {
        this.theme = theme;
        this.messages = messages;
    }
    setTheme(theme) {
        this.theme = theme;
    }
    invalidate() { }
    render(width) {
        if (this.messages.length === 0)
            return [];
        const { palette, markdownTheme } = this.theme;
        const lines = [];
        const headerRaw = `queued (${this.messages.length}) — alt+up to edit next`;
        const headerPad = " ";
        const headerWidth = Math.max(0, width - visibleWidth(headerPad));
        lines.push(palette.textDim(`${headerPad}${truncateToWidth(headerRaw, headerWidth, "…")}`));
        for (const [index, message] of this.messages.entries()) {
            const prefixRawArrow = `  ${index + 1}› `;
            const prefix = palette.textDim(prefixRawArrow);
            const prefixWidth = visibleWidth(prefixRawArrow);
            const line = firstLine(message);
            const available = Math.max(0, width - prefixWidth);
            const truncated = truncateToWidth(line, available, "…");
            const styled = markdownTheme.italic(palette.textMuted(truncated));
            lines.push(`${prefix}${styled}`);
        }
        return lines;
    }
}
//# sourceMappingURL=queued_messages.js.map