import { useEffect, useState } from "react";
const isEventInside = (containerRef, e) => containerRef.current?.contains(e.target) ?? false;
const isEventInsideSkipableContainer = (e) => {
  const target = e.target;
  return target.closest('[data-hook="mock-modal"]') !== null;
};
function useClickOutside({
  containerRef,
  onClickOutside,
  disable,
  triggerOn
}) {
  const [isPointerDownInside, setIsPointerDownInside] = useState(false);
  useEffect(() => {
    if (disable) {
      return;
    }
    document.addEventListener("pointerdown", handler, true);
    document.addEventListener("pointerup", handler, true);
    return () => {
      document.removeEventListener("pointerdown", handler, true);
      document.removeEventListener("pointerup", handler, true);
    };
  }, [disable, isPointerDownInside, onClickOutside]);
  function handler(e) {
    const isEventInsideContainer = isEventInside(containerRef, e);
    const eventInsideSkipableContainer = isEventInsideSkipableContainer(e);
    if (e.type === "pointerdown") {
      if (triggerOn === "pointerdown" && !isEventInsideContainer && !eventInsideSkipableContainer) {
        onClickOutside(e);
        return;
      }
      setIsPointerDownInside(isEventInsideContainer);
      return;
    }
    if (e.type === "pointerup" && !isEventInsideContainer && !isPointerDownInside && !eventInsideSkipableContainer) {
      onClickOutside(e);
      return;
    }
  }
}
export {
  useClickOutside
};
//# sourceMappingURL=use-click-outside.js.map
