// cli/formatter.ts — Convert MCP ToolResult to terminal output (SPEC-124)
/**
 * Format a ToolResult for terminal display.
 * Parses JSON text content when possible, otherwise returns raw text.
 */
export function formatToolResult(result, options) {
    const parts = [];
    for (const item of result.content) {
        if (options?.json) {
            parts.push(item.text);
            continue;
        }
        // Try to parse as JSON for prettier output
        try {
            const parsed = JSON.parse(item.text);
            if (typeof parsed === 'object' && parsed !== null) {
                parts.push(formatObject(parsed));
            }
            else {
                parts.push(String(parsed));
            }
        }
        catch {
            parts.push(item.text);
        }
    }
    return parts.join('\n');
}
function formatObject(obj, indent = 0) {
    const prefix = ' '.repeat(indent);
    const lines = [];
    for (const [key, value] of Object.entries(obj)) {
        if (Array.isArray(value)) {
            lines.push(`${prefix}${key}:`);
            for (const item of value) {
                if (typeof item === 'object' && item !== null) {
                    lines.push(formatObject(item, indent + 2));
                }
                else {
                    lines.push(`${prefix}  - ${String(item)}`);
                }
            }
        }
        else if (typeof value === 'object' && value !== null) {
            lines.push(`${prefix}${key}:`);
            lines.push(formatObject(value, indent + 2));
        }
        else {
            lines.push(`${prefix}${key}: ${String(value)}`);
        }
    }
    return lines.join('\n');
}
/**
 * Render an ASCII table with column alignment.
 */
export function table(headers, rows) {
    const widths = headers.map((h, i) => {
        const maxRow = rows.reduce((max, row) => Math.max(max, (row[i] ?? '').length), 0);
        return Math.max(h.length, maxRow);
    });
    const sep = widths.map((w) => '-'.repeat(w + 2)).join('+');
    const formatRow = (row) => row.map((cell, i) => ` ${cell.padEnd(widths[i])} `).join('|');
    const lines = [formatRow(headers), sep, ...rows.map(formatRow)];
    return lines.join('\n');
}
/**
 * Render a progress bar.
 * Example: [--------] 67%
 */
export function progressBar(current, total, width = 20) {
    const ratio = total > 0 ? Math.min(current / total, 1) : 0;
    const filled = Math.round(ratio * width);
    const empty = width - filled;
    const pct = Math.round(ratio * 100);
    return `[${'█'.repeat(filled)}${'░'.repeat(empty)}] ${String(pct)}%`;
}
//# sourceMappingURL=formatter.js.map