// engine/timing/structured-log.ts — Structured timing log helper (SPEC-713)
// Emits JSON {step, durationMs} entries to stderr for Phase 0 diagnostic.
// Zero production overhead: only active when PLANU_TIMING=1 env var is set.
const TIMING_ENABLED = process.env.PLANU_TIMING === '1';
/**
 * Emit a single timing record to stderr in JSON format.
 * Only active when PLANU_TIMING=1.
 */
export function emitTiming(record) {
    if (!TIMING_ENABLED) {
        return;
    }
    process.stderr.write(JSON.stringify(record) + '\n');
}
/**
 * Measure elapsed time for an async operation and emit a timing record.
 * The operation result is returned unchanged.
 * Only adds overhead when PLANU_TIMING=1.
 */
export async function measureStep(step, fn) {
    if (!TIMING_ENABLED) {
        return fn();
    }
    const start = performance.now();
    try {
        return await fn();
    }
    finally {
        const durationMs = Math.round(performance.now() - start);
        emitTiming({ step, durationMs, ts: new Date().toISOString() });
    }
}
/**
 * Synchronous variant of measureStep.
 */
export function measureStepSync(step, fn) {
    if (!TIMING_ENABLED) {
        return fn();
    }
    const start = performance.now();
    try {
        return fn();
    }
    finally {
        const durationMs = Math.round(performance.now() - start);
        emitTiming({ step, durationMs, ts: new Date().toISOString() });
    }
}
//# sourceMappingURL=structured-log.js.map