/**
 * Central timing instrumentation for startup profiling.
 * Enable with PI_TIMING=1 environment variable.
 */
const ENABLED = process.env.PI_TIMING === "1";
const timingNamespaces = new Map();
export function resetTimings(namespace = "main") {
    if (!ENABLED)
        return;
    timingNamespaces.set(namespace, { timings: [], lastTime: Date.now() });
}
export function time(label, namespace = "main") {
    if (!ENABLED)
        return;
    const now = Date.now();
    if (!timingNamespaces.has(namespace)) {
        resetTimings(namespace);
    }
    const timingNamespace = timingNamespaces.get(namespace);
    timingNamespace.timings.push({ label, ms: now - timingNamespace.lastTime });
    timingNamespace.lastTime = now;
}
function printTimingGroup(title, timings) {
    const printableTimings = timings.filter((timing) => timing.ms >= 0);
    if (printableTimings.length === 0)
        return;
    console.error(`\n--- ${title} ---`);
    for (const t of printableTimings) {
        console.error(`  ${t.label}: ${t.ms}ms`);
    }
    console.error(`  TOTAL: ${printableTimings.reduce((a, b) => a + b.ms, 0)}ms`);
    console.error(`${"-".repeat(title.length + 8)}\n`);
}
export function printTimings() {
    if (!ENABLED)
        return;
    for (const [namespace, timingNamespace] of timingNamespaces) {
        printTimingGroup(`Startup Timings: ${namespace}`, timingNamespace.timings);
    }
}
//# sourceMappingURL=timings.js.map