import { __rest as M } from "../../../../tslib/tslib.es6.es.js";
import { detectAnimationFromOptions as U } from "./utils/detect-animation-from-options.es.js";
import k, { cancelSync as w } from "../../../../framesync/dist/es/index.es.js";
import { interpolate as A } from "../utils/interpolate.es.js";
import { hasRepeatDelayElapsed as I, reverseElapsed as N, loopElapsed as q } from "./utils/elapsed.es.js";
const z = (n) => {
  const o = ({ delta: a }) => n(a);
  return {
    start: () => k.update(o, !0),
    stop: () => w.update(o)
  };
};
function L(n) {
  var o, a, { from: p, autoplay: O = !0, driver: P = z, elapsed: e = 0, repeat: R = 0, repeatType: x = "loop", repeatDelay: u = 0, onPlay: c, onStop: v, onComplete: D, onRepeat: T, onUpdate: y } = n, l = M(n, ["from", "autoplay", "driver", "elapsed", "repeat", "repeatType", "repeatDelay", "onPlay", "onStop", "onComplete", "onRepeat", "onUpdate"]);
  let { to: i } = l, s, m = 0, t = l.duration, d, f = !1, r = !0, b;
  const g = U(l);
  !((a = (o = g).needsInterpolation) === null || a === void 0) && a.call(o, p, i) && (b = A([0, 100], [p, i], {
    clamp: !1
  }), p = 0, i = 100);
  const E = g(Object.assign(Object.assign({}, l), { from: p, to: i }));
  function S() {
    m++, x === "reverse" ? (r = m % 2 === 0, e = N(e, t, u, r)) : (e = q(e, t, u), x === "mirror" && E.flipTarget()), f = !1, T && T();
  }
  function _() {
    s.stop(), D && D();
  }
  function h(C) {
    if (r || (C = -C), e += C, !f) {
      const F = E.next(Math.max(0, e));
      d = F.value, b && (d = b(d)), f = r ? F.done : e <= 0;
    }
    y == null || y(d), f && (m === 0 && (t != null || (t = e)), m < R ? I(e, t, u, r) && S() : _());
  }
  function j() {
    c == null || c(), s = P(h), s.start();
  }
  return O && j(), {
    stop: () => {
      v == null || v(), s.stop();
    }
  };
}
export {
  L as animate
};
//# sourceMappingURL=index.es.js.map
