/**
 * Central timing instrumentation for startup profiling.
 * Enable with the app-specific timing environment variable (for Atomic, ATOMIC_TIMING=1).
 */
import { ENV_TIMING, getEnvValue } from "../config.js";
const ENABLED = getEnvValue(ENV_TIMING) === "1";
const timingNamespaces = new Map();
export function isTimingEnabled() {
    return ENABLED;
}
function ensureNamespace(namespace) {
    let entry = timingNamespaces.get(namespace);
    if (!entry) {
        const now = Date.now();
        entry = { timings: [], lastTime: now, resetTime: now };
        timingNamespaces.set(namespace, entry);
    }
    return entry;
}
export function resetTimings(namespace = "main") {
    if (!ENABLED)
        return;
    const now = Date.now();
    timingNamespaces.set(namespace, { timings: [], lastTime: now, resetTime: now });
}
export function time(label, namespace = "main") {
    if (!ENABLED)
        return;
    const now = Date.now();
    const timingNamespace = ensureNamespace(namespace);
    timingNamespace.timings.push({ label, ms: now - timingNamespace.lastTime });
    timingNamespace.lastTime = now;
}
export function startTimingSpan(label, namespace = "main") {
    if (!ENABLED)
        return null;
    return { label, start: Date.now(), namespace };
}
export function endTimingSpan(span) {
    if (!ENABLED || !span)
        return;
    const now = Date.now();
    const timingNamespace = ensureNamespace(span.namespace);
    timingNamespace.timings.push({ label: span.label, ms: now - span.start });
    timingNamespace.lastTime = now;
}
export function recordTiming(label, ms, namespace = "main") {
    if (!ENABLED)
        return;
    const timingNamespace = ensureNamespace(namespace);
    timingNamespace.timings.push({ label, ms });
}
export function recordTimeSinceReset(label, namespace = "main") {
    if (!ENABLED)
        return;
    const timingNamespace = ensureNamespace(namespace);
    timingNamespace.timings.push({ label, ms: Date.now() - timingNamespace.resetTime });
}
function printTimingGroup(title, namespace) {
    const printableTimings = namespace.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 initialization time: ${Date.now() - namespace.resetTime}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);
    }
}
//# sourceMappingURL=timings.js.map