"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),require("../../../../../react/jsx-runtime.js");var e=require("react"),t=require("../../context/LayoutGroupContext.mjs.js"),r=require("../../utils/use-constant.mjs.js"),s=require("../../utils/use-isomorphic-effect.mjs.js"),n=require("./PresenceChild.mjs.js"),o=require("./use-presence.mjs.js"),i=require("./utils.mjs.js"),u=require("../../../../../../_virtual/jsx-runtime.js");exports.AnimatePresence=c=>{let{children:l,custom:a,initial:d=!0,onExitComplete:h,presenceAffectsLayout:m=!0,mode:p="sync",propagate:f=!1,anchorX:j="left",anchorY:x="top",root:g}=c;const[y,C]=o.usePresence(f),q=e.useMemo((()=>i.onlyElements(l)),[l]),v=f&&!y?[]:q.map(i.getChildKey),w=e.useRef(!0),E=e.useRef(q),P=r.useConstant((()=>new Map)),L=e.useRef(new Set),[A,K]=e.useState(q),[R,_]=e.useState(q);s.useIsomorphicLayoutEffect((()=>{w.current=!1,E.current=q;for(let e=0;e<R.length;e++){const t=i.getChildKey(R[e]);v.includes(t)?(P.delete(t),L.current.delete(t)):!0!==P.get(t)&&P.set(t,!1)}}),[R,v.length,v.join("-")]);const b=[];if(q!==A){let e=[...q];for(let t=0;t<R.length;t++){const r=R[t],s=i.getChildKey(r);v.includes(s)||(e.splice(t,0,r),b.push(r))}return"wait"===p&&b.length&&(e=b),_(i.onlyElements(e)),K(q),null}"production"!==process.env.NODE_ENV&&"wait"===p&&R.length>1&&console.warn('You\'re attempting to animate multiple children within AnimatePresence, but its mode is set to "wait". This will lead to odd visual behaviour.');const{forceRender:M}=e.useContext(t.LayoutGroupContext);return u.exports.jsx(u.exports.Fragment,{children:R.map((e=>{const t=i.getChildKey(e),r=!(f&&!y)&&(q===R||v.includes(t));return u.exports.jsx(n.PresenceChild,{isPresent:r,initial:!(w.current&&!d)&&void 0,custom:a,presenceAffectsLayout:m,mode:p,root:g,onExitComplete:r?void 0:()=>{if(L.current.has(t))return;if(!P.has(t))return;L.current.add(t),P.set(t,!0);let e=!0;P.forEach((t=>{t||(e=!1)})),e&&(M?.(),_(E.current),f&&C?.(),h&&h())},anchorX:j,anchorY:x,children:e},t)}))})};
//# sourceMappingURL=index.mjs.js.map
