"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createVerboseStdoutSink = void 0;
/**
 * Verbose stdout sink. Writes one ASCII line per event when verbose=true.
 * Format: `[oauth-trace] +{elapsedMs}ms {phase} {key=val ...}`
 * No emojis (CCS terminal rule). Goes to stderr to avoid mingling with normal stdout.
 */
function createVerboseStdoutSink(opts) {
    const out = opts.out ?? ((line) => process.stderr.write(line + '\n'));
    return {
        write(event) {
            if (!opts.enabled)
                return;
            const parts = [];
            parts.push(`[oauth-trace] +${event.elapsedMs}ms ${event.phase}`);
            if (event.data) {
                for (const [k, v] of Object.entries(event.data)) {
                    if (v === undefined)
                        continue;
                    parts.push(`${k}=${formatValue(v)}`);
                }
            }
            if (event.error) {
                parts.push(`error_code=${event.error.code ?? 'unknown'}`);
                parts.push(`error_msg="${event.error.message.replace(/"/g, "'")}"`);
            }
            out(parts.join(' '));
        },
    };
}
exports.createVerboseStdoutSink = createVerboseStdoutSink;
function formatValue(v) {
    if (v === null)
        return 'null';
    if (typeof v === 'string')
        return v.includes(' ') ? `"${v.replace(/"/g, "'")}"` : v;
    if (typeof v === 'number' || typeof v === 'boolean')
        return String(v);
    try {
        return JSON.stringify(v);
    }
    catch {
        return '[unserializable]';
    }
}
//# sourceMappingURL=sink-verbose-stdout.js.map