import{r as s,K as S,j as R,N as b,O as L,V as A,W as D,Y as H,Z as U}from"./index-CqI-dFNO.js";function z(e,o){if(typeof e=="function")return e(o);e!=null&&(e.current=o)}function V(...e){return o=>{let t=!1;const r=e.map(i=>{const n=z(i,o);return!t&&typeof n=="function"&&(t=!0),n});if(t)return()=>{for(let i=0;i<r.length;i++){const n=r[i];typeof n=="function"?n():z(e[i],null)}}}}function B(...e){return s.useCallback(V(...e),e)}class F extends s.Component{getSnapshotBeforeUpdate(o){const t=this.props.childRef.current;if(t&&o.isPresent&&!this.props.isPresent){const r=t.offsetParent,i=b(r)&&r.offsetWidth||0,n=this.props.sizeRef.current;n.height=t.offsetHeight||0,n.width=t.offsetWidth||0,n.top=t.offsetTop,n.left=t.offsetLeft,n.right=i-n.width-n.left}return null}componentDidUpdate(){}render(){return this.props.children}}function G({children:e,isPresent:o,anchorX:t,root:r}){const i=s.useId(),n=s.useRef(null),h=s.useRef({width:0,height:0,top:0,left:0,right:0}),{nonce:g}=s.useContext(S),E=B(n,e==null?void 0:e.ref);return s.useInsertionEffect(()=>{const{width:u,height:m,top:f,left:a,right:p}=h.current;if(o||!n.current||!u||!m)return;const C=t==="left"?`left: ${a}`:`right: ${p}`;n.current.dataset.motionPopId=i;const l=document.createElement("style");g&&(l.nonce=g);const P=r??document.head;return P.appendChild(l),l.sheet&&l.sheet.insertRule(`
          [data-motion-pop-id="${i}"] {
            position: absolute !important;
            width: ${u}px !important;
            height: ${m}px !important;
            ${C}px !important;
            top: ${f}px !important;
          }
        `),()=>{P.contains(l)&&P.removeChild(l)}},[o]),R.jsx(F,{isPresent:o,childRef:n,sizeRef:h,children:s.cloneElement(e,{ref:E})})}const N=({children:e,initial:o,isPresent:t,onExitComplete:r,custom:i,presenceAffectsLayout:n,mode:h,anchorX:g,root:E})=>{const u=L(O),m=s.useId();let f=!0,a=s.useMemo(()=>(f=!1,{id:m,initial:o,isPresent:t,custom:i,onExitComplete:p=>{u.set(p,!0);for(const C of u.values())if(!C)return;r&&r()},register:p=>(u.set(p,!1),()=>u.delete(p))}),[t,u,r]);return n&&f&&(a={...a}),s.useMemo(()=>{u.forEach((p,C)=>u.set(C,!1))},[t]),s.useEffect(()=>{!t&&!u.size&&r&&r()},[t]),h==="popLayout"&&(e=R.jsx(G,{isPresent:t,anchorX:g,root:E,children:e})),R.jsx(A.Provider,{value:a,children:e})};function O(){return new Map}const w=e=>e.key||"";function I(e){const o=[];return s.Children.forEach(e,t=>{s.isValidElement(t)&&o.push(t)}),o}const X=({children:e,custom:o,initial:t=!0,onExitComplete:r,presenceAffectsLayout:i=!0,mode:n="sync",propagate:h=!1,anchorX:g="left",root:E})=>{const[u,m]=D(h),f=s.useMemo(()=>I(e),[e]),a=h&&!u?[]:f.map(w),p=s.useRef(!0),C=s.useRef(f),l=L(()=>new Map),[P,W]=s.useState(f),[x,k]=s.useState(f);H(()=>{p.current=!1,C.current=f;for(let d=0;d<x.length;d++){const c=w(x[d]);a.includes(c)?l.delete(c):l.get(c)!==!0&&l.set(c,!1)}},[x,a.length,a.join("-")]);const M=[];if(f!==P){let d=[...f];for(let c=0;c<x.length;c++){const y=x[c],$=w(y);a.includes($)||(d.splice(c,0,y),M.push(y))}return n==="wait"&&M.length&&(d=M),k(I(d)),W(f),null}const{forceRender:j}=s.useContext(U);return R.jsx(R.Fragment,{children:x.map(d=>{const c=w(d),y=h&&!u?!1:f===x||a.includes(c),$=()=>{if(l.has(c))l.set(c,!0);else return;let v=!0;l.forEach(K=>{K||(v=!1)}),v&&(j==null||j(),k(C.current),h&&(m==null||m()),r&&r())};return R.jsx(N,{isPresent:y,initial:!p.current||t?void 0:!1,custom:o,presenceAffectsLayout:i,mode:n,root:E,onExitComplete:y?void 0:$,anchorX:g,children:d},c)})})};export{X as A};
