import{_ as S}from"./extends-dGVwEr9R.js";import{r as i}from"./index-4g5l5LRQ.js";import{$ as h}from"./index-pbeB-vsG.js";import{$ as _,a as U}from"./index-VfKohXEB.js";import{$ as k}from"./index-dNGI4_9C.js";import{$ as C}from"./index-TrPPXL0M.js";import{$ as z}from"./index-VGGqXfCL.js";const p="dismissableLayer.update",A="dismissableLayer.pointerDownOutside",N="dismissableLayer.focusOutside";let g;const H=i.createContext({layers:new Set,layersWithOutsidePointerEventsDisabled:new Set,branches:new Set}),Y=i.forwardRef((s,t)=>{var c;const{disableOutsidePointerEvents:r=!1,onEscapeKeyDown:n,onPointerDownOutside:d,onFocusOutside:v,onInteractOutside:u,onDismiss:l,...b}=s,o=i.useContext(H),[a,L]=i.useState(null),f=(c=a==null?void 0:a.ownerDocument)!==null&&c!==void 0?c:globalThis==null?void 0:globalThis.document,[,x]=i.useState({}),F=k(t,e=>L(e)),m=Array.from(o.layers),[I]=[...o.layersWithOutsidePointerEventsDisabled].slice(-1),R=m.indexOf(I),y=a?m.indexOf(a):-1,W=o.layersWithOutsidePointerEventsDisabled.size>0,O=y>=R,B=K(e=>{const $=e.target,P=[...o.branches].some(E=>E.contains($));!O||P||(d==null||d(e),u==null||u(e),e.defaultPrevented||l==null||l())},f),D=X(e=>{const $=e.target;[...o.branches].some(E=>E.contains($))||(v==null||v(e),u==null||u(e),e.defaultPrevented||l==null||l())},f);return z(e=>{y===o.layers.size-1&&(n==null||n(e),!e.defaultPrevented&&l&&(e.preventDefault(),l()))},f),i.useEffect(()=>{if(a)return r&&(o.layersWithOutsidePointerEventsDisabled.size===0&&(g=f.body.style.pointerEvents,f.body.style.pointerEvents="none"),o.layersWithOutsidePointerEventsDisabled.add(a)),o.layers.add(a),w(),()=>{r&&o.layersWithOutsidePointerEventsDisabled.size===1&&(f.body.style.pointerEvents=g)}},[a,f,r,o]),i.useEffect(()=>()=>{a&&(o.layers.delete(a),o.layersWithOutsidePointerEventsDisabled.delete(a),w())},[a,o]),i.useEffect(()=>{const e=()=>x({});return document.addEventListener(p,e),()=>document.removeEventListener(p,e)},[]),i.createElement(_.div,S({},b,{ref:F,style:{pointerEvents:W?O?"auto":"none":void 0,...s.style},onFocusCapture:h(s.onFocusCapture,D.onFocusCapture),onBlurCapture:h(s.onBlurCapture,D.onBlurCapture),onPointerDownCapture:h(s.onPointerDownCapture,B.onPointerDownCapture)}))});function K(s,t=globalThis==null?void 0:globalThis.document){const c=C(s),r=i.useRef(!1),n=i.useRef(()=>{});return i.useEffect(()=>{const d=u=>{if(u.target&&!r.current){let b=function(){T(A,c,l,{discrete:!0})};const l={originalEvent:u};u.pointerType==="touch"?(t.removeEventListener("click",n.current),n.current=b,t.addEventListener("click",n.current,{once:!0})):b()}else t.removeEventListener("click",n.current);r.current=!1},v=window.setTimeout(()=>{t.addEventListener("pointerdown",d)},0);return()=>{window.clearTimeout(v),t.removeEventListener("pointerdown",d),t.removeEventListener("click",n.current)}},[t,c]),{onPointerDownCapture:()=>r.current=!0}}function X(s,t=globalThis==null?void 0:globalThis.document){const c=C(s),r=i.useRef(!1);return i.useEffect(()=>{const n=d=>{d.target&&!r.current&&T(N,c,{originalEvent:d},{discrete:!1})};return t.addEventListener("focusin",n),()=>t.removeEventListener("focusin",n)},[t,c]),{onFocusCapture:()=>r.current=!0,onBlurCapture:()=>r.current=!1}}function w(){const s=new CustomEvent(p);document.dispatchEvent(s)}function T(s,t,c,{discrete:r}){const n=c.originalEvent.target,d=new CustomEvent(s,{bubbles:!1,cancelable:!0,detail:c});t&&n.addEventListener(s,t,{once:!0}),r?U(n,d):n.dispatchEvent(d)}export{Y as $};
