"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var use_click_outside_exports = {};
__export(use_click_outside_exports, {
  useClickOutside: () => useClickOutside
});
module.exports = __toCommonJS(use_click_outside_exports);
var import_react = require("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] = (0, import_react.useState)(false);
  (0, import_react.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;
    }
  }
}
//# sourceMappingURL=use-click-outside.js.map
