"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
require("../motion-utils/dist-es-errors.cjs");
const noop = require("../motion-utils/dist-es-noop.cjs");
const timeConversion = require("../motion-utils/dist-es-time-conversion.cjs");
const isGenerator = require("../motion-dom/dist-es-animation-generators-utils-is-generator.cjs");
const attachTimeline = require("../motion-dom/dist-es-animation-waapi-utils-attach-timeline.cjs");
const easing = require("../motion-dom/dist-es-animation-waapi-utils-easing.cjs");
const linearEasing = require("../motion-dom/dist-es-utils-supports-linear-easing.cjs");
const anticipate = require("./dist-es-easing-anticipate.cjs");
const back = require("./dist-es-easing-back.cjs");
const circ = require("./dist-es-easing-circ.cjs");
const DOMKeyframesResolver = require("./dist-es-render-dom-DOMKeyframesResolver.cjs");
const BaseAnimation = require("./dist-es-animation-animators-BaseAnimation.cjs");
const MainThreadAnimation = require("./dist-es-animation-animators-MainThreadAnimation.cjs");
const acceleratedValues = require("./dist-es-animation-animators-utils-accelerated-values.cjs");
const index = require("./dist-es-animation-animators-waapi-index.cjs");
const getFinalKeyframe = require("./dist-es-animation-animators-waapi-utils-get-final-keyframe.cjs");
const supportsWaapi = require("./dist-es-animation-animators-waapi-utils-supports-waapi.cjs");
const sampleDelta = 10;
const maxDuration = 2e4;
function requiresPregeneratedKeyframes(options) {
  return isGenerator.isGenerator(options.type) || options.type === "spring" || !easing.isWaapiSupportedEasing(options.ease);
}
function pregenerateKeyframes(keyframes, options) {
  const sampleAnimation = new MainThreadAnimation.MainThreadAnimation({
    ...options,
    keyframes,
    repeat: 0,
    delay: 0,
    isGenerator: true
  });
  let state = { done: false, value: keyframes[0] };
  const pregeneratedKeyframes = [];
  let t = 0;
  while (!state.done && t < maxDuration) {
    state = sampleAnimation.sample(t);
    pregeneratedKeyframes.push(state.value);
    t += sampleDelta;
  }
  return {
    times: void 0,
    keyframes: pregeneratedKeyframes,
    duration: t - sampleDelta,
    ease: "linear"
  };
}
const unsupportedEasingFunctions = {
  anticipate: anticipate.anticipate,
  backInOut: back.backInOut,
  circInOut: circ.circInOut
};
function isUnsupportedEase(key) {
  return key in unsupportedEasingFunctions;
}
class AcceleratedAnimation extends BaseAnimation.BaseAnimation {
  constructor(options) {
    super(options);
    const { name, motionValue, element, keyframes } = this.options;
    this.resolver = new DOMKeyframesResolver.DOMKeyframesResolver(keyframes, (resolvedKeyframes, finalKeyframe) => this.onKeyframesResolved(resolvedKeyframes, finalKeyframe), name, motionValue, element);
    this.resolver.scheduleResolve();
  }
  initPlayback(keyframes, finalKeyframe) {
    let { duration = 300, times, ease, type, motionValue, name, startTime } = this.options;
    if (!motionValue.owner || !motionValue.owner.current) {
      return false;
    }
    if (typeof ease === "string" && linearEasing.supportsLinearEasing() && isUnsupportedEase(ease)) {
      ease = unsupportedEasingFunctions[ease];
    }
    if (requiresPregeneratedKeyframes(this.options)) {
      const { onComplete, onUpdate, motionValue: motionValue2, element, ...options } = this.options;
      const pregeneratedAnimation = pregenerateKeyframes(keyframes, options);
      keyframes = pregeneratedAnimation.keyframes;
      if (keyframes.length === 1) {
        keyframes[1] = keyframes[0];
      }
      duration = pregeneratedAnimation.duration;
      times = pregeneratedAnimation.times;
      ease = pregeneratedAnimation.ease;
      type = "keyframes";
    }
    const animation = index.startWaapiAnimation(motionValue.owner.current, name, keyframes, { ...this.options, duration, times, ease });
    animation.startTime = startTime !== null && startTime !== void 0 ? startTime : this.calcStartTime();
    if (this.pendingTimeline) {
      attachTimeline.attachTimeline(animation, this.pendingTimeline);
      this.pendingTimeline = void 0;
    } else {
      animation.onfinish = () => {
        const { onComplete } = this.options;
        motionValue.set(getFinalKeyframe.getFinalKeyframe(keyframes, this.options, finalKeyframe));
        onComplete && onComplete();
        this.cancel();
        this.resolveFinishedPromise();
      };
    }
    return {
      animation,
      duration,
      times,
      type,
      ease,
      keyframes
    };
  }
  get duration() {
    const { resolved } = this;
    if (!resolved)
      return 0;
    const { duration } = resolved;
    return timeConversion.millisecondsToSeconds(duration);
  }
  get time() {
    const { resolved } = this;
    if (!resolved)
      return 0;
    const { animation } = resolved;
    return timeConversion.millisecondsToSeconds(animation.currentTime || 0);
  }
  set time(newTime) {
    const { resolved } = this;
    if (!resolved)
      return;
    const { animation } = resolved;
    animation.currentTime = timeConversion.secondsToMilliseconds(newTime);
  }
  get speed() {
    const { resolved } = this;
    if (!resolved)
      return 1;
    const { animation } = resolved;
    return animation.playbackRate;
  }
  set speed(newSpeed) {
    const { resolved } = this;
    if (!resolved)
      return;
    const { animation } = resolved;
    animation.playbackRate = newSpeed;
  }
  get state() {
    const { resolved } = this;
    if (!resolved)
      return "idle";
    const { animation } = resolved;
    return animation.playState;
  }
  get startTime() {
    const { resolved } = this;
    if (!resolved)
      return null;
    const { animation } = resolved;
    return animation.startTime;
  }
  /**
   * Replace the default DocumentTimeline with another AnimationTimeline.
   * Currently used for scroll animations.
   */
  attachTimeline(timeline) {
    if (!this._resolved) {
      this.pendingTimeline = timeline;
    } else {
      const { resolved } = this;
      if (!resolved)
        return noop.noop;
      const { animation } = resolved;
      attachTimeline.attachTimeline(animation, timeline);
    }
    return noop.noop;
  }
  play() {
    if (this.isStopped)
      return;
    const { resolved } = this;
    if (!resolved)
      return;
    const { animation } = resolved;
    if (animation.playState === "finished") {
      this.updateFinishedPromise();
    }
    animation.play();
  }
  pause() {
    const { resolved } = this;
    if (!resolved)
      return;
    const { animation } = resolved;
    animation.pause();
  }
  stop() {
    this.resolver.cancel();
    this.isStopped = true;
    if (this.state === "idle")
      return;
    this.resolveFinishedPromise();
    this.updateFinishedPromise();
    const { resolved } = this;
    if (!resolved)
      return;
    const { animation, keyframes, duration, type, ease, times } = resolved;
    if (animation.playState === "idle" || animation.playState === "finished") {
      return;
    }
    if (this.time) {
      const { motionValue, onUpdate, onComplete, element, ...options } = this.options;
      const sampleAnimation = new MainThreadAnimation.MainThreadAnimation({
        ...options,
        keyframes,
        duration,
        type,
        ease,
        times,
        isGenerator: true
      });
      const sampleTime = timeConversion.secondsToMilliseconds(this.time);
      motionValue.setWithVelocity(sampleAnimation.sample(sampleTime - sampleDelta).value, sampleAnimation.sample(sampleTime).value, sampleDelta);
    }
    const { onStop } = this.options;
    onStop && onStop();
    this.cancel();
  }
  complete() {
    const { resolved } = this;
    if (!resolved)
      return;
    resolved.animation.finish();
  }
  cancel() {
    const { resolved } = this;
    if (!resolved)
      return;
    resolved.animation.cancel();
  }
  static supports(options) {
    const { motionValue, name, repeatDelay, repeatType, damping, type } = options;
    if (!motionValue || !motionValue.owner || !(motionValue.owner.current instanceof HTMLElement)) {
      return false;
    }
    const { onUpdate, transformTemplate } = motionValue.owner.getProps();
    return supportsWaapi.supportsWaapi() && name && acceleratedValues.acceleratedValues.has(name) && /**
     * If we're outputting values to onUpdate then we can't use WAAPI as there's
     * no way to read the value from WAAPI every frame.
     */
    !onUpdate && !transformTemplate && !repeatDelay && repeatType !== "mirror" && damping !== 0 && type !== "inertia";
  }
}
exports.AcceleratedAnimation = AcceleratedAnimation;
//# sourceMappingURL=dist-es-animation-animators-AcceleratedAnimation.cjs.map
