import { truncateToWidth, visibleWidth, wrapTextWithAnsi } from "@threadwell/tui";
import { theme } from "../theme/theme.js";
/**
 * Dynamic border component that adjusts to viewport width.
 *
 * Note: When used from extensions loaded via jiti, the global `theme` may be undefined
 * because jiti creates a separate module cache. Always pass an explicit color
 * function when using DynamicBorder in components exported for extension use.
 */
export class DynamicBorder {
    color;
    constructor(color = (str) => theme.fg("border", str)) {
        this.color = color;
    }
    invalidate() {
        // No cached state to invalidate currently
    }
    render(width) {
        return [this.color("─".repeat(Math.max(1, width)))];
    }
}
export class DynamicFrame {
    text;
    color;
    wrap;
    contentStyle;
    title;
    constructor(text, color = (str) => theme.fg("border", str), wrap = false, contentStyle = (str) => str, title) {
        this.text = text;
        this.color = color;
        this.wrap = wrap;
        this.contentStyle = contentStyle;
        this.title = title;
    }
    invalidate() {
        // No cached state to invalidate currently
    }
    render(width) {
        const innerWidth = Math.max(1, width - 2);
        const lines = this.wrap ? wrapTextWithAnsi(this.text, innerWidth) : this.text.split(/\r?\n/u);
        return [
            this.renderTopBorder(innerWidth),
            ...lines.map((line) => `${this.color("│")}${this.contentStyle(truncateToWidth(line, innerWidth, "", true))}${this.color("│")}`),
            this.color(`╰${"─".repeat(innerWidth)}╯`),
        ];
    }
    renderTopBorder(innerWidth) {
        if (!this.title)
            return this.color(`╭${"─".repeat(innerWidth)}╮`);
        const title = truncateToWidth(this.title, Math.max(1, innerWidth - 2), "", false);
        const titleWidth = visibleWidth(title);
        const availableRuleWidth = Math.max(0, innerWidth - titleWidth);
        const leftWidth = Math.floor(availableRuleWidth / 2);
        const rightWidth = availableRuleWidth - leftWidth;
        return this.color(`╭${"─".repeat(leftWidth)}`) + title + this.color(`${"─".repeat(rightWidth)}╮`);
    }
}
//# sourceMappingURL=dynamic-border.js.map