// Cursor path animation — interpolate cursor across keyframes with easing.
// Usage: const {x, y, clicking} = useCursorPath([{t: 0, x: 100, y: 100}, ...], localTime)

function useCursorPath(keyframes, localTime) {
  // keyframes: [{ t, x, y, click?: boolean }]
  // click fires a quick scale-down at that keyframe
  if (!keyframes.length) return { x: 0, y: 0, clicking: false };

  // Find segment
  let x = keyframes[0].x, y = keyframes[0].y, clicking = false;
  if (localTime <= keyframes[0].t) {
    return { x: keyframes[0].x, y: keyframes[0].y, clicking: false };
  }
  if (localTime >= keyframes[keyframes.length - 1].t) {
    const last = keyframes[keyframes.length - 1];
    return { x: last.x, y: last.y, clicking: false };
  }
  for (let i = 0; i < keyframes.length - 1; i++) {
    const a = keyframes[i], b = keyframes[i + 1];
    if (localTime >= a.t && localTime <= b.t) {
      const span = b.t - a.t;
      const local = span > 0 ? (localTime - a.t) / span : 0;
      const eased = Easing.easeInOutCubic(local);
      x = a.x + (b.x - a.x) * eased;
      y = a.y + (b.y - a.y) * eased;
      // Click flash lasts 180ms after keyframe
      if (b.click && localTime >= b.t - 0.18 && localTime <= b.t + 0.05) {
        clicking = true;
      }
      break;
    }
  }
  return { x, y, clicking };
}

// Simple fade-in helper used across scenes
function fadeIn(localTime, start, dur = 0.4) {
  return clamp((localTime - start) / dur, 0, 1);
}

// Staggered fade for lists — returns opacity 0..1 for item i
function staggered(localTime, startT, stagger, dur, i) {
  return fadeIn(localTime, startT + i * stagger, dur);
}

Object.assign(window, { useCursorPath, fadeIn, staggered });
