import{r as s,j as R}from"./app-BwDemWQz.js";import{M as W,i as A,u as I,P as D,a as H,b as K,L as U}from"./proxy-DQ767J20.js";function v(e,o){if(typeof e=="function")return e(o);e!=null&&(e.current=o)}function B(...e){return o=>{let t=!1;const r=e.map(i=>{const n=v(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():v(e[i],null)}}}}function F(...e){return s.useCallback(B(...e),e)}class G extends s.Component{getSnapshotBeforeUpdate(o){const t=this.props.childRef.current;if(t&&o.isPresent&&!this.props.isPresent){const r=t.offsetParent,i=A(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 T({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(W),E=F(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(G,{isPresent:o,childRef:n,sizeRef:h,children:s.cloneElement(e,{ref:E})})}const V=({children:e,initial:o,isPresent:t,onExitComplete:r,custom:i,presenceAffectsLayout:n,mode:h,anchorX:g,root:E})=>{const u=I(X),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(T,{isPresent:t,anchorX:g,root:E,children:e})),R.jsx(D.Provider,{value:a,children:e})};function X(){return new Map}const M=e=>e.key||"";function z(e){const o=[];return s.Children.forEach(e,t=>{s.isValidElement(t)&&o.push(t)}),o}const J=({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]=H(h),f=s.useMemo(()=>z(e),[e]),a=h&&!u?[]:f.map(M),p=s.useRef(!0),C=s.useRef(f),l=I(()=>new Map),[P,b]=s.useState(f),[x,L]=s.useState(f);K(()=>{p.current=!1,C.current=f;for(let d=0;d<x.length;d++){const c=M(x[d]);a.includes(c)?l.delete(c):l.get(c)!==!0&&l.set(c,!1)}},[x,a.length,a.join("-")]);const w=[];if(f!==P){let d=[...f];for(let c=0;c<x.length;c++){const y=x[c],$=M(y);a.includes($)||(d.splice(c,0,y),w.push(y))}return n==="wait"&&w.length&&(d=w),L(z(d)),b(f),null}const{forceRender:j}=s.useContext(U);return R.jsx(R.Fragment,{children:x.map(d=>{const c=M(d),y=h&&!u?!1:f===x||a.includes(c),$=()=>{if(l.has(c))l.set(c,!0);else return;let k=!0;l.forEach(S=>{S||(k=!1)}),k&&(j==null||j(),L(C.current),h&&(m==null||m()),r&&r())};return R.jsx(V,{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{J as A};
