/*!
 * Copyright (C) Microsoft Corporation. All rights reserved.
 */
import chalk from 'chalk';
// Suppress decorative separator lines when output is piped or redirected (non-interactive).
// This applies to both shell pipelines and LLM agents that invoke CLI commands programmatically —
// in both cases stdout is not a TTY, and box-drawing characters would pollute the parseable output.
function isTTY() {
    return process.stdout.isTTY ?? false;
}
/**
 * Renders a titled table with optional metadata, column headers, and data rows.
 * Separator lines are suppressed in non-TTY mode (piped output, LLM agents).
 */
export function printTable(options) {
    const { title, metadata, columns, rows, emptyMessage, totalLabel, width = 80 } = options;
    const line = isTTY() ? chalk.dim('-'.repeat(width)) : null;
    console.log('');
    console.log(chalk.cyan.bold(`  ${title}`));
    if (metadata) {
        if (line)
            console.log(line);
        for (const meta of metadata) {
            console.log(chalk.white(`  ${meta}`));
        }
    }
    if (rows.length === 0) {
        if (emptyMessage) {
            if (line)
                console.log(line);
            console.log(`  ${emptyMessage}`);
        }
        return;
    }
    // Column width = max(header.length, longest effective cell length)
    // Effective cell length respects maxWidth (truncated value is always maxWidth chars)
    const colWidths = columns.map((col, i) => {
        const maxCellLen = rows.reduce((max, row) => {
            const cell = row[i] ?? '';
            const len = col.maxWidth !== undefined && cell.length > col.maxWidth ? col.maxWidth : cell.length;
            return Math.max(max, len);
        }, 0);
        return Math.max(col.header.length, maxCellLen);
    });
    if (line)
        console.log(line);
    const headerParts = columns.map((col, i) => i === columns.length - 1 ? col.header : col.header.padEnd(colWidths[i]));
    console.log(chalk.cyan(`  ${headerParts.join('  ')}`));
    if (line)
        console.log(line);
    for (const row of rows) {
        const rowParts = columns.map((col, i) => {
            let cell = row[i] ?? '';
            if (col.maxWidth !== undefined && cell.length > col.maxWidth) {
                if (col.maxWidth <= 3) {
                    cell = cell.slice(0, col.maxWidth);
                }
                else {
                    cell = cell.slice(0, col.maxWidth - 3) + '...';
                }
            }
            return i === columns.length - 1 ? cell : cell.padEnd(colWidths[i]);
        });
        console.log(`  ${rowParts.join('  ')}`);
    }
    if (line)
        console.log(line);
    if (totalLabel !== undefined) {
        console.log(chalk.dim(`  Total: ${rows.length} ${totalLabel}`));
    }
}
/**
 * Renders a titled key-value list where each item is displayed as a set of labeled fields.
 * Multiple items are separated by a divider line. Separator lines are suppressed in non-TTY mode.
 */
export function printKeyValueList(options) {
    const { title, items, fields, emptyMessage, width = 80 } = options;
    const line = isTTY() ? chalk.dim('-'.repeat(width)) : null;
    console.log('');
    console.log(chalk.cyan.bold(`  ${title}`));
    if (line)
        console.log(line);
    if (items.length === 0) {
        if (emptyMessage) {
            console.log(`  ${emptyMessage}`);
        }
        return;
    }
    for (let i = 0; i < items.length; i++) {
        for (const field of fields) {
            console.log(`  ${chalk.white(field.label)}: ${field.value(items[i])}`);
        }
        // Separator between items only — no trailing separator after the last item (intentional).
        if (i < items.length - 1 && line) {
            console.log(line);
        }
    }
}
export function printSuccess(message) {
    console.log(chalk.green(message));
}
export function printInfo(message) {
    console.log(message);
}
export function printWarning(message) {
    console.warn(chalk.yellow(message));
}
export function printError(message) {
    console.error(chalk.red(message));
}
//# sourceMappingURL=ConsoleOutput.js.map