import{r as s,j as w}from"./vendor-react-BUCujlKS.js";import{c as h}from"./vendor-radix-ui-primitive-Dc_FVRD7.js";import{a as g,d as k}from"./vendor-radix-ui-react-primitive-DhVoX9SD.js";import{u as R}from"./vendor-radix-ui-react-compose-refs-DcAh5WdT.js";import{u as x}from"./vendor-radix-ui-react-use-callback-ref-BZPnuXAC.js";import{u as U}from"./vendor-radix-ui-react-use-escape-keydown-oph88g-F.js";var j="DismissableLayer",p="dismissableLayer.update",z="dismissableLayer.pointerDownOutside",H="dismissableLayer.focusOutside",C,T=s.createContext({layers:new Set,layersWithOutsidePointerEventsDisabled:new Set,branches:new Set}),M=s.forwardRef((n,e)=>{const{disableOutsidePointerEvents:r=!1,onEscapeKeyDown:a,onPointerDownOutside:i,onFocusOutside:u,onInteractOutside:f,onDismiss:d,...E}=n,o=s.useContext(T),[c,F]=s.useState(null),l=c?.ownerDocument??globalThis?.document,[,I]=s.useState({}),S=R(e,t=>F(t)),y=Array.from(o.layers),[W]=[...o.layersWithOutsidePointerEventsDisabled].slice(-1),A=y.indexOf(W),b=c?y.indexOf(c):-1,N=o.layersWithOutsidePointerEventsDisabled.size>0,D=b>=A,_=Y(t=>{const v=t.target,P=[...o.branches].some(m=>m.contains(v));!D||P||(i?.(t),f?.(t),t.defaultPrevented||d?.())},l),O=q(t=>{const v=t.target;[...o.branches].some(m=>m.contains(v))||(u?.(t),f?.(t),t.defaultPrevented||d?.())},l);return U(t=>{b===o.layers.size-1&&(a?.(t),!t.defaultPrevented&&d&&(t.preventDefault(),d()))},l),s.useEffect(()=>{if(c)return r&&(o.layersWithOutsidePointerEventsDisabled.size===0&&(C=l.body.style.pointerEvents,l.body.style.pointerEvents="none"),o.layersWithOutsidePointerEventsDisabled.add(c)),o.layers.add(c),L(),()=>{r&&o.layersWithOutsidePointerEventsDisabled.size===1&&(l.body.style.pointerEvents=C)}},[c,l,r,o]),s.useEffect(()=>()=>{c&&(o.layers.delete(c),o.layersWithOutsidePointerEventsDisabled.delete(c),L())},[c,o]),s.useEffect(()=>{const t=()=>I({});return document.addEventListener(p,t),()=>document.removeEventListener(p,t)},[]),w.jsx(g.div,{...E,ref:S,style:{pointerEvents:N?D?"auto":"none":void 0,...n.style},onFocusCapture:h(n.onFocusCapture,O.onFocusCapture),onBlurCapture:h(n.onBlurCapture,O.onBlurCapture),onPointerDownCapture:h(n.onPointerDownCapture,_.onPointerDownCapture)})});M.displayName=j;var K="DismissableLayerBranch",X=s.forwardRef((n,e)=>{const r=s.useContext(T),a=s.useRef(null),i=R(e,a);return s.useEffect(()=>{const u=a.current;if(u)return r.branches.add(u),()=>{r.branches.delete(u)}},[r.branches]),w.jsx(g.div,{...n,ref:i})});X.displayName=K;function Y(n,e=globalThis?.document){const r=x(n),a=s.useRef(!1),i=s.useRef(()=>{});return s.useEffect(()=>{const u=d=>{if(d.target&&!a.current){let E=function(){B(z,r,o,{discrete:!0})};const o={originalEvent:d};d.pointerType==="touch"?(e.removeEventListener("click",i.current),i.current=E,e.addEventListener("click",i.current,{once:!0})):E()}else e.removeEventListener("click",i.current);a.current=!1},f=window.setTimeout(()=>{e.addEventListener("pointerdown",u)},0);return()=>{window.clearTimeout(f),e.removeEventListener("pointerdown",u),e.removeEventListener("click",i.current)}},[e,r]),{onPointerDownCapture:()=>a.current=!0}}function q(n,e=globalThis?.document){const r=x(n),a=s.useRef(!1);return s.useEffect(()=>{const i=u=>{u.target&&!a.current&&B(H,r,{originalEvent:u},{discrete:!1})};return e.addEventListener("focusin",i),()=>e.removeEventListener("focusin",i)},[e,r]),{onFocusCapture:()=>a.current=!0,onBlurCapture:()=>a.current=!1}}function L(){const n=new CustomEvent(p);document.dispatchEvent(n)}function B(n,e,r,{discrete:a}){const i=r.originalEvent.target,u=new CustomEvent(n,{bubbles:!1,cancelable:!0,detail:r});e&&i.addEventListener(n,e,{once:!0}),a?k(i,u):i.dispatchEvent(u)}export{M as D};
