import { setStyle as u } from "../render/dom/style-set.es.js";
import { supportsScrollTimeline as p } from "../utils/supports/scroll-timeline.es.js";
import { getFinalKeyframe as f } from "./keyframes/get-final.es.js";
import { WithPromise as d } from "./utils/WithPromise.es.js";
import { startWaapiAnimation as c } from "./waapi/start-waapi-animation.es.js";
import { applyGeneratorOptions as T } from "./waapi/utils/apply-generator.es.js";
import { invariant as y } from "../../../../../../motion-utils@12.36.0/node_modules/motion-utils/dist/es/errors.es.js";
import { millisecondsToSeconds as o, secondsToMilliseconds as S } from "../../../../../../motion-utils@12.36.0/node_modules/motion-utils/dist/es/time-conversion.es.js";
import { noop as g } from "../../../../../../motion-utils@12.36.0/node_modules/motion-utils/dist/es/noop.es.js";
class x extends d {
  constructor(i) {
    if (super(), this.finishedTime = null, this.isStopped = !1, this.manualStartTime = null, !i)
      return;
    const { element: t, name: e, keyframes: n, pseudoElement: s, allowFlatten: h = !1, finalKeyframe: l, onComplete: a } = i;
    this.isPseudoElement = !!s, this.allowFlatten = h, this.options = i, y(typeof i.type != "string", `Mini animate() doesn't support "type" as a string.`, "mini-spring");
    const m = T(i);
    this.animation = c(t, e, n, m, s), m.autoplay === !1 && this.animation.pause(), this.animation.onfinish = () => {
      if (this.finishedTime = this.time, !s) {
        const r = f(n, this.options, l, this.speed);
        this.updateMotionValue && this.updateMotionValue(r), u(t, e, r), this.animation.cancel();
      }
      a == null || a(), this.notifyFinished();
    };
  }
  play() {
    this.isStopped || (this.manualStartTime = null, this.animation.play(), this.state === "finished" && this.updateFinished());
  }
  pause() {
    this.animation.pause();
  }
  complete() {
    var i, t;
    (t = (i = this.animation).finish) == null || t.call(i);
  }
  cancel() {
    try {
      this.animation.cancel();
    } catch {
    }
  }
  stop() {
    if (this.isStopped)
      return;
    this.isStopped = !0;
    const { state: i } = this;
    i === "idle" || i === "finished" || (this.updateMotionValue ? this.updateMotionValue() : this.commitStyles(), this.isPseudoElement || this.cancel());
  }
  /**
   * WAAPI doesn't natively have any interruption capabilities.
   *
   * In this method, we commit styles back to the DOM before cancelling
   * the animation.
   *
   * This is designed to be overridden by NativeAnimationExtended, which
   * will create a renderless JS animation and sample it twice to calculate
   * its current value, "previous" value, and therefore allow
   * Motion to also correctly calculate velocity for any subsequent animation
   * while deferring the commit until the next animation frame.
   */
  commitStyles() {
    var t, e, n;
    const i = (t = this.options) == null ? void 0 : t.element;
    !this.isPseudoElement && (i != null && i.isConnected) && ((n = (e = this.animation).commitStyles) == null || n.call(e));
  }
  get duration() {
    var t, e;
    const i = ((e = (t = this.animation.effect) == null ? void 0 : t.getComputedTiming) == null ? void 0 : e.call(t).duration) || 0;
    return o(Number(i));
  }
  get iterationDuration() {
    const { delay: i = 0 } = this.options || {};
    return this.duration + o(i);
  }
  get time() {
    return o(Number(this.animation.currentTime) || 0);
  }
  set time(i) {
    const t = this.finishedTime !== null;
    this.manualStartTime = null, this.finishedTime = null, this.animation.currentTime = S(i), t && this.animation.pause();
  }
  /**
   * The playback speed of the animation.
   * 1 = normal speed, 2 = double speed, 0.5 = half speed.
   */
  get speed() {
    return this.animation.playbackRate;
  }
  set speed(i) {
    i < 0 && (this.finishedTime = null), this.animation.playbackRate = i;
  }
  get state() {
    return this.finishedTime !== null ? "finished" : this.animation.playState;
  }
  get startTime() {
    return this.manualStartTime ?? Number(this.animation.startTime);
  }
  set startTime(i) {
    this.manualStartTime = this.animation.startTime = i;
  }
  /**
   * Attaches a timeline to the animation, for instance the `ScrollTimeline`.
   */
  attachTimeline({ timeline: i, rangeStart: t, rangeEnd: e, observe: n }) {
    var s;
    return this.allowFlatten && ((s = this.animation.effect) == null || s.updateTiming({ easing: "linear" })), this.animation.onfinish = null, i && p() ? (this.animation.timeline = i, t && (this.animation.rangeStart = t), e && (this.animation.rangeEnd = e), g) : n(this);
  }
}
export {
  x as NativeAnimation
};
//# sourceMappingURL=NativeAnimation.es.js.map
