"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
require("../motion-utils/dist-es-errors.cjs");
const getValueTransition = require("../motion-dom/dist-es-animation-utils-get-value-transition.cjs");
const keysPosition = require("./dist-es-render-html-utils-keys-position.cjs");
const setters = require("./dist-es-render-utils-setters.cjs");
const addWillChange = require("./dist-es-value-use-will-change-add-will-change.cjs");
const getAppearId = require("./dist-es-animation-optimized-appear-get-appear-id.cjs");
const motionValue = require("./dist-es-animation-interfaces-motion-value.cjs");
const frame = require("./dist-es-frameloop-frame.cjs");
function shouldBlockAnimation({ protectedKeys, needsAnimating }, key) {
  const shouldBlock = protectedKeys.hasOwnProperty(key) && needsAnimating[key] !== true;
  needsAnimating[key] = false;
  return shouldBlock;
}
function animateTarget(visualElement, targetAndTransition, { delay = 0, transitionOverride, type } = {}) {
  var _a;
  let { transition = visualElement.getDefaultTransition(), transitionEnd, ...target } = targetAndTransition;
  if (transitionOverride)
    transition = transitionOverride;
  const animations = [];
  const animationTypeState = type && visualElement.animationState && visualElement.animationState.getState()[type];
  for (const key in target) {
    const value = visualElement.getValue(key, (_a = visualElement.latestValues[key]) !== null && _a !== void 0 ? _a : null);
    const valueTarget = target[key];
    if (valueTarget === void 0 || animationTypeState && shouldBlockAnimation(animationTypeState, key)) {
      continue;
    }
    const valueTransition = {
      delay,
      ...getValueTransition.getValueTransition(transition || {}, key)
    };
    let isHandoff = false;
    if (window.MotionHandoffAnimation) {
      const appearId = getAppearId.getOptimisedAppearId(visualElement);
      if (appearId) {
        const startTime = window.MotionHandoffAnimation(appearId, key, frame.frame);
        if (startTime !== null) {
          valueTransition.startTime = startTime;
          isHandoff = true;
        }
      }
    }
    addWillChange.addValueToWillChange(visualElement, key);
    value.start(motionValue.animateMotionValue(key, value, valueTarget, visualElement.shouldReduceMotion && keysPosition.positionalKeys.has(key) ? { type: false } : valueTransition, visualElement, isHandoff));
    const animation = value.animation;
    if (animation) {
      animations.push(animation);
    }
  }
  if (transitionEnd) {
    Promise.all(animations).then(() => {
      frame.frame.update(() => {
        transitionEnd && setters.setTarget(visualElement, transitionEnd);
      });
    });
  }
  return animations;
}
exports.animateTarget = animateTarget;
//# sourceMappingURL=dist-es-animation-interfaces-visual-element-target.cjs.map
