"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const Group = require("../motion-dom/dist-es-animation-controls-Group.cjs");
const getValueTransition = require("../motion-dom/dist-es-animation-utils-get-value-transition.cjs");
require("../motion-utils/dist-es-errors.cjs");
const timeConversion = require("../motion-utils/dist-es-time-conversion.cjs");
const frame = require("./dist-es-frameloop-frame.cjs");
const AcceleratedAnimation = require("./dist-es-animation-animators-AcceleratedAnimation.cjs");
const MainThreadAnimation = require("./dist-es-animation-animators-MainThreadAnimation.cjs");
const getFinalKeyframe = require("./dist-es-animation-animators-waapi-utils-get-final-keyframe.cjs");
const defaultTransitions = require("./dist-es-animation-utils-default-transitions.cjs");
const isTransitionDefined = require("./dist-es-animation-utils-is-transition-defined.cjs");
const animateMotionValue = (name, value, target, transition = {}, element, isHandoff) => (onComplete) => {
  const valueTransition = getValueTransition.getValueTransition(transition, name) || {};
  const delay = valueTransition.delay || transition.delay || 0;
  let { elapsed = 0 } = transition;
  elapsed = elapsed - timeConversion.secondsToMilliseconds(delay);
  let options = {
    keyframes: Array.isArray(target) ? target : [null, target],
    ease: "easeOut",
    velocity: value.getVelocity(),
    ...valueTransition,
    delay: -elapsed,
    onUpdate: (v) => {
      value.set(v);
      valueTransition.onUpdate && valueTransition.onUpdate(v);
    },
    onComplete: () => {
      onComplete();
      valueTransition.onComplete && valueTransition.onComplete();
    },
    name,
    motionValue: value,
    element: isHandoff ? void 0 : element
  };
  if (!isTransitionDefined.isTransitionDefined(valueTransition)) {
    options = {
      ...options,
      ...defaultTransitions.getDefaultTransition(name, options)
    };
  }
  if (options.duration) {
    options.duration = timeConversion.secondsToMilliseconds(options.duration);
  }
  if (options.repeatDelay) {
    options.repeatDelay = timeConversion.secondsToMilliseconds(options.repeatDelay);
  }
  if (options.from !== void 0) {
    options.keyframes[0] = options.from;
  }
  let shouldSkip = false;
  if (options.type === false || options.duration === 0 && !options.repeatDelay) {
    options.duration = 0;
    if (options.delay === 0) {
      shouldSkip = true;
    }
  }
  if (shouldSkip && !isHandoff && value.get() !== void 0) {
    const finalKeyframe = getFinalKeyframe.getFinalKeyframe(options.keyframes, valueTransition);
    if (finalKeyframe !== void 0) {
      frame.frame.update(() => {
        options.onUpdate(finalKeyframe);
        options.onComplete();
      });
      return new Group.GroupPlaybackControls([]);
    }
  }
  if (!isHandoff && AcceleratedAnimation.AcceleratedAnimation.supports(options)) {
    return new AcceleratedAnimation.AcceleratedAnimation(options);
  } else {
    return new MainThreadAnimation.MainThreadAnimation(options);
  }
};
exports.animateMotionValue = animateMotionValue;
//# sourceMappingURL=dist-es-animation-interfaces-motion-value.cjs.map
