import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var _excluded = ["active", "className", "backdrop", "lockScroll", "onEscKeyDown", "onOverlayClick"];

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
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';
/** @type {React.ComponentType<any>} */

var Overlay = /*#__PURE__*/function (_PureComponent) {
  _inherits(Overlay, _PureComponent);

  var _super = _createSuper(Overlay);

  function Overlay() {
    var _this;

    _classCallCheck(this, Overlay);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));

    _defineProperty(_assertThisInitialized(_this), "innerWrapperRef", /*#__PURE__*/React.createRef());

    _defineProperty(_assertThisInitialized(_this), "clickOriginRef", /*#__PURE__*/React.createRef());

    _defineProperty(_assertThisInitialized(_this), "handleEscKey", function (event) {
      if (_this.props.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 && _this.props.onEscKeyDown && _this.props.onEscKeyDown(event);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleMouseDown", function (event) {
      _this.clickOriginRef.current = event.target;
    });

    _defineProperty(_assertThisInitialized(_this), "handleMouseUp", function (event) {
      var _this$innerWrapperRef;

      // Only register clicks outside of the children
      if (_this.props.onOverlayClick && // if the clickOrigin is no longer part of the DOM tree, (f.e. due to internal React re-renders)
      document.body.contains(_this.clickOriginRef.current) && !((_this$innerWrapperRef = _this.innerWrapperRef.current) !== null && _this$innerWrapperRef !== void 0 && _this$innerWrapperRef.contains(_this.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(_this.clickOriginRef.current) && // make sure only clicks within the current portal's DOM tree are handled
      event.currentTarget.contains(event.target)) {
        _this.props.onOverlayClick(event);
      }
    });

    return _this;
  }

  _createClass(Overlay, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this$props = this.props,
          active = _this$props.active,
          lockScroll = _this$props.lockScroll;

      if (active && lockScroll) {
        document.body.style.overflow = 'hidden';
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      if (this.props.lockScroll) {
        var becomingActive = this.props.active && !prevProps.active;
        var becomingUnactive = !this.props.active && prevProps.active;

        if (becomingActive) {
          document.body.style.overflow = 'hidden';
        }

        if (becomingUnactive && !document.querySelectorAll('[data-teamleader-ui="overlay"]')[1]) {
          document.body.style.overflow = '';
        }
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (this.props.active && this.props.lockScroll) {
        if (!document.querySelectorAll('[data-teamleader-ui="overlay"]')[1]) {
          document.body.style.overflow = '';
        }
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props2 = this.props,
          active = _this$props2.active,
          className = _this$props2.className,
          backdrop = _this$props2.backdrop,
          lockScroll = _this$props2.lockScroll,
          onEscKeyDown = _this$props2.onEscKeyDown,
          onOverlayClick = _this$props2.onOverlayClick,
          other = _objectWithoutProperties(_this$props2, _excluded);

      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: _this2.handleEscKey,
          onMouseDown: _this2.handleMouseDown,
          onMouseUp: _this2.handleMouseUp,
          className: cx(theme['overlay'], theme[backdrop], _defineProperty({}, theme['is-entered'], state === 'entered'), className)
        }), /*#__PURE__*/React.createElement("div", {
          ref: _this2.innerWrapperRef
        }, _this2.props.children));
      });
    }
  }]);

  return Overlay;
}(PureComponent);

Overlay.defaultProps = {
  backdrop: 'dark',
  lockScroll: true
};
export default Overlay;