import { jsx as B } from "react/jsx-runtime";
import { useMemoizedFn as d, useAsyncEffect as h } from "ahooks";
import { createContext as D, useState as c, use as p } from "react";
import { getBlockletData as r, getFederatedEnabled as w, getMaster as b } from "../Util/federated.js";
const s = D({
  blocklet: null,
  masterBlocklet: null
}), { Provider: g, Consumer: M } = s;
function j({
  children: l,
  baseUrl: e = "",
  loading: i = null
}) {
  const [a, u] = c(null), [k, m] = c(null), f = d(async () => {
    if (!e || window.location.href.startsWith(e))
      throw new Error("no blocklet data");
    return await r(e);
  });
  return h(async () => {
    let t = null, n = null;
    try {
      t = await f();
    } catch {
      t = globalThis.blocklet || globalThis.env;
    }
    if (t && w(t)) {
      const o = b(t);
      o && (n = await r(o.appUrl));
    }
    u(t), m(n);
  }, [e]), /* @__PURE__ */ B(g, { value: { blocklet: a, masterBlocklet: k }, children: a ? l : i || null });
}
function E() {
  const { blocklet: l, masterBlocklet: e } = p(s);
  return { blocklet: l, masterBlocklet: e };
}
export {
  M as BlockletConsumer,
  s as BlockletContext,
  j as BlockletProvider,
  E as useBlockletContext
};
