/**
 * Frame-rate-independent math helpers for the Logistics Brain scene.
 *
 * NOTE: these are intentionally local `function` declarations rather than
 * importing from `../optimization/math`. Under Turbopack, cross-folder imports
 * of that module's `const`-arrow exports (`lerp`, `clamp01`) resolved to
 * `undefined` at runtime ("lerp is not defined" thrown every frame) even though
 * they type-check and build fine. Keeping the section self-contained avoids it.
 */

export function clamp01(v: number): number {
  return v < 0 ? 0 : v > 1 ? 1 : v;
}

export function lerp(a: number, b: number, t: number): number {
  return a + (b - a) * t;
}

export function smoothstep(edge0: number, edge1: number, x: number): number {
  const t = clamp01((x - edge0) / (edge1 - edge0 || 1));
  return t * t * (3 - 2 * t);
}

/** Exponential damp toward a target — `lambda` ~ responsiveness. */
export function damp(current: number, target: number, lambda: number, dt: number): number {
  return lerp(current, target, 1 - Math.exp(-lambda * dt));
}

/** Deterministic pseudo-random in [0,1) — stable across SSR/render. */
export function seeded(i: number): number {
  const x = Math.sin(i * 127.1 + 311.7) * 43758.5453;
  return x - Math.floor(x);
}
