import { useSyncExternalStore } from "react";
const DEFAULT_INTERVAL_MS = 80;
let tick = 0;
let interval = null;
let intervalMs = DEFAULT_INTERVAL_MS;
const listeners = /* @__PURE__ */ new Set();
function start() {
  if (interval) return;
  interval = setInterval(() => {
    tick = (tick + 1) % 1e6;
    for (const listener of listeners) listener();
  }, intervalMs);
}
function stop() {
  if (!interval) return;
  clearInterval(interval);
  interval = null;
}
function subscribe(listener) {
  listeners.add(listener);
  start();
  return () => {
    listeners.delete(listener);
    if (listeners.size === 0) {
      stop();
    }
  };
}
function getSnapshot() {
  return tick;
}
function useAnimationTick(options) {
  const enabled = options?.enabled ?? true;
  const nextIntervalMs = options?.intervalMs ?? DEFAULT_INTERVAL_MS;
  if (nextIntervalMs < intervalMs) {
    intervalMs = nextIntervalMs;
    if (interval) {
      stop();
      start();
    }
  }
  return useSyncExternalStore(enabled ? subscribe : () => () => {
  }, getSnapshot, getSnapshot);
}
export {
  useAnimationTick
};
