import { useEffect } from "react";
import isAncestor from "../../components/drops/mixins/isAncestor";
export default (function (dropRef, onClickOutside, target, hasBackdrop, dataDrop) {
  if (dataDrop === void 0) {
    dataDrop = "";
  }
  useEffect(function () {
    if (!onClickOutside || hasBackdrop) return;
    var onMousedown = function onMousedown(event) {
      if (event.target !== dropRef.current &&
      // dont fire when clicking in drop
      !isAncestor(dropRef.current, event.target) &&
      // dont fire when clicking dropdown-button
      !isAncestor(target, event.target) && !event.target.closest("[data-drop=\"" + dataDrop + "\"]")) {
        onClickOutside(event);
      }
    };
    document.addEventListener("mousedown", onMousedown);
    return function () {
      return document.removeEventListener("mousedown", onMousedown);
    };
  }, [onClickOutside]);
});