import * as React from 'react';
import { Animated, BackHandler, Easing, StyleSheet, TouchableWithoutFeedback, View } from 'react-native';
import color from 'color';
// import {
//   getStatusBarHeight,
//   getBottomSpace,
// } from 'react-native-iphone-x-helper';
import Surface from '../Surface';
import { useTheme } from '../../core/theming';
import useAnimatedValue from '../../utils/useAnimatedValue';
import { addEventListener } from '../../utils/addEventListener';
import { colors } from '../../styles/tokens';
const DEFAULT_DURATION = 220;
// const TOP_INSET = getStatusBarHeight(true);
// const BOTTOM_INSET = getBottomSpace();

export default function Modal(_ref) {
  let {
    dismissable = true,
    visible = false,
    overlayAccessibilityLabel = 'Close modal',
    onDismiss,
    children,
    contentContainerStyle,
    style
  } = _ref;
  const visibleRef = React.useRef(visible);
  React.useEffect(() => {
    visibleRef.current = visible;
  });
  const theme = useTheme();
  const {
    scale
  } = theme.animation;
  const opacity = useAnimatedValue(visible ? 1 : 0);
  const [rendered, setRendered] = React.useState(visible);
  if (visible && !rendered) {
    setRendered(true);
  }
  const handleBack = () => {
    if (dismissable) {
      hideModal();
    }
    return true;
  };
  const subscription = React.useRef(undefined);
  const showModal = () => {
    var _subscription$current;
    (_subscription$current = subscription.current) === null || _subscription$current === void 0 ? void 0 : _subscription$current.remove();
    subscription.current = addEventListener(BackHandler, 'hardwareBackPress', handleBack);
    Animated.timing(opacity, {
      toValue: 1,
      duration: scale * DEFAULT_DURATION,
      easing: Easing.out(Easing.cubic),
      useNativeDriver: true
    }).start();
  };
  const removeListeners = () => {
    var _subscription$current2;
    if ((_subscription$current2 = subscription.current) !== null && _subscription$current2 !== void 0 && _subscription$current2.remove) {
      var _subscription$current3;
      (_subscription$current3 = subscription.current) === null || _subscription$current3 === void 0 ? void 0 : _subscription$current3.remove();
    } else {
      BackHandler.removeEventListener('hardwareBackPress', handleBack);
    }
  };
  const hideModal = () => {
    console.log('hideModal');
    removeListeners();
    Animated.timing(opacity, {
      toValue: 0,
      duration: scale * DEFAULT_DURATION,
      easing: Easing.out(Easing.cubic),
      useNativeDriver: true
    }).start(_ref2 => {
      let {
        finished
      } = _ref2;
      if (!finished) {
        return;
      }
      if (visible && onDismiss) {
        onDismiss();
      }
      if (visibleRef.current) {
        showModal();
      } else {
        setRendered(false);
      }
    });
  };
  const prevVisible = React.useRef(null);
  React.useEffect(() => {
    if (prevVisible.current !== visible) {
      if (visible) {
        showModal();
      } else {
        hideModal();
      }
    }
    prevVisible.current = visible;
  });
  React.useEffect(() => {
    return removeListeners;
  }, []);
  if (!rendered) return null;
  return /*#__PURE__*/React.createElement(Animated.View, {
    pointerEvents: visible ? 'auto' : 'none',
    accessibilityViewIsModal: true,
    accessibilityLiveRegion: "polite",
    style: StyleSheet.absoluteFill,
    onAccessibilityEscape: hideModal
  }, /*#__PURE__*/React.createElement(TouchableWithoutFeedback, {
    accessibilityLabel: overlayAccessibilityLabel,
    accessibilityRole: "button",
    disabled: !dismissable,
    onPress: dismissable ? hideModal : undefined,
    importantForAccessibility: "no"
  }, /*#__PURE__*/React.createElement(Animated.View, {
    style: [styles.backdrop, {
      backgroundColor: color(colors.black).alpha(0.4).rgb().string(),
      opacity
    }]
  })), /*#__PURE__*/React.createElement(View, {
    style: [styles.wrapper,
    //   { marginTop: TOP_INSET, marginBottom: BOTTOM_INSET },
    style, {
      marginHorizontal: 16
    }],
    pointerEvents: "box-none"
  }, /*#__PURE__*/React.createElement(Surface, {
    style: [{
      opacity
    }, styles.content, contentContainerStyle]
  }, children)));
}
const styles = StyleSheet.create({
  backdrop: {
    flex: 1
  },
  wrapper: {
    ...StyleSheet.absoluteFillObject,
    justifyContent: 'center',
    alignItems: 'center'
  },
  content: {
    backgroundColor: 'transparent',
    justifyContent: 'center'
  }
});
//# sourceMappingURL=Modal.js.map