{"version":3,"file":"visual-element-variant.mjs","sources":["../../../../src/animation/interfaces/visual-element-variant.ts"],"sourcesContent":["import { resolveVariant } from \"../../render/utils/resolve-dynamic-variants\"\nimport { calcChildStagger } from \"../utils/calc-child-stagger\"\nimport type { VisualElementAnimationOptions } from \"./types\"\nimport { animateTarget } from \"./visual-element-target\"\nimport type { DynamicOption } from \"../types\"\nimport type { VisualElement } from \"../../render/VisualElement\"\n\nexport function animateVariant(\n    visualElement: VisualElement,\n    variant: string,\n    options: VisualElementAnimationOptions = {}\n): Promise<any> {\n    const resolved = resolveVariant(\n        visualElement,\n        variant,\n        options.type === \"exit\"\n            ? visualElement.presenceContext?.custom\n            : undefined\n    )\n\n    let { transition = visualElement.getDefaultTransition() || {} } =\n        resolved || {}\n\n    if (options.transitionOverride) {\n        transition = options.transitionOverride\n    }\n\n    /**\n     * If we have a variant, create a callback that runs it as an animation.\n     * Otherwise, we resolve a Promise immediately for a composable no-op.\n     */\n    const getAnimation: () => Promise<any> = resolved\n        ? () => Promise.all(animateTarget(visualElement, resolved, options))\n        : () => Promise.resolve()\n\n    /**\n     * If we have children, create a callback that runs all their animations.\n     * Otherwise, we resolve a Promise immediately for a composable no-op.\n     */\n    const getChildAnimations =\n        visualElement.variantChildren && visualElement.variantChildren.size\n            ? (forwardDelay = 0) => {\n                  const {\n                      delayChildren = 0,\n                      staggerChildren,\n                      staggerDirection,\n                  } = transition\n\n                  return animateChildren(\n                      visualElement,\n                      variant,\n                      forwardDelay,\n                      delayChildren,\n                      staggerChildren,\n                      staggerDirection,\n                      options\n                  )\n              }\n            : () => Promise.resolve()\n\n    /**\n     * If the transition explicitly defines a \"when\" option, we need to resolve either\n     * this animation or all children animations before playing the other.\n     */\n    const { when } = transition\n    if (when) {\n        const [first, last] =\n            when === \"beforeChildren\"\n                ? [getAnimation, getChildAnimations]\n                : [getChildAnimations, getAnimation]\n\n        return first().then(() => last())\n    } else {\n        return Promise.all([getAnimation(), getChildAnimations(options.delay)])\n    }\n}\n\nfunction animateChildren(\n    visualElement: VisualElement,\n    variant: string,\n    delay: number = 0,\n    delayChildren: number | DynamicOption<number> = 0,\n    staggerChildren = 0,\n    staggerDirection = 1,\n    options: VisualElementAnimationOptions\n) {\n    const animations: Promise<any>[] = []\n\n    for (const child of visualElement.variantChildren!) {\n        child.notify(\"AnimationStart\", variant)\n        animations.push(\n            animateVariant(child, variant, {\n                ...options,\n                delay:\n                    delay +\n                    (typeof delayChildren === \"function\" ? 0 : delayChildren) +\n                    calcChildStagger(\n                        visualElement.variantChildren!,\n                        child,\n                        delayChildren,\n                        staggerChildren,\n                        staggerDirection\n                    ),\n            }).then(() => child.notify(\"AnimationComplete\", variant))\n        )\n    }\n\n    return Promise.all(animations)\n}\n"],"names":[],"mappings":";;;;AAOM,SAAU,cAAc,CAC1B,aAA4B,EAC5B,OAAe,EACf,UAAyC,EAAE,EAAA;AAE3C,IAAA,MAAM,QAAQ,GAAG,cAAc,CAC3B,aAAa,EACb,OAAO,EACP,OAAO,CAAC,IAAI,KAAK;AACb,UAAE,aAAa,CAAC,eAAe,EAAE;UAC/B,SAAS,CAClB;AAED,IAAA,IAAI,EAAE,UAAU,GAAG,aAAa,CAAC,oBAAoB,EAAE,IAAI,EAAE,EAAE,GAC3D,QAAQ,IAAI,EAAE;AAElB,IAAA,IAAI,OAAO,CAAC,kBAAkB,EAAE;AAC5B,QAAA,UAAU,GAAG,OAAO,CAAC,kBAAkB;IAC3C;AAEA;;;AAGG;IACH,MAAM,YAAY,GAAuB;AACrC,UAAE,MAAM,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,aAAa,EAAE,QAAQ,EAAE,OAAO,CAAC;UACjE,MAAM,OAAO,CAAC,OAAO,EAAE;AAE7B;;;AAGG;IACH,MAAM,kBAAkB,GACpB,aAAa,CAAC,eAAe,IAAI,aAAa,CAAC,eAAe,CAAC;AAC3D,UAAE,CAAC,YAAY,GAAG,CAAC,KAAI;YACjB,MAAM,EACF,aAAa,GAAG,CAAC,EACjB,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