import { defineComponent as m, ref as r, onMounted as S, onUpdated as p, onBeforeUnmount as w, provide as C, renderSlot as y } from "vue";
import g from "@master/css-runtime";
const I = /* @__PURE__ */ m({
  __name: "CSSRuntimeProvider",
  props: {
    config: {},
    root: {}
  },
  setup(f) {
    const o = f, i = r(!1), a = r(0), t = r(), s = r(!1), u = async () => {
      if (o.config instanceof Promise) {
        const e = await o.config;
        return (e == null ? void 0 : e.config) || (e == null ? void 0 : e.default) || e;
      } else
        return o.config;
    }, l = async (e) => {
      i.value = !0;
      const n = o.root ?? document, c = globalThis.runtimeCSSs.find((d) => d.root === n);
      c ? (t.value = c, s.value = !0) : t.value = new g(n, e ?? await u()).observe(), i.value = !1;
    }, v = async () => {
      i.value && await new Promise((e) => {
        const n = setInterval(() => {
          i.value || (clearInterval(n), e());
        }, 10);
      });
    };
    return S(async () => {
      typeof window > "u" || await l();
    }), p(async () => {
      const e = ++a.value;
      if (await v(), e !== a.value)
        return;
      const n = await u();
      t.value.root !== o.root && (o.root || t.value.root !== document) ? (t.value.destroy(), await l(n)) : t.value.refresh(n);
    }), w(async () => {
      var e;
      s.value || (e = t.value) == null || e.destroy();
    }), C("runtime-css", t), (e, n) => y(e.$slots, "default");
  }
});
export {
  I as CSSRuntimeProvider,
  I as default
};
