"use strict";

import React, { useCallback, useContext, useEffect, useRef } from 'react';
import { Animated, Easing, KeyboardAvoidingView, Platform, Pressable, StyleSheet } from 'react-native';
import { ApplicationContext } from "../Context/index.js";
import { Colors, Spacing, Styles } from "../Consts/index.js";
import BottomSheet from "./BottomSheet.js";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const ModalScreen = props => {
  useEffect(() => {
    return () => {
      props.route.params?.onDismiss?.();
    };
  }, [props.route.params]);
  if (props.route?.params?.isBottomSheet) {
    return /*#__PURE__*/_jsx(BottomSheet, {
      ...props
    });
  }
  return /*#__PURE__*/_jsx(Modal, {
    ...props
  });
};
const Modal = ({
  navigation,
  route
}) => {
  const {
    navigator
  } = useContext(ApplicationContext);
  const {
    screen: Component,
    barrierDismissible,
    modalStyle
  } = route.params;
  const opacity = useRef(new Animated.Value(0));
  const scale = useRef(new Animated.Value(0.8));
  const params = {
    ...route.params,
    navigation
  };
  delete params.screen;
  const onDismiss = useCallback((force = false, callback) => {
    if (barrierDismissible && !force) {
      return;
    }
    Animated.parallel([Animated.timing(opacity.current, {
      toValue: 0,
      duration: 200,
      useNativeDriver: true
    }), Animated.timing(scale.current, {
      toValue: 0.8,
      duration: 200,
      easing: Easing.linear,
      useNativeDriver: true
    })]).start(() => {
      callback?.();
      navigator?.pop();
    });
  }, [barrierDismissible, navigator]);
  const onRequestClose = useCallback(callback => {
    onDismiss(true, callback);
  }, [onDismiss]);
  useEffect(() => {
    Animated.parallel([Animated.timing(opacity.current, {
      toValue: 1,
      duration: 250,
      useNativeDriver: true
    }), Animated.timing(scale.current, {
      toValue: 1,
      duration: 250,
      easing: Easing.bezier(0.2, 0.0, 0, 1.0),
      useNativeDriver: true
    })]).start();
  }, []);
  return /*#__PURE__*/_jsxs(KeyboardAvoidingView, {
    style: Styles.flexCenter,
    behavior: Platform.OS === 'ios' ? 'padding' : 'height',
    children: [/*#__PURE__*/_jsx(Pressable, {
      style: StyleSheet.absoluteFillObject,
      onPress: () => onDismiss(),
      children: /*#__PURE__*/_jsx(Animated.View, {
        style: [styles.overlayContent, {
          opacity: opacity.current
        }]
      })
    }), /*#__PURE__*/_jsx(Animated.View, {
      style: [{
        opacity: opacity.current,
        transform: [{
          scale: scale.current
        }],
        padding: Spacing.XL
      }, modalStyle],
      children: /*#__PURE__*/_jsx(Component, {
        ...params,
        onRequestClose: onRequestClose
      })
    })]
  });
};
export default ModalScreen;
const styles = StyleSheet.create({
  overlayContent: {
    flex: 1,
    backgroundColor: Colors.modal
  }
});
//# sourceMappingURL=ModalScreen.js.map