/**
 * SAC output formatting
 */
const SPINNER_FRAMES = [
    "\u280B",
    "\u2819",
    "\u2839",
    "\u2838",
    "\u283C",
    "\u2834",
    "\u2826",
    "\u2827",
    "\u2807",
    "\u280F",
];
export function createSpinner(label) {
    const isTTY = process.stderr.isTTY;
    let frame = 0;
    let interval = null;
    let currentLabel = label;
    return {
        start() {
            if (!isTTY)
                return;
            interval = setInterval(() => {
                process.stderr.write(`\r${SPINNER_FRAMES[frame % SPINNER_FRAMES.length]} ${currentLabel}`);
                frame++;
            }, 80);
        },
        update(text) {
            currentLabel = text;
        },
        stop(finalText) {
            if (interval) {
                clearInterval(interval);
                interval = null;
            }
            if (isTTY) {
                process.stderr.write("\r\x1b[K");
                if (finalText) {
                    process.stderr.write(`${finalText}\n`);
                }
            }
        },
    };
}
export function createProgressBar(total, label = "") {
    const isTTY = process.stderr.isTTY;
    const width = 30;
    return {
        update(current) {
            if (!isTTY)
                return;
            const pct = Math.min(1, current / total);
            const filled = Math.round(width * pct);
            const empty = width - filled;
            const bar = "\u2588".repeat(filled) + "\u2591".repeat(empty);
            const pctStr = `${Math.round(pct * 100)}%`;
            process.stderr.write(`\r${label} ${bar} ${pctStr}`);
        },
        finish() {
            if (isTTY)
                process.stderr.write("\n");
        },
    };
}
export function detectOutputFormat(flagValue) {
    if (flagValue === "json" || flagValue === "text")
        return flagValue;
    if (!process.stdout.isTTY)
        return "json";
    return "text";
}
export function formatJson(data) {
    return JSON.stringify(data, null, 2);
}
export function formatKeyValue(obj, indent = 0) {
    const prefix = " ".repeat(indent);
    const lines = [];
    for (const [key, value] of Object.entries(obj)) {
        if (value === null || value === undefined)
            continue;
        if (Array.isArray(value)) {
            lines.push(`${prefix}${key}:`);
            for (const item of value) {
                if (typeof item === "object" && item !== null) {
                    lines.push(`${prefix}  - ${formatKeyValue(item, indent + 4).trimStart()}`);
                }
                else {
                    lines.push(`${prefix}  - ${String(item)}`);
                }
            }
        }
        else if (typeof value === "object") {
            lines.push(`${prefix}${key}:`);
            lines.push(formatKeyValue(value, indent + 2));
        }
        else {
            lines.push(`${prefix}${key}: ${String(value)}`);
        }
    }
    return lines.join("\n");
}
export function formatText(data) {
    if (data === null || data === undefined)
        return "";
    if (typeof data === "string")
        return data;
    if (typeof data === "number" || typeof data === "boolean")
        return String(data);
    if (Array.isArray(data)) {
        if (data.length === 0)
            return "(empty)";
        if (typeof data[0] === "object" && data[0] !== null) {
            return formatTable(data);
        }
        return data.map(String).join("\n");
    }
    if (typeof data === "object") {
        return formatKeyValue(data);
    }
    return String(data);
}
export function formatTable(rows) {
    if (rows.length === 0)
        return "(empty)";
    const keys = Object.keys(rows[0]);
    const widths = keys.map((k) => Math.max(k.length, ...rows.map((r) => String(r[k] ?? "").length)));
    const header = keys.map((k, i) => k.toUpperCase().padEnd(widths[i])).join("  ");
    const separator = widths.map((w) => "-".repeat(w)).join("  ");
    const body = rows.map((r) => keys.map((k, i) => String(r[k] ?? "").padEnd(widths[i])).join("  "));
    return [header, separator, ...body].join("\n");
}
export function formatOutput(data, format) {
    switch (format) {
        case "json":
            return formatJson(data);
        case "text":
            return formatText(data);
    }
}
//# sourceMappingURL=output.js.map