"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = require("react");
var _isAncestor = _interopRequireDefault(require("../../components/drops/mixins/isAncestor"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
var _default = exports["default"] = function _default(dropRef, onClickOutside, target, hasBackdrop, dataDrop) {
  if (dataDrop === void 0) {
    dataDrop = "";
  }
  (0, _react.useEffect)(function () {
    if (!onClickOutside || hasBackdrop) return;
    var onMousedown = function onMousedown(event) {
      if (event.target !== dropRef.current &&
      // dont fire when clicking in drop
      !(0, _isAncestor["default"])(dropRef.current, event.target) &&
      // dont fire when clicking dropdown-button
      !(0, _isAncestor["default"])(target, event.target) && !event.target.closest("[data-drop=\"" + dataDrop + "\"]")) {
        onClickOutside(event);
      }
    };
    document.addEventListener("mousedown", onMousedown);
    return function () {
      return document.removeEventListener("mousedown", onMousedown);
    };
  }, [onClickOutside]);
};