import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
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 = ["handleClickOutside", "handleEnterKeydown", "handleEscapeKeydown", "closeOnTab"];
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() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
import React, { PureComponent, useEffect, useState } from 'react';
import ReactDOM from 'react-dom';
import ReactEditorViewContext from './ReactEditorViewContext';

// This needs exporting to be used alongisde `withReactEditorViewOuterListeners`
var WithOutsideClick = /*#__PURE__*/function (_PureComponent) {
  _inherits(WithOutsideClick, _PureComponent);
  var _super = _createSuper(WithOutsideClick);
  function WithOutsideClick() {
    var _this;
    _classCallCheck(this, WithOutsideClick);
    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), "handleClick", function (evt) {
      if (!_this.props.isActiveComponent) {
        return;
      }
      var domNode = ReactDOM.findDOMNode(_assertThisInitialized(_this)); // eslint-disable-line react/no-find-dom-node
      if (!domNode || evt.target instanceof Node && !domNode.contains(evt.target)) {
        if (_this.props.handleClickOutside) {
          var _this$props$editorVie;
          _this.props.handleClickOutside(evt);
          // When the menus are closed by clicking outside the focus is set on editor.
          if (!((_this$props$editorVie = _this.props.editorView) !== null && _this$props$editorVie !== void 0 && _this$props$editorVie.hasFocus())) {
            var _this$props$editorVie2;
            (_this$props$editorVie2 = _this.props.editorView) === null || _this$props$editorVie2 === void 0 || _this$props$editorVie2.focus();
          }
        }
      }
    });
    _defineProperty(_assertThisInitialized(_this), "handleKeydown", function (evt) {
      if (!_this.props.isActiveComponent) {
        return;
      }
      if (evt.code === 'Escape' && _this.props.handleEscapeKeydown) {
        evt.preventDefault();
        evt.stopPropagation();
        _this.props.handleEscapeKeydown(evt);
        // on 'Esc', Focus is handled in 'handleEscapeKeydown'.
        return false;
      } else if (evt.code === 'Enter' && _this.props.handleEnterKeydown) {
        _this.props.handleEnterKeydown(evt);
      } else if (evt.code === 'Tab' && _this.props.handleEscapeKeydown && _this.props.closeOnTab) {
        // The menus should be closed when the tab is pressed as it takes the focus out of the menu
        _this.props.handleEscapeKeydown(evt);
      }
    });
    return _this;
  }
  _createClass(WithOutsideClick, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      if (this.props.handleClickOutside) {
        document.addEventListener('click', this.handleClick, false);
      }
      if (this.props.handleEscapeKeydown) {
        var _this$props$editorRef;
        // Attached event to the menu so that 'ESC' events from the opened menu also will be handled.
        (((_this$props$editorRef = this.props.editorRef) === null || _this$props$editorRef === void 0 ? void 0 : _this$props$editorRef.current) || this.props.targetRef || document).addEventListener('keydown', this.handleKeydown, false);
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (this.props.handleClickOutside) {
        document.removeEventListener('click', this.handleClick, false);
      }
      if (this.props.handleEscapeKeydown) {
        var _this$props$editorRef2;
        (((_this$props$editorRef2 = this.props.editorRef) === null || _this$props$editorRef2 === void 0 ? void 0 : _this$props$editorRef2.current) || this.props.targetRef || document).removeEventListener('keydown', this.handleKeydown, false);
      }
    }
  }, {
    key: "render",
    value: function render() {
      return this.props.children;
    }
  }]);
  return WithOutsideClick;
}(PureComponent);
function hasIsOpen(props) {
  return 'isOpen' in props;
}
export default function withReactEditorViewOuterListeners(Component) {
  return function (_ref) {
    var handleClickOutside = _ref.handleClickOutside,
      handleEnterKeydown = _ref.handleEnterKeydown,
      handleEscapeKeydown = _ref.handleEscapeKeydown,
      closeOnTab = _ref.closeOnTab,
      props = _objectWithoutProperties(_ref, _excluded);
    var isActiveProp = hasIsOpen(props) ? props.isOpen : true;
    var _useState = useState(false),
      _useState2 = _slicedToArray(_useState, 2),
      isActiveComponent = _useState2[0],
      setActiveComponent = _useState2[1];
    useEffect(function () {
      requestAnimationFrame(function () {
        setActiveComponent(isActiveProp);
      });
    }, [isActiveProp]);
    return /*#__PURE__*/React.createElement(ReactEditorViewContext.Consumer, null, function (_ref2) {
      var editorView = _ref2.editorView,
        editorRef = _ref2.editorRef;
      return /*#__PURE__*/React.createElement(WithOutsideClick, {
        editorView: editorView,
        editorRef: editorRef,
        targetRef: props.targetRef,
        isActiveComponent: isActiveComponent,
        handleClickOutside: handleClickOutside,
        handleEnterKeydown: handleEnterKeydown,
        handleEscapeKeydown: handleEscapeKeydown,
        closeOnTab: closeOnTab
      }, /*#__PURE__*/React.createElement(Component, props));
    });
  };
}