import{r as e}from"./react-vendor-CmFgShSM.js";import{fu as z,fv as $,fw as O,fx as S,fy as U,fz as b}from"./index-Vwb3Bce5.js";const M=new Set;function A(s,n,t){M.has(n)||(console.warn(n),M.add(n))}function I(){const s=e.useRef(!1);return z(()=>(s.current=!0,()=>{s.current=!1}),[]),s}function K(){const s=I(),[n,t]=e.useState(0),r=e.useCallback(()=>{s.current&&t(n+1)},[n]);return[e.useCallback(()=>$.postRender(r),[r]),n]}class B extends e.Component{getSnapshotBeforeUpdate(n){const t=this.props.childRef.current;if(t&&n.isPresent&&!this.props.isPresent){const r=this.props.sizeRef.current;r.height=t.offsetHeight||0,r.width=t.offsetWidth||0,r.top=t.offsetTop,r.left=t.offsetLeft}return null}componentDidUpdate(){}render(){return this.props.children}}function G({children:s,isPresent:n}){const t=e.useId(),r=e.useRef(null),m=e.useRef({width:0,height:0,top:0,left:0});return e.useInsertionEffect(()=>{const{width:a,height:u,top:i,left:h}=m.current;if(n||!r.current||!a||!u)return;r.current.dataset.motionPopId=t;const d=document.createElement("style");return document.head.appendChild(d),d.sheet&&d.sheet.insertRule(`
          [data-motion-pop-id="${t}"] {
            position: absolute !important;
            width: ${a}px !important;
            height: ${u}px !important;
            top: ${i}px !important;
            left: ${h}px !important;
          }
        `),()=>{document.head.removeChild(d)}},[n]),e.createElement(B,{isPresent:n,childRef:r,sizeRef:m},e.cloneElement(s,{ref:r}))}const w=({children:s,initial:n,isPresent:t,onExitComplete:r,custom:m,presenceAffectsLayout:a,mode:u})=>{const i=O(T),h=e.useId(),d=e.useMemo(()=>({id:h,initial:n,isPresent:t,custom:m,onExitComplete:f=>{i.set(f,!0);for(const c of i.values())if(!c)return;r&&r()},register:f=>(i.set(f,!1),()=>i.delete(f))}),a?void 0:[t]);return e.useMemo(()=>{i.forEach((f,c)=>i.set(c,!1))},[t]),e.useEffect(()=>{!t&&!i.size&&r&&r()},[t]),u==="popLayout"&&(s=e.createElement(G,{isPresent:t},s)),e.createElement(S.Provider,{value:d},s)};function T(){return new Map}const R=s=>s.key||"";function D(s,n){s.forEach(t=>{const r=R(t);n.set(r,t)})}function H(s){const n=[];return e.Children.forEach(s,t=>{e.isValidElement(t)&&n.push(t)}),n}const _=({children:s,custom:n,initial:t=!0,onExitComplete:r,exitBeforeEnter:m,presenceAffectsLayout:a=!0,mode:u="sync"})=>{m&&(u="wait",A(!1,"Replace exitBeforeEnter with mode='wait'"));let[i]=K();const h=e.useContext(U).forceRender;h&&(i=h);const d=I(),f=H(s);let c=f;const l=new Set,E=e.useRef(c),C=e.useRef(new Map).current,x=e.useRef(!0);if(z(()=>{x.current=!1,D(f,C),E.current=c}),b(()=>{x.current=!0,C.clear(),l.clear()}),x.current)return e.createElement(e.Fragment,null,c.map(o=>e.createElement(w,{key:R(o),isPresent:!0,initial:t?void 0:!1,presenceAffectsLayout:a,mode:u},o)));c=[...c];const y=E.current.map(R),g=f.map(R),P=y.length;for(let o=0;o<P;o++){const p=y[o];g.indexOf(p)===-1&&l.add(p)}return u==="wait"&&l.size&&(c=[]),l.forEach(o=>{if(g.indexOf(o)!==-1)return;const p=C.get(o);if(!p)return;const k=y.indexOf(o),L=()=>{C.delete(o),l.delete(o);const v=E.current.findIndex(F=>F.key===o);if(E.current.splice(v,1),!l.size){if(E.current=f,d.current===!1)return;i(),r&&r()}};c.splice(k,0,e.createElement(w,{key:R(p),isPresent:!1,onExitComplete:L,custom:n,presenceAffectsLayout:a,mode:u},p))}),c=c.map(o=>{const p=o.key;return l.has(p)?o:e.createElement(w,{key:R(o),isPresent:!0,presenceAffectsLayout:a,mode:u},o)}),e.createElement(e.Fragment,null,l.size?c:c.map(o=>e.cloneElement(o)))};export{_ as A};
