const MIN_SWIPE_DISTANCE = 50;
const MIN_SWIPE_VELOCITY = 0.1;

const swipedSignificantDistance = (difference: number) => Math.abs(difference) > MIN_SWIPE_DISTANCE;

const swipedWithSignificantVelocity = (velocity: number) => velocity > MIN_SWIPE_VELOCITY;

export interface Swipe {
  x: number;
  y: number;
  time: number;
}

export const getSwipeVelocity = (start: Swipe, end: Swipe) => {
  const timePassed = end.time - start.time;

  return getSwipeDifference(start, end) / timePassed;
};

export const getSwipeDifference = (start: Swipe, end: Swipe, axis: 'x' | 'y' = 'x') => {
  return Math.abs(start[axis] - end[axis]);
};

export const swipedLeftToRight = (start: Swipe, end: Swipe) => {
  return end.x > start.x;
};

export const swipedRightToLeft = (start: Swipe, end: Swipe) => {
  return start.x > end.x;
};

export const swipeShouldChangeTab = (start: Swipe, end: Swipe) => {
  const difference = getSwipeDifference(start, end);
  const velocity = getSwipeVelocity(start, end);

  return swipedSignificantDistance(difference) && swipedWithSignificantVelocity(velocity);
};
