export const PERFORMANCE_PROBE_ENABLED = typeof window !== "undefined"
  && new URLSearchParams(window.location.search).has("perf");

const MAX_TICK_SAMPLES = 300;
const simulationTickSamples: number[] = [];

export function recordSimulationTick(durationMs: number) {
  if (!PERFORMANCE_PROBE_ENABLED) return;
  if (simulationTickSamples.length === MAX_TICK_SAMPLES) simulationTickSamples.shift();
  simulationTickSamples.push(durationMs);
}

export function simulationTickSnapshot() {
  if (!simulationTickSamples.length) return { averageMs: 0, maximumMs: 0, samples: 0 };
  let total = 0;
  let maximumMs = 0;
  for (const duration of simulationTickSamples) {
    total += duration;
    if (duration > maximumMs) maximumMs = duration;
  }
  return {
    averageMs: total / simulationTickSamples.length,
    maximumMs,
    samples: simulationTickSamples.length,
  };
}
