/**
 * LAFS-compliant output formatter for CLEO V2.
 *
 * LAFS (LLM-Agent-First Schema) ensures all CLI output is
 * machine-parseable JSON by default, with optional human-readable modes.
 *
 * All envelopes are now full LAFS-compliant with $schema and _meta.
 * The backward-compatible shape (success + data, no _meta) has been removed.
 *
 * Types are re-exported from the canonical source in src/types/lafs.ts.
 *
 * @epic T4663
 * @task T4672
 */
import { randomUUID } from 'node:crypto';
import { CleoError } from './errors.js';
import { getCurrentSessionId } from './sessions/context-alert.js';
/**
 * Accumulated warnings for the current request.
 * Reset on each formatSuccess/formatError call via drainWarnings().
 *
 * @task T4669
 * @epic T4663
 */
const pendingWarnings = [];
/**
 * Push a deprecation or informational warning into the current envelope.
 * Warnings are drained (consumed) by the next formatSuccess/formatError call.
 *
 * @task T4669
 * @epic T4663
 */
export function pushWarning(warning) {
    pendingWarnings.push(warning);
}
/**
 * Drain all pending warnings (returns and clears the queue).
 *
 * @task T4669
 * @epic T4663
 */
function drainWarnings() {
    if (pendingWarnings.length === 0)
        return undefined;
    const drained = [...pendingWarnings];
    pendingWarnings.length = 0;
    return drained;
}
/**
 * Create a LAFS-conformant _meta object for CLI envelopes.
 * Includes sessionId (T4702) and warnings (T4669) when present.
 *
 * @task T4700
 * @task T4702
 * @epic T4663
 */
function createCliMeta(operation, mvi = 'standard') {
    const warnings = drainWarnings();
    const meta = {
        specVersion: '1.2.3',
        schemaVersion: '2026.2.1',
        timestamp: new Date().toISOString(),
        operation,
        requestId: randomUUID(),
        transport: 'cli',
        strict: true,
        mvi,
        contextVersion: 1,
        ...(warnings && { warnings }),
    };
    const sessionId = getCurrentSessionId();
    if (sessionId) {
        meta.sessionId = sessionId;
    }
    return meta;
}
/**
 * Format a successful result as a full LAFS-conformant envelope.
 *
 * Always produces the full LAFSEnvelope with $schema and _meta.
 * When operation is omitted, defaults to 'cli.output'.
 * Supports optional page (T4668) and _extensions (T4670).
 *
 * @task T4672
 * @task T4668
 * @task T4670
 * @epic T4663
 */
export function formatSuccess(data, message, operationOrOpts) {
    const opts = typeof operationOrOpts === 'string' ? { operation: operationOrOpts } : (operationOrOpts ?? {});
    const envelope = {
        $schema: 'https://lafs.dev/schemas/v1/envelope.schema.json',
        _meta: createCliMeta(opts.operation ?? 'cli.output', opts.mvi),
        success: true,
        result: data,
        ...(message && { message }),
    };
    if (opts.page) {
        envelope['page'] = opts.page;
    }
    if (opts.extensions && Object.keys(opts.extensions).length > 0) {
        envelope['_extensions'] = opts.extensions;
    }
    return JSON.stringify(envelope);
}
/**
 * Format an error as a full LAFS-conformant envelope.
 *
 * Always produces the full LAFSEnvelope with $schema and _meta.
 * When operation is omitted, defaults to 'cli.output'.
 *
 * @task T4672
 * @epic T4663
 */
export function formatError(error, operation) {
    const envelope = {
        $schema: 'https://lafs.dev/schemas/v1/envelope.schema.json',
        _meta: createCliMeta(operation ?? 'cli.output'),
        success: false,
        result: null,
        error: error.toLAFSError(),
    };
    return JSON.stringify(envelope);
}
/** Format any result (success or error) as LAFS JSON. */
export function formatOutput(result) {
    if (result instanceof CleoError) {
        return formatError(result);
    }
    return formatSuccess(result);
}
//# sourceMappingURL=output.js.map