"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "PlainOutsideClickTargetRefContext", {
  enumerable: true,
  get: function get() {
    return _PlainOutsideClickTargetRefContext.PlainOutsideClickTargetRefContext;
  }
});
exports.default = withOuterListeners;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireWildcard(require("react"));
var _PlainOutsideClickTargetRefContext = require("./PlainOutsideClickTargetRefContext");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function withOuterListeners(Component) {
  return /*#__PURE__*/function (_PureComponent) {
    function WithOutsideClick() {
      var _this;
      (0, _classCallCheck2.default)(this, WithOutsideClick);
      for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }
      _this = _callSuper(this, WithOutsideClick, [].concat(args));
      (0, _defineProperty2.default)(_this, "outsideClickTargetRef", /*#__PURE__*/_react.default.createRef());
      (0, _defineProperty2.default)(_this, "handleClick", function (evt) {
        var _this$outsideClickTar;
        var domNode = (_this$outsideClickTar = _this.outsideClickTargetRef.current) === null || _this$outsideClickTar === void 0 ? void 0 : _this$outsideClickTar.deref();
        if (!domNode || evt.target instanceof Node && !domNode.contains(evt.target)) {
          var _this$props$handleCli, _this$props;
          (_this$props$handleCli = (_this$props = _this.props).handleClickOutside) === null || _this$props$handleCli === void 0 || _this$props$handleCli.call(_this$props, evt);
        }
      });
      (0, _defineProperty2.default)(_this, "handleKeydown", function (evt) {
        if (evt.code === 'Escape' && _this.props.handleEscapeKeydown) {
          _this.props.handleEscapeKeydown(evt);
        }
      });
      (0, _defineProperty2.default)(_this, "setOutsideClickTargetRef", function (el) {
        _this.outsideClickTargetRef.current = el && new WeakRef(el);
      });
      return _this;
    }
    (0, _inherits2.default)(WithOutsideClick, _PureComponent);
    return (0, _createClass2.default)(WithOutsideClick, [{
      key: "componentDidMount",
      value: function componentDidMount() {
        if (this.props.handleClickOutside) {
          // Ignored via go/ees005
          // eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
          document.addEventListener('click', this.handleClick, false);
        }
        if (this.props.handleEscapeKeydown) {
          // Ignored via go/ees005
          // eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
          document.addEventListener('keydown', this.handleKeydown, false);
        }
      }
    }, {
      key: "componentWillUnmount",
      value: function componentWillUnmount() {
        if (this.props.handleClickOutside) {
          // Ignored via go/ees005
          // eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
          document.removeEventListener('click', this.handleClick, false);
        }
        if (this.props.handleEscapeKeydown) {
          // Ignored via go/ees005
          // eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
          document.removeEventListener('keydown', this.handleKeydown, false);
        }
      }
    }, {
      key: "render",
      value: function render() {
        return /*#__PURE__*/_react.default.createElement(_PlainOutsideClickTargetRefContext.PlainOutsideClickTargetRefContext.Provider, {
          value: this.setOutsideClickTargetRef
        }, /*#__PURE__*/_react.default.createElement(Component
        // Ignored via go/ees005
        // eslint-disable-next-line react/jsx-props-no-spreading
        , this.props));
      }
    }]);
  }(_react.PureComponent);
}
// eslint-disable-next-line @atlaskit/editor/no-re-export