import * as fs from "node:fs";
import * as path from "node:path";
import { getCapabilities, truncateToWidth, visibleWidth } from "@voxcode-dev/voxcode-tui";
import { APP_NAME } from "../../../config.js";
import { theme } from "../theme/theme.js";
const BANNER_PATH = path.join(process.env.HOME || "/root", "banner.txt");
const EMBEDDED_BANNER = [
    "⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀",
    "⠀⠀⠀⠀⠀⠀⣄⠀⠀⠀⣦⣤⣾⣿⠿⠛⣋⣥⣤⣀⠀⠀⠀⠀",
    "⠀⠀⠀⠀⡤⡀⢈⢻⣬⣿⠟⢁⣤⣶⣿⣿⡿⠿⠿⠛⠛⢀⣄⠀",
    "⠀⠀⢢⣘⣿⣿⣶⣿⣯⣤⣾⣿⣿⣿⠟⠁⠄⠀⣾⡇⣼⢻⣿⣾",
    "⣰⠞⠛⢉⣩⣿⣿⣿⣿⣿⣿⣿⣿⠋⣼⣧⣤⣴⠟⣠⣿⢰⣿⣿",
    "⣶⡾⠿⠿⠿⢿⣿⣿⣿⣿⣿⣿⣿⣈⣩⣤⡶⠟⢛⣩⣴⣿⣿⡟",
    "⣠⣄⠈⠀⣰⡦⠙⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣟⡛⠛⠛⠁",
    "⣉⠛⠛⠛⣁⡔⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠥⠀⠀",
    "⣭⣏⣭⣭⣥⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢠",
];
function loadBannerLines() {
    try {
        if (fs.existsSync(BANNER_PATH)) {
            const content = fs.readFileSync(BANNER_PATH, "utf-8");
            const lines = content.split("\n").filter((l) => l.trim().length > 0);
            if (lines.length > 0 && lines.length <= 12)
                return lines;
        }
    }
    catch {
        // Fallback to embedded banner
    }
    return EMBEDDED_BANNER;
}
function supportsBraille() {
    const caps = getCapabilities();
    return caps.trueColor || true;
}
function padCenter(text, width) {
    const textWidth = visibleWidth(text);
    if (textWidth >= width)
        return text;
    const left = Math.floor((width - textWidth) / 2);
    const right = width - textWidth - left;
    return " ".repeat(left) + text + " ".repeat(right);
}
function renderLine(width, content) {
    return truncateToWidth(theme.fg("border", "│") + content + theme.fg("border", "│"), width);
}
function renderEmptyLine(width) {
    return theme.fg("border", "│") + " ".repeat(Math.max(0, width - 2)) + theme.fg("border", "│");
}
export class WelcomeBox {
    cwd;
    version;
    dismissed = false;
    bannerLines;
    constructor(options) {
        this.version = options.version;
        this.cwd = options.cwd;
        this.bannerLines = loadBannerLines();
    }
    dismiss() {
        this.dismissed = true;
    }
    invalidate() {
        // No cached state
    }
    render(width) {
        if (this.dismissed)
            return [];
        const result = [];
        const contentInner = Math.max(2, width - 2);
        const borderInner = Math.max(0, width - 4);
        const title = ` ${APP_NAME} `;
        const titleLine = `╭─${title}${"─".repeat(Math.max(0, borderInner - visibleWidth(title)))}─╮`;
        result.push(theme.fg("borderAccent", titleLine));
        result.push(renderEmptyLine(width));
        const useBraille = this.bannerLines.length > 0 && supportsBraille();
        const displayLines = useBraille ? this.bannerLines : [theme.bold("Voxcode")];
        for (const line of displayLines) {
            const coloredLine = theme.fg("accent", line);
            result.push(renderLine(width, padCenter(coloredLine, contentInner)));
        }
        result.push(renderEmptyLine(width));
        const versionStr = `${APP_NAME} v${this.version}`;
        result.push(renderLine(width, padCenter(theme.fg("text", versionStr), contentInner)));
        result.push(renderLine(width, padCenter(theme.fg("dim", this.cwd), contentInner)));
        result.push(renderEmptyLine(width));
        const hintStr = `─── type a message to start ───`;
        result.push(renderLine(width, padCenter(theme.fg("dim", hintStr), contentInner)));
        result.push(renderEmptyLine(width));
        result.push(theme.fg("borderAccent", `╰─${"─".repeat(borderInner)}─╯`));
        return result;
    }
}
//# sourceMappingURL=welcome-box.js.map