const R = {
  skipAnimations: !1,
  useManualTiming: !1
};
class p {
  constructor() {
    this.order = [], this.scheduled = /* @__PURE__ */ new Set();
  }
  add(e) {
    if (!this.scheduled.has(e))
      return this.scheduled.add(e), this.order.push(e), !0;
  }
  remove(e) {
    const s = this.order.indexOf(e);
    s !== -1 && (this.order.splice(s, 1), this.scheduled.delete(e));
  }
  clear() {
    this.order.length = 0, this.scheduled.clear();
  }
}
function T(a) {
  let e = new p(), s = new p(), n = 0, t = !1, o = !1;
  const f = /* @__PURE__ */ new WeakSet(), d = {
    /**
     * Schedule a process to run on the next frame.
     */
    schedule: (c, l = !1, u = !1) => {
      const r = u && t, i = r ? e : s;
      return l && f.add(c), i.add(c) && r && t && (n = e.order.length), c;
    },
    /**
     * Cancel the provided callback from running on the next frame.
     */
    cancel: (c) => {
      s.remove(c), f.delete(c);
    },
    /**
     * Execute all schedule callbacks.
     */
    process: (c) => {
      if (t) {
        o = !0;
        return;
      }
      if (t = !0, [e, s] = [s, e], s.clear(), n = e.order.length, n)
        for (let l = 0; l < n; l++) {
          const u = e.order[l];
          f.has(u) && (d.schedule(u), a()), u(c);
        }
      t = !1, o && (o = !1, d.process(c));
    }
  };
  return d;
}
const h = [
  "prepare",
  "read",
  "update",
  "preRender",
  "render",
  "postRender"
], q = 40;
function A(a, e) {
  let s = !1, n = !0;
  const t = {
    delta: 0,
    timestamp: 0,
    isProcessing: !1
  }, o = h.reduce((r, i) => (r[i] = T(() => s = !0), r), {}), f = (r) => {
    o[r].process(t);
  }, d = () => {
    const r = performance.now();
    s = !1, t.delta = n ? 1e3 / 60 : Math.max(Math.min(r - t.timestamp, q), 1), t.timestamp = r, t.isProcessing = !0, h.forEach(f), t.isProcessing = !1, s && e && (n = !1, a(d));
  }, c = () => {
    s = !0, n = !0, t.isProcessing || a(d);
  };
  return { schedule: h.reduce((r, i) => {
    const x = o[i];
    return r[i] = (M, P = !1, E = !1) => (s || c(), x.schedule(M, P, E)), r;
  }, {}), cancel: (r) => h.forEach((i) => o[i].cancel(r)), state: t, steps: o };
}
const S = (a) => a, { schedule: v, cancel: g, state: w, steps: C } = A(typeof requestAnimationFrame < "u" ? requestAnimationFrame : S, !0);
let m;
function k() {
  m = void 0;
}
const F = {
  now: () => (m === void 0 && F.set(w.isProcessing || R.useManualTiming ? w.timestamp : performance.now()), m),
  set: (a) => {
    m = a, queueMicrotask(k);
  }
};
function O(a, e) {
  const s = F.now(), n = ({ timestamp: t }) => {
    const o = t - s;
    o >= e && (g(n), a(o - e));
  };
  return v.read(n, !0), () => g(n);
}
export {
  R as M,
  g as a,
  w as b,
  A as c,
  O as d,
  v as f,
  S as n,
  C as s,
  F as t
};
