import {BottomSheetProps} from '@gorhom/bottom-sheet';
import {windowDimensions} from '@shared/constants';
import {isIos} from '@shared/utils';

const getRatioFromSnapPoint = (percentage: string) =>
  parseInt(percentage.replace(/\D/g, ''), 10) / 100;

const getSnapPointValue = (snapPoint: string | number) =>
  typeof snapPoint === 'number'
    ? snapPoint
    : windowDimensions.height * getRatioFromSnapPoint(snapPoint);

export const getMaxSnapPoint = (snapPoints: BottomSheetProps['snapPoints']) => {
  const snaps = 'value' in snapPoints! ? snapPoints.value : snapPoints;
  const lastSnapPointValue = getSnapPointValue(snaps![snaps!.length - 1]);
  const firstSnapPointValue = getSnapPointValue(snaps![0]);
  return isIos
    ? lastSnapPointValue
    : (lastSnapPointValue + firstSnapPointValue) / 2;
};
