import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["active", "backdrop", "children", "className", "lockScroll", "onEscKeyDown", "onOverlayClick"];
import React, { useRef, useEffect } from 'react';
import Transition from 'react-transition-group/Transition';
import cx from 'classnames';
import theme from './theme.css';
import { KEY } from '../../constants';
import { selectOverlayNode } from '../select/Select';
export var Overlay = function Overlay(_ref) {
  var active = _ref.active,
      _ref$backdrop = _ref.backdrop,
      backdrop = _ref$backdrop === void 0 ? 'dark' : _ref$backdrop,
      children = _ref.children,
      className = _ref.className,
      _ref$lockScroll = _ref.lockScroll,
      lockScroll = _ref$lockScroll === void 0 ? true : _ref$lockScroll,
      onEscKeyDown = _ref.onEscKeyDown,
      onOverlayClick = _ref.onOverlayClick,
      other = _objectWithoutProperties(_ref, _excluded);

  var innerWrapperRef = useRef(null);
  var clickOriginRef = useRef(null);

  var handleEscKey = function handleEscKey(event) {
    if (active && event.key === KEY.Escape) {
      event.stopPropagation(); // react-select has its own implementation of an overlay, conflicting with our custom implementation
      // so escape events fired while the select menu is open should be ignored

      var selectMenuOpen = selectOverlayNode.hasChildNodes();
      !selectMenuOpen && onEscKeyDown && onEscKeyDown(event);
    }
  };

  var handleMouseDown = function handleMouseDown(event) {
    clickOriginRef.current = event.target;
  };

  var handleMouseUp = function handleMouseUp(event) {
    var _innerWrapperRef$curr;

    // Only register clicks outside of the children
    if (onOverlayClick && // if the clickOrigin is no longer part of the DOM tree, (f.e. due to internal React re-renders)
    document.body.contains(clickOriginRef.current) && !((_innerWrapperRef$curr = innerWrapperRef.current) !== null && _innerWrapperRef$curr !== void 0 && _innerWrapperRef$curr.contains(clickOriginRef.current)) && // react-select has its own implementation of an overlay, conflicting with our custom implementation
    // so clicks on the select overlay shouldn't be registered either
    !selectOverlayNode.contains(clickOriginRef.current) && // make sure only clicks within the current portal's DOM tree are handled
    event.currentTarget.contains(event.target)) {
      onOverlayClick(event);
    }
  };

  useEffect(function () {
    if (lockScroll) {
      if (active) {
        document.body.style.overflow = 'hidden';
      }

      if (!active && !document.querySelectorAll('[data-teamleader-ui="overlay"]')[1]) {
        document.body.style.overflow = '';
      }
    }
  }, [active, lockScroll]);
  return /*#__PURE__*/React.createElement(Transition, {
    timeout: 0,
    in: active,
    appear: true
  }, function (state) {
    return /*#__PURE__*/React.createElement("div", _extends({
      "data-teamleader-ui": "overlay"
    }, other, {
      onKeyDown: handleEscKey,
      onMouseDown: handleMouseDown,
      onMouseUp: handleMouseUp,
      className: cx(theme['overlay'], theme[backdrop], _defineProperty({}, theme['is-entered'], state === 'entered'), className)
    }), /*#__PURE__*/React.createElement("div", {
      ref: innerWrapperRef
    }, children));
  });
};
export default Overlay;