import { getTheme } from "../../internal/theme-detect.js";
export function renderStatsPane(buffer, rect, stats) {
    buffer.clearRect(rect);
    if (rect.width <= 0 || rect.height <= 0) {
        return;
    }
    const lines = statsToLines(stats, rect.width);
    for (let row = 0; row < rect.height; row += 1) {
        const line = lines[row];
        if (line === undefined) {
            continue;
        }
        if (line.prefix.length > 0) {
            buffer.putInRect(rect, row, line.prefix, line.prefixStyle);
        }
        if (line.text.length === 0) {
            continue;
        }
        const textStart = Math.min(line.prefix.length, rect.width);
        buffer.putInRect({ x: rect.x + textStart, y: rect.y + row, width: rect.width - textStart, height: 1 }, 0, line.text, line.style);
    }
}
export function formatElapsed(ms) {
    const safeMs = Number.isFinite(ms) ? ms : 0;
    const totalSeconds = Math.max(0, Math.floor(safeMs / 1_000));
    const hours = Math.floor(totalSeconds / 3_600);
    const minutes = Math.floor((totalSeconds % 3_600) / 60);
    const seconds = totalSeconds % 60;
    return [hours, minutes, seconds].map((value) => value.toString().padStart(2, "0")).join(":");
}
export function formatNumber(n) {
    return new Intl.NumberFormat("en-US").format(n);
}
export function statsToLines(stats, width) {
    if (width <= 0) {
        return [];
    }
    const mutedStyle = getToneStyle("muted");
    const totalTokens = stats.tokensIn + stats.tokensOut;
    const iterationsLabel = stats.iterationsLabel ?? "Iteration";
    const lines = [
        createKeyValueLine("Status", formatStatus(stats.status), width, getStatusStyle(stats.status)),
        createKeyValueLine(iterationsLabel, formatNumber(stats.iterations), width),
        createKeyValueLine("Elapsed", formatElapsed(stats.elapsedMs), width),
        createBlankLine(),
        createKeyValueLine("Tokens In", formatNumber(stats.tokensIn), width),
        createKeyValueLine("Tokens Out", formatNumber(stats.tokensOut), width),
        createKeyValueLine("Total", formatNumber(totalTokens), width)
    ];
    if (stats.currentAction !== undefined) {
        lines.push(createBlankLine(), {
            prefix: clipText("Current:", width),
            prefixStyle: {},
            style: {},
            text: ""
        }, {
            prefix: width > 0 ? clipText("  ", width) : "",
            prefixStyle: mutedStyle,
            style: mutedStyle,
            text: clipText(stats.currentAction, Math.max(width - 2, 0))
        });
    }
    return lines;
}
function createBlankLine() {
    return {
        prefix: "",
        prefixStyle: {},
        style: {},
        text: ""
    };
}
function createKeyValueLine(label, value, width, valueStyle = {}) {
    const clippedValue = clipText(value, width);
    const availableBeforeValue = Math.max(width - clippedValue.length, 0);
    const clippedLabel = clipText(label, Math.max(availableBeforeValue - 1, 0));
    return {
        prefix: clippedLabel + " ".repeat(Math.max(availableBeforeValue - clippedLabel.length, 0)),
        prefixStyle: {},
        style: valueStyle,
        text: clippedValue
    };
}
function clipText(value, width) {
    return width <= 0 ? "" : value.slice(0, width);
}
function formatStatus(status) {
    return `${status.slice(0, 1).toUpperCase()}${status.slice(1)}`;
}
function getStatusStyle(status) {
    if (status === "running") {
        return getToneStyle("info");
    }
    if (status === "paused") {
        return getToneStyle("warning");
    }
    if (status === "error") {
        return getToneStyle("error");
    }
    if (status === "done") {
        return getToneStyle("success");
    }
    return getToneStyle("muted");
}
function getToneStyle(tone) {
    return getTheme().styles[tone];
}
