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