"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _reactNative = require("react-native");

var _portal = require("@gorhom/portal");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const DEFAULT_LAYOUT = {
  width: 0,
  height: 0,
  x: 0,
  y: 0
};

const Popup = ({
  debug,
  isOpen,
  position,
  horizontalSpacing,
  verticalSpacing,
  children,
  safeAreaInsets,
  overlay,
  onClose
}) => {
  const dimensions = (0, _reactNative.useWindowDimensions)();
  const anchorRef = (0, _react.useRef)(null);
  const contentRef = (0, _react.useRef)(null);
  const [anchorLayout, setAnchorLayout] = (0, _react.useState)(DEFAULT_LAYOUT);
  const [contentLayout, setContentLayout] = (0, _react.useState)(DEFAULT_LAYOUT);
  const [computedPosition, setComputedPosition] = (0, _react.useState)(position);
  const handleAnchorLayout = (0, _react.useCallback)(() => {
    if (anchorRef.current) {
      anchorRef.current.measureInWindow((x, y, width, height) => {
        setAnchorLayout({
          x,
          y,
          width,
          height
        });
      });
    }
  }, [anchorRef]);
  const handleContentLayout = (0, _react.useCallback)(() => {
    if (contentRef.current) {
      contentRef.current.measureInWindow((x, y, width, height) => {
        setContentLayout({
          x,
          y,
          width,
          height
        });
      });
    }
  }, [contentRef]);
  (0, _react.useEffect)(() => {
    let timer;

    const recalculateAnchorLayout = () => {
      timer = setTimeout(handleAnchorLayout, 100);
    };

    _reactNative.Dimensions.addEventListener('change', recalculateAnchorLayout);

    return () => {
      _reactNative.Dimensions.removeEventListener('change', recalculateAnchorLayout);

      clearTimeout(timer);
    };
  }, [handleAnchorLayout]);
  (0, _react.useEffect)(() => {
    if (isOpen && anchorRef.current) {
      handleAnchorLayout();
    }
  }, [isOpen, handleAnchorLayout]);
  (0, _react.useEffect)(() => {
    if (isOpen && contentRef.current) {
      handleContentLayout();
    }
  }, [isOpen, handleContentLayout]);
  (0, _react.useEffect)(() => {
    let [yAxis, xAxis] = position.split('-');

    switch (yAxis) {
      case 'top':
        if (anchorLayout.y - contentLayout.height < 0 + (safeAreaInsets.top || 0)) {
          yAxis = 'bottom';
        }

        break;

      case 'bottom':
        if (anchorLayout.y + anchorLayout.height + contentLayout.height > dimensions.height - (safeAreaInsets.bottom || 0)) {
          yAxis = 'top';
        }

        break;

      default:
        break;
    }

    switch (xAxis) {
      case 'left':
        if (anchorLayout.x + contentLayout.width > dimensions.width - (safeAreaInsets.right || 0)) {
          xAxis = 'right';
        }

        break;

      case 'right':
        if (anchorLayout.x + anchorLayout.width - contentLayout.width < 0 + (safeAreaInsets.left || 0)) {
          xAxis = 'left';
        }

        break;

      case 'center':
        const centerPoint = anchorLayout.x + anchorLayout.width / 2;

        if (centerPoint + contentLayout.width / 2 > dimensions.width - (safeAreaInsets.right || 0)) {
          xAxis = 'right';
        }

        if (centerPoint - contentLayout.width / 2 < 0 + (safeAreaInsets.top || 0)) {
          xAxis = 'left';
        }

        break;

      default:
        break;
    }

    const newPosition = `${yAxis}-${xAxis}`;
    setComputedPosition(newPosition);
  }, [position, anchorLayout, contentLayout, dimensions, safeAreaInsets.top, safeAreaInsets.bottom, safeAreaInsets.right, safeAreaInsets.left]);
  const computedStyle = (0, _react.useMemo)(() => {
    const [yAxis, xAxis] = computedPosition.split('-');
    let top = 0;
    let left = 0;

    switch (yAxis) {
      case 'top':
        top = anchorLayout.y - contentLayout.height - verticalSpacing;
        break;

      case 'bottom':
        top = anchorLayout.y + anchorLayout.height + verticalSpacing;
        break;

      default:
        break;
    }

    switch (xAxis) {
      case 'left':
        left = anchorLayout.x + horizontalSpacing;
        break;

      case 'right':
        left = anchorLayout.x + anchorLayout.width - contentLayout.width - horizontalSpacing;
        break;

      case 'center':
        left = anchorLayout.x + anchorLayout.width / 2 - contentLayout.width / 2;
        break;

      default:
        break;
    }

    return {
      top,
      left,
      opacity: top === 0 && left === 0 ? 0 : 1
    };
  }, [computedPosition, anchorLayout, contentLayout, horizontalSpacing, verticalSpacing]);
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    ref: anchorRef,
    style: [styles.anchor, debug && styles.anchorDebug],
    pointerEvents: "none"
  }), /*#__PURE__*/_react.default.createElement(_portal.Portal, null, isOpen && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, overlay && /*#__PURE__*/_react.default.createElement(_reactNative.Pressable, {
    style: [styles.overlay, debug && styles.overlayDebug],
    onPress: onClose
  }), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    ref: contentRef,
    onLayout: handleContentLayout,
    style: [styles.content, debug && styles.contentDebug, computedStyle]
  }, typeof children === 'function' ? children({
    position,
    computedPosition,
    computedStyle,
    horizontalSpacing,
    verticalSpacing
  }) : children))));
};

Popup.defaultProps = {
  debug: false,
  isOpen: false,
  position: 'bottom-right',
  horizontalSpacing: 0,
  verticalSpacing: 0,
  safeAreaInsets: {
    top: 0,
    right: 0,
    bottom: 0,
    left: 0
  },
  children: null,
  overlay: true,
  onClose: () => null
};

const styles = _reactNative.StyleSheet.create({
  anchor: { ..._reactNative.StyleSheet.absoluteFillObject
  },
  anchorDebug: {
    backgroundColor: 'rgba(255, 0, 0, .5)'
  },
  overlay: { ..._reactNative.StyleSheet.absoluteFillObject
  },
  overlayDebug: {
    backgroundColor: 'rgba(0, 0, 255, .5)'
  },
  content: {
    position: 'absolute'
  },
  contentDebug: {
    backgroundColor: 'rgba(0, 255, 0, .5)'
  }
});

var _default = Popup;
exports.default = _default;
//# sourceMappingURL=Popup.js.map