"use client";
'use strict';

var HLTQOHKZ = require('./HLTQOHKZ.cjs');
var _3NDVDEB4 = require('./3NDVDEB4.cjs');
var PFRGBC2K = require('./PFRGBC2K.cjs');
var YV4JVR4I = require('./YV4JVR4I.cjs');
var ABQUS43J = require('./ABQUS43J.cjs');
var _3YLGPPWQ = require('./3YLGPPWQ.cjs');
var DTR5TSDJ = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/__chunks/DTR5TSDJ.cjs');
var events = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/utils/events.cjs');
var React = require('react');

function isInDocument(target) {
  if (target.tagName === "HTML") return true;
  return DTR5TSDJ.contains(DTR5TSDJ.getDocument(target).body, target);
}
function isDisclosure(disclosure, target) {
  if (!disclosure) return false;
  if (DTR5TSDJ.contains(disclosure, target)) return true;
  const activeId = target.getAttribute("aria-activedescendant");
  if (activeId) {
    const activeElement = DTR5TSDJ.getDocument(disclosure).getElementById(activeId);
    if (activeElement) {
      return DTR5TSDJ.contains(disclosure, activeElement);
    }
  }
  return false;
}
function isMouseEventOnDialog(event, dialog) {
  if (!("clientY" in event)) return false;
  const rect = dialog.getBoundingClientRect();
  if (rect.width === 0 || rect.height === 0) return false;
  return rect.top <= event.clientY && event.clientY <= rect.top + rect.height && rect.left <= event.clientX && event.clientX <= rect.left + rect.width;
}
function useEventOutside({
  store,
  type,
  listener,
  capture,
  domReady
}) {
  const callListener = ABQUS43J.useEvent(listener);
  const open = YV4JVR4I.useStoreState(store, "open");
  const focusedRef = React.useRef(false);
  ABQUS43J.useSafeLayoutEffect(() => {
    if (!open) return;
    if (!domReady) return;
    const { contentElement } = store.getState();
    if (!contentElement) return;
    const onFocus = () => {
      focusedRef.current = true;
    };
    contentElement.addEventListener("focusin", onFocus, true);
    return () => contentElement.removeEventListener("focusin", onFocus, true);
  }, [store, open, domReady]);
  React.useEffect(() => {
    if (!open) return;
    const onEvent = (event) => {
      const { contentElement, disclosureElement } = store.getState();
      const target = event.target;
      if (!contentElement) return;
      if (!target) return;
      if (!isInDocument(target)) return;
      if (DTR5TSDJ.contains(contentElement, target)) return;
      if (isDisclosure(disclosureElement, target)) return;
      if (target.hasAttribute("data-focus-trap")) return;
      if (isMouseEventOnDialog(event, contentElement)) return;
      const focused = focusedRef.current;
      if (focused && !_3NDVDEB4.isElementMarked(target, contentElement.id)) return;
      if (PFRGBC2K.isSafariFocusAncestor(target)) return;
      callListener(event);
    };
    return events.addGlobalEventListener(type, onEvent, capture);
  }, [open, capture]);
}
function shouldHideOnInteractOutside(hideOnInteractOutside, event) {
  if (typeof hideOnInteractOutside === "function") {
    return hideOnInteractOutside(event);
  }
  return !!hideOnInteractOutside;
}
function useHideOnInteractOutside(store, hideOnInteractOutside, domReady) {
  const open = YV4JVR4I.useStoreState(store, "open");
  const previousMouseDownRef = HLTQOHKZ.usePreviousMouseDownRef(open);
  const props = { store, domReady, capture: true };
  useEventOutside(_3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues({}, props), {
    type: "click",
    listener: (event) => {
      const { contentElement } = store.getState();
      const previousMouseDown = previousMouseDownRef.current;
      if (!previousMouseDown) return;
      if (!DTR5TSDJ.isVisible(previousMouseDown)) return;
      if (!_3NDVDEB4.isElementMarked(previousMouseDown, contentElement == null ? void 0 : contentElement.id)) return;
      if (!shouldHideOnInteractOutside(hideOnInteractOutside, event)) return;
      store.hide();
    }
  }));
  useEventOutside(_3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues({}, props), {
    type: "focusin",
    listener: (event) => {
      const { contentElement } = store.getState();
      if (!contentElement) return;
      if (event.target === DTR5TSDJ.getDocument(contentElement)) return;
      if (!shouldHideOnInteractOutside(hideOnInteractOutside, event)) return;
      store.hide();
    }
  }));
  useEventOutside(_3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues({}, props), {
    type: "contextmenu",
    listener: (event) => {
      if (!shouldHideOnInteractOutside(hideOnInteractOutside, event)) return;
      store.hide();
    }
  }));
}

exports.useHideOnInteractOutside = useHideOnInteractOutside;
//# sourceMappingURL=HP6HHD56.cjs.map
