import { visibleWidth } from "@earendil-works/pi-tui";
import { truncateFromEndByWidth, truncateFromEndByWidthPreserveAnsi, } from "./components/one_line_segments.js";
const SUBAGENT_HEADER_LABEL = "subagents";
const SUBAGENT_PADDING_X = 1;
export class SubagentEditorPaneComponent {
    theme;
    subagentPanel;
    editor;
    constructor(theme, subagentPanel, editor) {
        this.theme = theme;
        this.subagentPanel = subagentPanel;
        this.editor = editor;
    }
    setTheme(theme) {
        this.theme = theme;
    }
    invalidate() {
        this.subagentPanel.invalidate();
        this.editor.invalidate();
    }
    render(width) {
        if (!this.subagentPanel.hasActiveSubagents()) {
            return this.editor.render(width);
        }
        const editorLines = this.editor.render(width);
        if (editorLines.length === 0)
            return editorLines;
        const innerWidth = Math.max(0, width - 2);
        const contentWidth = Math.max(0, innerWidth - SUBAGENT_PADDING_X * 2);
        const panelLines = this.subagentPanel.render(contentWidth);
        const borderedPanelLines = panelLines.map((line) => this.wrapPanelLine(line, contentWidth, SUBAGENT_PADDING_X));
        const headerLine = this.renderSubagentHeaderLine(width);
        const dividerLine = this.editor.renderDividerLineWithCornerStyle(width, this.theme.palette.editorSubagentBorder);
        return [headerLine, ...borderedPanelLines, dividerLine, ...editorLines.slice(1)];
    }
    wrapPanelLine(line, contentWidth, paddingX) {
        const border = this.theme.palette.editorSubagentBorder;
        const vertical = border("│");
        const pad = " ".repeat(Math.max(0, paddingX));
        const truncated = truncateFromEndByWidthPreserveAnsi(line, contentWidth);
        const padded = this.padToWidth(truncated, contentWidth);
        return `${vertical}${pad}${padded}${pad}${vertical}`;
    }
    padToWidth(line, width) {
        const pad = Math.max(0, width - visibleWidth(line));
        return `${line}${" ".repeat(pad)}`;
    }
    renderSubagentHeaderLine(width) {
        const border = this.theme.palette.editorSubagentBorder;
        if (width <= 1)
            return border("─").repeat(Math.max(0, width));
        if (width === 2) {
            return `${border("╭")}${border("╮")}`;
        }
        const innerWidth = width - 2;
        const dash = border("─");
        const leftCorner = border("╭");
        const rightCorner = border("╮");
        const labelWidth = Math.max(0, innerWidth - 4);
        const label = labelWidth > 0 ? truncateFromEndByWidth(SUBAGENT_HEADER_LABEL, labelWidth) : "";
        const labelSegment = label ? ` ${this.theme.palette.textDim(label)} ` : "";
        const fillWidth = Math.max(0, innerWidth - 2 - visibleWidth(labelSegment));
        const fill = dash.repeat(fillWidth);
        return `${leftCorner}${dash}${labelSegment}${fill}${dash}${rightCorner}`;
    }
}
//# sourceMappingURL=subagent_editor_pane.js.map