"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.printTable = printTable;
exports.printJson = printJson;
exports.printIds = printIds;
exports.formatTime = formatTime;
exports.formatStatus = formatStatus;
exports.output = output;
const picocolors_1 = __importDefault(require("picocolors"));
/** Print a formatted table to stdout */
function printTable(rows, columns) {
    if (rows.length === 0) {
        console.log(picocolors_1.default.dim('\n  No results.\n'));
        return;
    }
    // Calculate column widths
    const widths = columns.map((col) => {
        const headerLen = col.label.length;
        const maxDataLen = rows.reduce((max, row) => {
            const val = col.format ? col.format(row[col.key]) : String(row[col.key] ?? '');
            return Math.max(max, val.length);
        }, 0);
        return col.width || Math.min(Math.max(headerLen, maxDataLen) + 2, 40);
    });
    // Header
    const header = columns.map((col, i) => {
        const text = col.label.toUpperCase();
        return col.align === 'right' ? text.padStart(widths[i]) : text.padEnd(widths[i]);
    }).join('  ');
    console.log(`\n  ${picocolors_1.default.dim(header)}`);
    // Rows
    for (const row of rows) {
        const line = columns.map((col, i) => {
            const raw = row[col.key];
            const text = col.format ? col.format(raw) : String(raw ?? '');
            const truncated = text.length > widths[i] ? text.slice(0, widths[i] - 1) + '…' : text;
            return col.align === 'right' ? truncated.padStart(widths[i]) : truncated.padEnd(widths[i]);
        }).join('  ');
        console.log(`  ${line}`);
    }
    console.log();
}
/** Print JSON to stdout (for piping) */
function printJson(data) {
    console.log(JSON.stringify(data, null, 2));
}
/** Print IDs only, one per line (quiet mode) */
function printIds(rows, idKey = 'id') {
    for (const row of rows) {
        console.log(row[idKey]);
    }
}
/** Format a timestamp for table display */
function formatTime(iso) {
    if (!iso)
        return '—';
    const d = new Date(iso);
    const now = Date.now();
    const diff = now - d.getTime();
    if (diff < 60_000)
        return 'just now';
    if (diff < 3600_000)
        return `${Math.floor(diff / 60_000)}m ago`;
    if (diff < 86400_000)
        return `${Math.floor(diff / 3600_000)}h ago`;
    return d.toLocaleDateString();
}
/** Format a status with color */
function formatStatus(status) {
    switch (status) {
        case 'pending': return picocolors_1.default.yellow(status);
        case 'claimed': return picocolors_1.default.blue(status);
        case 'resolved': return picocolors_1.default.green(status);
        case 'active': return picocolors_1.default.green(status);
        case 'draft': return picocolors_1.default.dim(status);
        case 'deployed': return picocolors_1.default.cyan(status);
        case 'archived': return picocolors_1.default.dim(status);
        case 'failed': return picocolors_1.default.red(status);
        case 'running': return picocolors_1.default.blue(status);
        case 'completed': return picocolors_1.default.green(status);
        default: return status;
    }
}
/** Standard output handler — table, json, or quiet based on flags */
function output(data, rows, columns, opts, idKey = 'id') {
    if (opts.json)
        return printJson(data);
    if (opts.quiet)
        return printIds(rows, idKey);
    printTable(rows, columns);
}
