import { truncateToWidth, visibleWidth } from "@threadwell/tui";
import { theme } from "../theme/theme.js";
import { getVisualTokens } from "../theme/visual-profile.js";
const MIN_SPLIT_WIDTH = 58;
const MIN_RIGHT_WIDTH = 22;
function padVisible(text, width) {
    const truncated = truncateToWidth(text, width, "", true);
    return truncated + " ".repeat(Math.max(0, width - visibleWidth(truncated)));
}
function centerVisible(text, width) {
    const truncated = truncateToWidth(text, width, "", false);
    const padding = Math.max(0, width - visibleWidth(truncated));
    const left = Math.floor(padding / 2);
    const right = padding - left;
    return `${" ".repeat(left)}${truncated}${" ".repeat(right)}`;
}
function titledRule(left, title, right, width, color, ruleChar = "─") {
    const titleText = ` ${title} `;
    const titleWidth = visibleWidth(titleText);
    if (width <= titleWidth)
        return color(left) + truncateToWidth(titleText, Math.max(0, width), "", true) + color(right);
    const ruleWidth = width - titleWidth;
    const leftRule = Math.floor(ruleWidth / 2);
    const rightRule = ruleWidth - leftRule;
    return `${color(`${left}${ruleChar.repeat(leftRule)}`)}${titleText}${color(`${ruleChar.repeat(rightRule)}${right}`)}`;
}
export class SplitInfoCard {
    options;
    constructor(options) {
        this.options = options;
    }
    invalidate() {
        // No cached state.
    }
    render(width) {
        if (width < MIN_SPLIT_WIDTH)
            return this.renderStacked(width);
        return this.renderSplit(width);
    }
    renderSplit(width) {
        const color = this.options.borderColor ?? ((text) => theme.fg("border", text));
        const visual = getVisualTokens();
        const border = visual.borders.rounded;
        const rule = border.horizontal;
        const innerWidth = Math.max(1, width - 2);
        const rightWidth = Math.max(MIN_RIGHT_WIDTH, Math.floor(innerWidth * 0.34));
        const leftWidth = Math.max(1, innerWidth - rightWidth - 1);
        const bodyRows = Math.max(this.options.leftLines.length, this.options.rightLines.length);
        const lines = [];
        lines.push(titledRule(border.topLeft, theme.fg("accent", this.options.leftTitle), "", leftWidth, color, rule) +
            color(visual.enhanced ? "┬" : "+") +
            titledRule("", theme.fg("muted", this.options.rightTitle), border.topRight, rightWidth, color, rule));
        for (let i = 0; i < bodyRows; i++) {
            const left = this.options.leftLines[i] ?? "";
            const right = this.options.rightLines[i] ?? "";
            lines.push(`${color(border.vertical)}${theme.fg("muted", padVisible(left, leftWidth))}${color(border.vertical)}${theme.fg("muted", padVisible(right, rightWidth))}${color(border.vertical)}`);
        }
        lines.push(color(`${visual.enhanced ? "├" : "+"}${rule.repeat(leftWidth)}${visual.enhanced ? "┴" : "+"}${rule.repeat(rightWidth)}${visual.enhanced ? "┤" : "+"}`));
        for (const footerLine of this.options.footerLines) {
            lines.push(`${color(border.vertical)}${theme.fg("muted", centerVisible(footerLine, innerWidth))}${color(border.vertical)}`);
        }
        lines.push(color(`${border.bottomLeft}${rule.repeat(innerWidth)}${border.bottomRight}`));
        return lines;
    }
    renderStacked(width) {
        const color = this.options.borderColor ?? ((text) => theme.fg("border", text));
        const visual = getVisualTokens();
        const border = visual.borders.rounded;
        const rule = border.horizontal;
        const innerWidth = Math.max(1, width - 2);
        const lines = [];
        lines.push(titledRule(border.topLeft, theme.fg("accent", this.options.leftTitle), border.topRight, innerWidth, color, rule));
        for (const line of this.options.leftLines) {
            lines.push(`${color(border.vertical)}${theme.fg("muted", padVisible(line, innerWidth))}${color(border.vertical)}`);
        }
        lines.push(color(`${visual.enhanced ? "├" : "+"}${rule.repeat(innerWidth)}${visual.enhanced ? "┤" : "+"}`));
        lines.push(`${color(border.vertical)}${centerVisible(theme.fg("muted", this.options.rightTitle), innerWidth)}${color(border.vertical)}`);
        for (const line of this.options.rightLines) {
            lines.push(`${color(border.vertical)}${theme.fg("muted", padVisible(line, innerWidth))}${color(border.vertical)}`);
        }
        lines.push(color(`${visual.enhanced ? "├" : "+"}${rule.repeat(innerWidth)}${visual.enhanced ? "┤" : "+"}`));
        for (const footerLine of this.options.footerLines) {
            lines.push(`${color(border.vertical)}${theme.fg("muted", centerVisible(footerLine, innerWidth))}${color(border.vertical)}`);
        }
        lines.push(color(`${border.bottomLeft}${rule.repeat(innerWidth)}${border.bottomRight}`));
        return lines;
    }
}
//# sourceMappingURL=split-info-card.js.map