import { time as y } from "../frameloop/sync-time.es.js";
import { JSAnimation as g } from "./JSAnimation.es.js";
import { getFinalKeyframe as R } from "./keyframes/get-final.es.js";
import { KeyframeResolver as T, flushKeyframeResolvers as _ } from "./keyframes/KeyframesResolver.es.js";
import { NativeAnimationExtended as w } from "./NativeAnimationExtended.es.js";
import { canAnimate as K } from "./utils/can-animate.es.js";
import { makeAnimationInstant as D } from "./utils/make-animation-instant.es.js";
import { WithPromise as E } from "./utils/WithPromise.es.js";
import { supportsBrowserAnimation as k } from "./waapi/supports/waapi.es.js";
import { MotionGlobalConfig as x } from "../../../../../../motion-utils@12.36.0/node_modules/motion-utils/dist/es/global-config.es.js";
import { noop as M } from "../../../../../../motion-utils@12.36.0/node_modules/motion-utils/dist/es/noop.es.js";
const V = 40;
class J extends E {
  constructor({ autoplay: t = !0, delay: o = 0, type: n = "keyframes", repeat: c = 0, repeatDelay: d = 0, repeatType: p = "loop", keyframes: u, name: r, motionValue: m, element: e, ...h }) {
    var l;
    super(), this.stop = () => {
      var a, i;
      this._animation && (this._animation.stop(), (a = this.stopTimeline) == null || a.call(this)), (i = this.keyframeResolver) == null || i.cancel();
    }, this.createdAt = y.now();
    const v = {
      autoplay: t,
      delay: o,
      type: n,
      repeat: c,
      repeatDelay: d,
      repeatType: p,
      name: r,
      motionValue: m,
      element: e,
      ...h
    }, s = (e == null ? void 0 : e.KeyframeResolver) || T;
    this.keyframeResolver = new s(u, (a, i, f) => this.onKeyframesResolved(a, i, v, !f), r, m, e), (l = this.keyframeResolver) == null || l.scheduleResolve();
  }
  onKeyframesResolved(t, o, n, c) {
    var f, A;
    this.keyframeResolver = void 0;
    const { name: d, type: p, velocity: u, delay: r, isHandoff: m, onUpdate: e } = n;
    this.resolvedAt = y.now();
    let h = !0;
    K(t, d, p, u) || (h = !1, (x.instantAnimations || !r) && (e == null || e(R(t, n, o))), t[0] = t[t.length - 1], D(n), n.repeat = 0);
    const s = {
      startTime: c ? this.resolvedAt ? this.resolvedAt - this.createdAt > V ? this.resolvedAt : this.createdAt : this.createdAt : void 0,
      finalKeyframe: o,
      ...n,
      keyframes: t
    }, l = h && !m && k(s), a = (A = (f = s.motionValue) == null ? void 0 : f.owner) == null ? void 0 : A.current;
    let i;
    if (l)
      try {
        i = new w({
          ...s,
          element: a
        });
      } catch {
        i = new g(s);
      }
    else
      i = new g(s);
    i.finished.then(() => {
      this.notifyFinished();
    }).catch(M), this.pendingTimeline && (this.stopTimeline = i.attachTimeline(this.pendingTimeline), this.pendingTimeline = void 0), this._animation = i;
  }
  get finished() {
    return this._animation ? this.animation.finished : this._finished;
  }
  then(t, o) {
    return this.finished.finally(t).then(() => {
    });
  }
  get animation() {
    var t;
    return this._animation || ((t = this.keyframeResolver) == null || t.resume(), _()), this._animation;
  }
  get duration() {
    return this.animation.duration;
  }
  get iterationDuration() {
    return this.animation.iterationDuration;
  }
  get time() {
    return this.animation.time;
  }
  set time(t) {
    this.animation.time = t;
  }
  get speed() {
    return this.animation.speed;
  }
  get state() {
    return this.animation.state;
  }
  set speed(t) {
    this.animation.speed = t;
  }
  get startTime() {
    return this.animation.startTime;
  }
  attachTimeline(t) {
    return this._animation ? this.stopTimeline = this.animation.attachTimeline(t) : this.pendingTimeline = t, () => this.stop();
  }
  play() {
    this.animation.play();
  }
  pause() {
    this.animation.pause();
  }
  complete() {
    this.animation.complete();
  }
  cancel() {
    var t;
    this._animation && this.animation.cancel(), (t = this.keyframeResolver) == null || t.cancel();
  }
}
export {
  J as AsyncMotionValueAnimation
};
//# sourceMappingURL=AsyncMotionValueAnimation.es.js.map
