/**
 * Timing Utilities
 *
 * Helpers for converting between milliseconds and frames.
 */

const DEFAULT_FPS = 30;

/**
 * Convert milliseconds to frame count.
 */
export function msToFrames(ms: number, fps: number = DEFAULT_FPS): number {
  return Math.round((ms / 1000) * fps);
}

/**
 * Convert frame count to milliseconds.
 */
export function framesToMs(frames: number, fps: number = DEFAULT_FPS): number {
  return (frames / fps) * 1000;
}

/**
 * Get the frame range for a scene.
 */
export function getSceneFrameRange(
  startMs: number,
  durationMs: number,
  fps: number = DEFAULT_FPS
): { start: number; end: number; duration: number } {
  const start = msToFrames(startMs, fps);
  const duration = msToFrames(durationMs, fps);
  return {
    start,
    end: start + duration,
    duration,
  };
}

/**
 * Check if a frame is within a scene's range.
 */
export function isFrameInScene(
  frame: number,
  startMs: number,
  durationMs: number,
  fps: number = DEFAULT_FPS
): boolean {
  const { start, end } = getSceneFrameRange(startMs, durationMs, fps);
  return frame >= start && frame < end;
}
