"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.OutsideClickListener = exports.OUTSIDE_CLICK_IGNORED_CLASS = void 0;
var _react = _interopRequireWildcard(require("react"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (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 (const 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); }
const OUTSIDE_CLICK_IGNORED_CLASS = exports.OUTSIDE_CLICK_IGNORED_CLASS = 'outside-click-listener-ignore';
class OutsideClickListener extends _react.Component {
  static defaultProps = {
    onClickOutside: () => {},
    shouldCloseOnEsc: false
  };
  constructor(props) {
    super(props);
    this.setWrapperRef = this.setWrapperRef.bind(this);
    this.isOutsideClick = this.isOutsideClick.bind(this);
    this.onMouseDown = this.onMouseDown.bind(this);
    this.onMouseUp = this.onMouseUp.bind(this);
    this.onKeyDown = this.onKeyDown.bind(this);
  }
  componentDidMount() {
    this.addMouseDownEventListener();
    if (this.props.shouldCloseOnEsc) {
      this.addKeyDownEventListener();
    }
  }
  componentWillUnmount() {
    this.removeMouseUp();
    this.removeMouseDown();
    this.removeKeyDownEventListener();
  }
  onMouseDown(e) {
    const isOutsideClick = this.isOutsideClick(e);

    // Click is outside, so start listening for the mouseup event.
    if (isOutsideClick) {
      this.removeMouseUp();
      document.addEventListener('mouseup', this.onMouseUp, {
        capture: true
      });
    }
  }
  onMouseUp(e) {
    this.removeMouseUp();
    const isOutsideClick = this.isOutsideClick(e);
    if (isOutsideClick) {
      this.props.onClickOutside(e);
    }
  }
  onKeyDown(e) {
    if (e.key === 'Escape') {
      this.props.onClickOutside();
    }
  }
  addMouseDownEventListener() {
    document.addEventListener('mousedown', this.onMouseDown, {
      capture: true
    });
  }
  removeMouseUp() {
    document.removeEventListener('mouseup', this.onMouseUp, {
      capture: true
    });
  }
  removeMouseDown() {
    document.removeEventListener('mousedown', this.onMouseDown, {
      capture: true
    });
  }
  addKeyDownEventListener() {
    document.addEventListener('keydown', this.onKeyDown, {
      capture: true
    });
  }
  removeKeyDownEventListener() {
    document.removeEventListener('keydown', this.onKeyDown, {
      capture: true
    });
  }
  setWrapperRef(node) {
    this.wrapperRef = node;
  }
  isOutsideClick(event) {
    const elementsToAvoidOutsideClick = document.querySelectorAll(`.${OUTSIDE_CLICK_IGNORED_CLASS}`);
    let isInsideIgnoredElement = false;
    elementsToAvoidOutsideClick.forEach(element => {
      if (element.contains(event.target)) {
        isInsideIgnoredElement = true;
      }
    });
    if (isInsideIgnoredElement) {
      return false;
    }
    return this.wrapperRef && !this.wrapperRef.contains(event.target);
  }
  render() {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      "test-id": "outside-click-listener",
      ref: this.setWrapperRef,
      className: "outside-click-listener-container",
      children: this.props.children
    });
  }
}
exports.OutsideClickListener = OutsideClickListener;