import{j as M}from"./jsx-runtime-C35QZyMw.js";import{r as n}from"./chunk-LFPYN7LY-BBzdRvLF.js";import{M as D,g as v,u as H,P as K,h as U,c as W,L as B}from"./proxy-Bhwq1qjG.js";function I(e,r){if(typeof e=="function")return e(r);e!=null&&(e.current=r)}function G(...e){return r=>{let t=!1;const o=e.map(f=>{const u=I(f,r);return!t&&typeof u=="function"&&(t=!0),u});if(t)return()=>{for(let f=0;f{const{width:p,height:h,top:g,left:x,right:$,bottom:b}=E.current;if(r||u===!1||!s.current||!p||!h)return;const L=t==="left"?`left: ${x}`:`right: ${$}`,m=o==="bottom"?`bottom: ${b}`:`top: ${g}`;s.current.dataset.motionPopId=a;const C=document.createElement("style");w&&(C.nonce=w);const R=f??document.head;return R.appendChild(C),C.sheet&&C.sheet.insertRule(` [data-motion-pop-id="${a}"] { position: absolute !important; width: ${p}px !important; height: ${h}px !important; ${L}px !important; ${m}px !important; } `),()=>{R.contains(C)&&R.removeChild(C)}},[r]),M.jsx(V,{isPresent:r,childRef:s,sizeRef:E,pop:u,children:u===!1?e:n.cloneElement(e,{ref:y})})}const Y=({children:e,initial:r,isPresent:t,onExitComplete:o,custom:f,presenceAffectsLayout:u,mode:a,anchorX:s,anchorY:E,root:w})=>{const l=H(_),y=n.useId();let c=!0,p=n.useMemo(()=>(c=!1,{id:y,initial:r,isPresent:t,custom:f,onExitComplete:h=>{l.set(h,!0);for(const g of l.values())if(!g)return;o&&o()},register:h=>(l.set(h,!1),()=>l.delete(h))}),[t,l,o]);return u&&c&&(p={...p}),n.useMemo(()=>{l.forEach((h,g)=>l.set(g,!1))},[t]),n.useEffect(()=>{!t&&!l.size&&o&&o()},[t]),e=M.jsx(X,{pop:a==="popLayout",isPresent:t,anchorX:s,anchorY:E,root:w,children:e}),M.jsx(K.Provider,{value:p,children:e})};function _(){return new Map}const j=e=>e.key||"";function F(e){const r=[];return n.Children.forEach(e,t=>{n.isValidElement(t)&&r.push(t)}),r}const O=({children:e,custom:r,initial:t=!0,onExitComplete:o,presenceAffectsLayout:f=!0,mode:u="sync",propagate:a=!1,anchorX:s="left",anchorY:E="top",root:w})=>{const[l,y]=U(a),c=n.useMemo(()=>F(e),[e]),p=a&&!l?[]:c.map(j),h=n.useRef(!0),g=n.useRef(c),x=H(()=>new Map),$=n.useRef(new Set),[b,L]=n.useState(c),[m,C]=n.useState(c);W(()=>{h.current=!1,g.current=c;for(let d=0;d{const i=j(d),P=a&&!l?!1:c===m||p.includes(i),k=()=>{if($.current.has(i))return;if($.current.add(i),x.has(i))x.set(i,!0);else return;let z=!0;x.forEach(A=>{A||(z=!1)}),z&&(S==null||S(),C(g.current),a&&(y==null||y()),o&&o())};return M.jsx(Y,{isPresent:P,initial:!h.current||t?void 0:!1,custom:r,presenceAffectsLayout:f,mode:u,root:w,onExitComplete:P?void 0:k,anchorX:s,anchorY:E,children:d},i)})})};export{O as A};