var m = u(), n = (e) => d(e, m), p = u();
n.write = (e) => d(e, p);
var s = u();
n.onStart = (e) => d(e, s);
var v = u();
n.onFrame = (e) => d(e, v);
var w = u();
n.onFinish = (e) => d(e, w);
var l = [];
n.setTimeout = (e, t) => {
  const a = n.now() + t, o = () => {
    const f = l.findIndex((L) => L.cancel == o);
    ~f && l.splice(f, 1), i -= ~f ? 1 : 0;
  }, c = { time: a, handler: e, cancel: o };
  return l.splice(F(a), 0, c), i += 1, Q(), c;
};
var F = (e) => ~(~l.findIndex((t) => t.time > e) || ~l.length);
n.cancel = (e) => {
  s.delete(e), v.delete(e), w.delete(e), m.delete(e), p.delete(e);
};
n.sync = (e) => {
  h = !0, n.batchedUpdates(e), h = !1;
};
n.throttle = (e) => {
  let t;
  function a() {
    try {
      e(...t);
    } finally {
      t = null;
    }
  }
  function o(...c) {
    t = c, n.onStart(a);
  }
  return o.handler = e, o.cancel = () => {
    s.delete(a), t = null;
  }, o;
};
var y = typeof window < "u" ? window.requestAnimationFrame : (
  // eslint-disable-next-line @typescript-eslint/no-empty-function
  (() => {
  })
);
n.use = (e) => y = e;
n.now = typeof performance < "u" ? () => performance.now() : Date.now;
n.batchedUpdates = (e) => e();
n.catch = console.error;
n.frameLoop = "always";
n.advance = () => {
  n.frameLoop !== "demand" || x();
};
var r = -1, i = 0, h = !1;
function d(e, t) {
  h ? (t.delete(e), e(0)) : (t.add(e), Q());
}
function Q() {
  r < 0 && (r = 0, n.frameLoop !== "demand" && y(S));
}
function b() {
  r = -1;
}
function S() {
  ~r && (y(S), n.batchedUpdates(x));
}
function x() {
  const e = r;
  r = n.now();
  const t = F(r);
  if (t && (z(l.splice(0, t), (a) => a.handler()), i -= t), !i) {
    b();
    return;
  }
  s.flush(), m.flush(e ? Math.min(64, r - e) : 16.667), v.flush(), p.flush(), w.flush();
}
function u() {
  let e = /* @__PURE__ */ new Set(), t = e;
  return {
    add(a) {
      i += t == e && !e.has(a) ? 1 : 0, e.add(a);
    },
    delete(a) {
      return i -= t == e && e.has(a) ? 1 : 0, e.delete(a);
    },
    flush(a) {
      t.size && (e = /* @__PURE__ */ new Set(), i -= t.size, z(t, (o) => o(a) && e.add(o)), i += e.size, t = e);
    }
  };
}
function z(e, t) {
  e.forEach((a) => {
    try {
      t(a);
    } catch (o) {
      n.catch(o);
    }
  });
}
export {
  n as raf
};
