import { spring, interpolate } from 'remotion';

// Snappy spring — overshoots slightly, settles fast. For entrance "pops".
export function springIn(frame: number, fps: number, delay = 0) {
  return spring({
    frame: frame - delay,
    fps,
    config: { damping: 12, mass: 0.45, stiffness: 110 },
    durationInFrames: 24,
  });
}

// Soft spring — no overshoot, gentle. For settling animations.
export function softSpring(frame: number, fps: number, delay = 0, durationInFrames = 30) {
  return spring({
    frame: frame - delay,
    fps,
    config: { damping: 22, mass: 1, stiffness: 80 },
    durationInFrames,
  });
}

// Linear interpolation clamped to [0, 1] over a frame window.
export function clampLerp(frame: number, start: number, end: number) {
  return interpolate(frame, [start, end], [0, 1], {
    extrapolateLeft: 'clamp',
    extrapolateRight: 'clamp',
  });
}

// Eased-in-out for sweeps. Cubic.
export function easeInOut(t: number) {
  return t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
}

// Deterministic pseudo-random — particles need stable positions across the
// renderer's stateless per-frame calls. Linear congruential, seeded by index.
export function rand(seed: number) {
  const x = Math.sin(seed * 12.9898) * 43758.5453;
  return x - Math.floor(x);
}
