export type SwipeDirection = 'left' | 'right';

export type DetectHorizontalSwipeInput = {
  startX: number;
  startY: number;
  endX: number;
  endY: number;
  elapsedMs: number;
  minDistancePx?: number;
  maxDurationMs?: number;
  minHorizontalDominanceRatio?: number;
};

export const detectHorizontalSwipe = (input: DetectHorizontalSwipeInput): SwipeDirection | null => {
  const minDistancePx = input.minDistancePx ?? 60;
  const maxDurationMs = input.maxDurationMs ?? 600;
  const minHorizontalDominanceRatio = input.minHorizontalDominanceRatio ?? 1.2;

  if (!Number.isFinite(input.startX) || !Number.isFinite(input.startY)) return null;
  if (!Number.isFinite(input.endX) || !Number.isFinite(input.endY)) return null;
  if (!Number.isFinite(input.elapsedMs)) return null;
  if (input.elapsedMs < 0) return null;
  if (input.elapsedMs > maxDurationMs) return null;

  const dx = input.endX - input.startX;
  const dy = input.endY - input.startY;
  const absDx = Math.abs(dx);
  const absDy = Math.abs(dy);

  if (absDx < minDistancePx) return null;
  if (absDx < absDy * minHorizontalDominanceRatio) return null;

  return dx < 0 ? 'left' : 'right';
};

