// Copyright (c) 2026 Hellmai Ltd
// SPDX-License-Identifier: AGPL-3.0-only
/**
 * Beautiful CLI Output Formatters
 *
 * WU-1929: Provides branded header, colored status indicators,
 * structured tables, and progress spinners for consistent CLI output.
 *
 * Dependencies: chalk (color), cli-table3 (tables) - both already in package.json.
 * Spinner: lightweight inline implementation (no external dependency).
 *
 * @module formatters
 */
import chalk from 'chalk';
import Table from 'cli-table3';
// ── Constants ───────────────────────────────────────────────────────
const BRAND_NAME = 'LumenFlow';
const HEADER_WIDTH = 50;
const SPINNER_INTERVAL_MS = 80;
const SPINNER_FRAMES = ['|', '/', '-', '\\'];
/**
 * Color mappings for WU status values.
 * Maps each status to a chalk color function name.
 */
export const STATUS_COLORS = {
    ready: chalk.cyan,
    in_progress: chalk.yellow,
    done: chalk.green,
    blocked: chalk.red,
    waiting: chalk.magenta,
};
/** Default color for unknown statuses */
const DEFAULT_STATUS_COLOR = chalk.white;
// ── AC1: Branded Header ─────────────────────────────────────────────
/**
 * Format a branded header string with version information.
 *
 * @param options - Header options
 * @param options.version - CLI version string (optional)
 * @returns Formatted header string
 */
export function formatHeader(options = {}) {
    const separator = chalk.dim('─'.repeat(HEADER_WIDTH));
    const versionStr = options.version ? ` v${options.version}` : '';
    const title = chalk.bold.cyan(`${BRAND_NAME}${versionStr}`);
    const tagline = chalk.dim('The governance layer between AI agents and the world.');
    return [separator, title, tagline, separator].join('\n');
}
/**
 * Print the branded header to console.log.
 *
 * @param options - Header options
 * @param options.version - CLI version string (optional)
 */
export function printHeader(options = {}) {
    console.log(formatHeader(options));
}
// ── AC2: Colored Status Indicators ──────────────────────────────────
/**
 * Apply color to a WU status string based on its value.
 *
 * @param status - The WU status string (e.g., "ready", "in_progress", "done", "blocked")
 * @returns Colored string suitable for terminal output
 */
export function statusColor(status) {
    const colorFn = STATUS_COLORS[status] ?? DEFAULT_STATUS_COLOR;
    return colorFn(status);
}
// ── AC3: Structured Tables ──────────────────────────────────────────
/**
 * Create a formatted table string using cli-table3.
 *
 * @param options - Table configuration
 * @param options.head - Column headers
 * @param options.rows - Array of row arrays
 * @param options.colWidths - Optional column widths
 * @returns Formatted table string
 */
export function createStatusTable(options) {
    const tableConfig = {
        head: options.head,
        style: {
            head: ['cyan'],
        },
    };
    if (options.colWidths) {
        tableConfig.colWidths = options.colWidths;
    }
    const table = new Table(tableConfig);
    for (const row of options.rows) {
        table.push(row);
    }
    return table.toString();
}
/**
 * Create a lightweight terminal spinner for long-running operations.
 * Uses stderr to avoid polluting stdout (which may be piped).
 * No external dependency required.
 *
 * @param message - The message to display next to the spinner
 * @returns Spinner object with start/stop/succeed/fail methods
 */
export function createSpinner(message) {
    let intervalId = null;
    let frameIndex = 0;
    function clearLine() {
        // Move cursor to beginning of line and clear it
        process.stderr.write('\r\x1B[K');
    }
    function renderFrame() {
        const frame = SPINNER_FRAMES[frameIndex % SPINNER_FRAMES.length];
        clearLine();
        process.stderr.write(`${chalk.cyan(frame)} ${message}`);
        frameIndex++;
    }
    return {
        start() {
            if (intervalId !== null)
                return;
            frameIndex = 0;
            renderFrame();
            intervalId = setInterval(renderFrame, SPINNER_INTERVAL_MS);
        },
        stop() {
            if (intervalId !== null) {
                clearInterval(intervalId);
                intervalId = null;
            }
            clearLine();
        },
        succeed(msg) {
            if (intervalId !== null) {
                clearInterval(intervalId);
                intervalId = null;
            }
            clearLine();
            const text = msg ?? message;
            process.stderr.write(`${chalk.green('✓')} ${text}\n`);
        },
        fail(msg) {
            if (intervalId !== null) {
                clearInterval(intervalId);
                intervalId = null;
            }
            clearLine();
            const text = msg ?? message;
            process.stderr.write(`${chalk.red('✗')} ${text}\n`);
        },
    };
}
//# sourceMappingURL=formatters.js.map