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