import{useEffect as d,useRef as c}from"react";import{FocusableMode as p,isFocusableElement as C}from'../utils/focus-management.js';import{useDocumentEvent as f}from'./use-document-event.js';import{useWindowEvent as M}from'./use-window-event.js';function H(s,m,i=!0){let l=c(!1);d(()=>{requestAnimationFrame(()=>{l.current=i})},[i]);function a(e,o){if(!l.current||e.defaultPrevented)return;let n=o(e);if(n===null||!n.getRootNode().contains(n))return;let E=function r(t){return typeof t=="function"?r(t()):Array.isArray(t)||t instanceof Set?t:[t]}(s);for(let r of E){if(r===null)continue;let t=r instanceof HTMLElement?r:r.current;if(t!=null&&t.contains(n)||e.composed&&e.composedPath().includes(t))return}return!C(n,p.Loose)&&n.tabIndex!==-1&&e.preventDefault(),m(e,n)}let u=c(null);f("mousedown",e=>{var o,n;l.current&&(u.current=((n=(o=e.composedPath)==null?void 0:o.call(e))==null?void 0:n[0])||e.target)},!0),f("click",e=>{u.current&&(a(e,()=>u.current),u.current=null)},!0),M("blur",e=>a(e,()=>window.document.activeElement instanceof HTMLIFrameElement?window.document.activeElement:null),!0)}export{H as useOutsideClick};
