/**
 * 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 timings = [];
let lastTime = Date.now();
let resetTime = lastTime;
export function isTimingEnabled() {
    return ENABLED;
}
export function resetTimings() {
    if (!ENABLED)
        return;
    timings.length = 0;
    lastTime = Date.now();
    resetTime = lastTime;
}
export function time(label) {
    if (!ENABLED)
        return;
    const now = Date.now();
    timings.push({ label, ms: now - lastTime });
    lastTime = now;
}
export function startTimingSpan(label) {
    if (!ENABLED)
        return null;
    return { label, start: Date.now() };
}
export function endTimingSpan(span) {
    if (!ENABLED || !span)
        return;
    const now = Date.now();
    timings.push({ label: span.label, ms: now - span.start });
    lastTime = now;
}
export function recordTiming(label, ms) {
    if (!ENABLED)
        return;
    timings.push({ label, ms });
}
export function recordTimeSinceReset(label) {
    if (!ENABLED)
        return;
    timings.push({ label, ms: Date.now() - resetTime });
}
export function printTimings() {
    if (!ENABLED || timings.length === 0)
        return;
    console.error("\n--- Startup Timings ---");
    for (const t of timings) {
        console.error(`  ${t.label}: ${t.ms}ms`);
    }
    console.error(`  TOTAL initialization time: ${Date.now() - resetTime}ms`);
    console.error("------------------------\n");
}
//# sourceMappingURL=timings.js.map