"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _typeof = require("@babel/runtime/helpers/typeof");

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

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

var _Transition = _interopRequireDefault(require("react-transition-group/Transition"));

var _classnames = _interopRequireDefault(require("classnames"));

var _theme = _interopRequireDefault(require("./theme.css"));

var _constants = require("../../constants");

var _Select = require("../select/Select");

var _excluded = ["active", "backdrop", "children", "className", "lockScroll", "onEscKeyDown", "onOverlayClick"];

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(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; }

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 = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var innerWrapperRef = (0, _react.useRef)(null);
  var clickOriginRef = (0, _react.useRef)(null);

  var handleEscKey = function handleEscKey(event) {
    if (active && event.key === _constants.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 = _Select.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
    !_Select.selectOverlayNode.contains(clickOriginRef.current) && // make sure only clicks within the current portal's DOM tree are handled
    event.currentTarget.contains(event.target)) {
      onOverlayClick(event);
    }
  };

  (0, _react.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.default.createElement(_Transition.default, {
    timeout: 0,
    in: active,
    appear: true
  }, function (state) {
    return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
      "data-teamleader-ui": "overlay"
    }, other, {
      onKeyDown: handleEscKey,
      onMouseDown: handleMouseDown,
      onMouseUp: handleMouseUp,
      className: (0, _classnames.default)(_theme.default['overlay'], _theme.default[backdrop], (0, _defineProperty2.default)({}, _theme.default['is-entered'], state === 'entered'), className)
    }), /*#__PURE__*/_react.default.createElement("div", {
      ref: innerWrapperRef
    }, children));
  });
};

exports.Overlay = Overlay;
var _default = Overlay;
exports.default = _default;