import {memo, PropsWithChildren} from 'react';
import {Gesture, GestureDetector} from 'react-native-gesture-handler';
import {runOnJS} from 'react-native-reanimated';

import {GestureEvent} from './types';

const GestureContainer = memo(
  ({
    children,
    onGesture,
  }: PropsWithChildren<{onGesture: (e: GestureEvent) => void}>) => {
    const gesture = Gesture.Exclusive(
      Gesture.LongPress()
        .minDuration(220)
        .onStart(e => {
          runOnJS(onGesture)(e);
        }),
      Gesture.Tap()
        .numberOfTaps(2)
        .onEnd(e => {
          runOnJS(onGesture)(e);
        }),
    );

    return <GestureDetector gesture={gesture}>{children}</GestureDetector>;
  },
);

export default GestureContainer;
