import { useState as u, useRef as g, useCallback as d, useEffect as C } from "react";
let o = { credits: 0 };
const i = /* @__PURE__ */ new Set();
let s = null, r = null, l = !1;
async function h() {
  const t = "/editor/config";
  try {
    const e = await fetch(t);
    return e.status === 200 && e.headers.get("content-type")?.startsWith("application/json") ? await e.json() ?? {} : (console.warn("useConfig: ignoring invalid response"), {});
  } catch (e) {
    return console.error("useConfig: fetch error", e), {};
  }
}
async function a() {
  const t = await h();
  o = {
    ...o,
    ...t
  }, i.forEach((e) => e(o));
}
function p() {
  r || (r = setInterval(() => {
    a().catch((t) => {
      console.error("useConfig: polling error", t);
    });
  }, 2e4));
}
function b() {
  r && (clearInterval(r), r = null);
}
function w() {
  const [t, e] = u(o), n = g(null), c = d((f) => {
    e(f);
  }, []);
  return C(() => (n.current || (n.current = c), i.add(n.current), l || (l = !0, s || (s = a().finally(() => {
    s = null;
  })), p()), () => {
    n.current && i.delete(n.current), i.size === 0 && (b(), l = !1);
  }), [c]), t;
}
export {
  w as useUserConfig
};
//# sourceMappingURL=use-user-config.js.map
