"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("../../animation/interfaces/visual-element.mjs.js"),t=require("../../animation/utils/calc-child-stagger.mjs.js"),i=require("./get-variant-context.mjs.js"),n=require("./is-animation-controls.mjs.js"),r=require("./is-keyframes-target.mjs.js"),a=require("./is-variant-label.mjs.js"),s=require("./resolve-dynamic-variants.mjs.js"),o=require("./shallow-compare.mjs.js"),l=require("./variant-props.mjs.js");const c=[...l.variantPriorityOrder].reverse(),u=l.variantPriorityOrder.length;function m(t){return i=>Promise.all(i.map((i=>{let{animation:n,options:r}=i;return e.animateVisualElement(t,n,r)})))}function v(e,t){return"string"==typeof t?t!==e:!!Array.isArray(t)&&!o.shallowCompare(t,e)}function p(){return{isActive:arguments.length>0&&void 0!==arguments[0]&&arguments[0],protectedKeys:{},needsAnimating:{},prevResolvedValues:{}}}function d(){return{animate:p(!0),whileInView:p(),whileHover:p(),whileTap:p(),whileDrag:p(),whileFocus:p(),exit:p()}}exports.checkVariantsDidChange=v,exports.createAnimationState=function(e){let l=m(e),p=d(),f=!0,y=!1;const h=t=>(i,n)=>{const r=s.resolveVariant(e,n,"exit"===t?e.presenceContext?.custom:void 0);if(r){const{transition:e,transitionEnd:t,...n}=r;i={...i,...n,...t}}return i};function g(m){const{props:d}=e,g=i.getVariantContext(e.parent)||{},A=[],j=new Set;let V={},C=1/0;for(let i=0;i<u;i++){const l=c[i],u=p[l],w=void 0!==d[l]?d[l]:g[l],P=a.isVariantLabel(w),q=l===m?u.isActive:null;!1===q&&(C=i);let x=w===g[l]&&w!==d[l]&&P;if(x&&(f||y)&&e.manuallyAnimateOnMount&&(x=!1),u.protectedKeys={...V},!u.isActive&&null===q||!w&&!u.prevProp||n.isAnimationControls(w)||"boolean"==typeof w)continue;if("exit"===l&&u.isActive&&!0!==q){u.prevResolvedValues&&(V={...V,...u.prevResolvedValues});continue}const O=v(u.prevProp,w);let S=O||l===m&&u.isActive&&!x&&P||i>C&&P,b=!1;const K=Array.isArray(w)?w:[w];let R=K.reduce(h(l),{});!1===q&&(R={});const{prevResolvedValues:E={}}=u,M={...E,...R},T=t=>{S=!0,j.has(t)&&(b=!0,j.delete(t)),u.needsAnimating[t]=!0;const i=e.getValue(t);i&&(i.liveStyle=!1)};for(const e in M){const t=R[e],i=E[e];if(V.hasOwnProperty(e))continue;let n=!1;n=r.isKeyframesTarget(t)&&r.isKeyframesTarget(i)?!o.shallowCompare(t,i)||O:t!==i,n?null!=t?T(e):j.add(e):void 0!==t&&j.has(e)?T(e):u.protectedKeys[e]=!0}u.prevProp=w,u.prevResolvedValues=R,u.isActive&&(V={...V,...R}),(f||y)&&e.blockInitialAnimation&&(S=!1);const k=x&&O;S&&(!k||b)&&A.push(...K.map((i=>{const n={type:l};if("string"==typeof i&&(f||y)&&!k&&e.manuallyAnimateOnMount&&e.parent){const{parent:r}=e,a=s.resolveVariant(r,i);if(r.enteringChildren&&a){const{delayChildren:i}=a.transition||{};n.delay=t.calcChildStagger(r.enteringChildren,e,i)}}return{animation:i,options:n}})))}if(j.size){const t={};if("boolean"!=typeof d.initial){const i=s.resolveVariant(e,Array.isArray(d.initial)?d.initial[0]:d.initial);i&&i.transition&&(t.transition=i.transition)}j.forEach((i=>{const n=e.getBaseTarget(i),r=e.getValue(i);r&&(r.liveStyle=!0),t[i]=n??null})),A.push({animation:t})}let w=Boolean(A.length);return!f||!1!==d.initial&&d.initial!==d.animate||e.manuallyAnimateOnMount||(w=!1),f=!1,y=!1,w?l(A):Promise.resolve()}return{animateChanges:g,setActive:function(t,i){if(p[t].isActive===i)return Promise.resolve();e.variantChildren?.forEach((e=>e.animationState?.setActive(t,i))),p[t].isActive=i;const n=g(t);for(const e in p)p[e].protectedKeys={};return n},setAnimateFunction:function(t){l=t(e)},getState:()=>p,reset:()=>{p=d(),y=!0}}};
//# sourceMappingURL=animation-state.mjs.js.map
