"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Dismissable = void 0;
var _reactNative = require("react-native");
var _reactNativeGestureHandler = require("react-native-gesture-handler");
var _reactNativeReanimated = _interopRequireWildcard(require("react-native-reanimated"));
var _reactNativeWorklets = require("react-native-worklets");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const windowWidth = _reactNative.Dimensions.get("window").width;
/**
 * Component that allows for a dismissable gesture, both left and right.
 * When the threshold is reached, the `onDismiss` callback is called.
 * @param onDismiss Callback to be called when the threshold is reached.
 * @param dismissThreshold Threshold to be reached to call the `onDismiss` callback.
 * @param children Children to be rendered inside the component.
 * @returns A dismissable component.
 */
const Dismissable = ({
  onDismiss = () => undefined,
  dismissThreshold = windowWidth / 3,
  children,
  testID
}) => {
  const translateX = (0, _reactNativeReanimated.useSharedValue)(0);
  const animatedStyle = (0, _reactNativeReanimated.useAnimatedStyle)(() => ({
    transform: [{
      translateX: translateX.value
    }]
  }));
  const pan = _reactNativeGestureHandler.Gesture.Pan().onUpdate(event => {
    // eslint-disable-next-line functional/immutable-data
    translateX.value = event.translationX;
  }).onEnd(event => {
    if (Math.abs(event.translationX) > dismissThreshold) {
      // eslint-disable-next-line functional/immutable-data
      translateX.value = (0, _reactNativeReanimated.withTiming)(windowWidth * Math.sign(event.translationX), {
        duration: 300,
        easing: _reactNativeReanimated.Easing.inOut(_reactNativeReanimated.Easing.exp)
      }, () => {
        "worklet";

        (0, _reactNativeWorklets.scheduleOnRN)(onDismiss);
      });
    } else {
      // eslint-disable-next-line functional/immutable-data
      translateX.value = (0, _reactNativeReanimated.withSpring)(0, {
        mass: 0.5
      });
    }
  }).withTestId(testID ?? "");
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeGestureHandler.GestureDetector, {
    gesture: pan,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeReanimated.default.View, {
      style: animatedStyle,
      children: children
    })
  });
};
exports.Dismissable = Dismissable;
//# sourceMappingURL=Dismissable.js.map