import { GroupPlaybackControls } from "../motion-dom/dist-es-animation-controls-Group.js";
import { getValueTransition } from "../motion-dom/dist-es-animation-utils-get-value-transition.js";
import "../motion-utils/dist-es-errors.js";
import { secondsToMilliseconds } from "../motion-utils/dist-es-time-conversion.js";
import { frame } from "./dist-es-frameloop-frame.js";
import { AcceleratedAnimation } from "./dist-es-animation-animators-AcceleratedAnimation.js";
import { MainThreadAnimation } from "./dist-es-animation-animators-MainThreadAnimation.js";
import { getFinalKeyframe } from "./dist-es-animation-animators-waapi-utils-get-final-keyframe.js";
import { getDefaultTransition } from "./dist-es-animation-utils-default-transitions.js";
import { isTransitionDefined } from "./dist-es-animation-utils-is-transition-defined.js";
const animateMotionValue = (name, value, target, transition = {}, element, isHandoff) => (onComplete) => {
  const valueTransition = getValueTransition(transition, name) || {};
  const delay = valueTransition.delay || transition.delay || 0;
  let { elapsed = 0 } = transition;
  elapsed = elapsed - 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(valueTransition)) {
    options = {
      ...options,
      ...getDefaultTransition(name, options)
    };
  }
  if (options.duration) {
    options.duration = secondsToMilliseconds(options.duration);
  }
  if (options.repeatDelay) {
    options.repeatDelay = 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(options.keyframes, valueTransition);
    if (finalKeyframe !== void 0) {
      frame.update(() => {
        options.onUpdate(finalKeyframe);
        options.onComplete();
      });
      return new GroupPlaybackControls([]);
    }
  }
  if (!isHandoff && AcceleratedAnimation.supports(options)) {
    return new AcceleratedAnimation(options);
  } else {
    return new MainThreadAnimation(options);
  }
};
export {
  animateMotionValue
};
//# sourceMappingURL=dist-es-animation-interfaces-motion-value.js.map
