import o from "lodash/isEmpty";
const a = {};
function i(e = window.blocklet) {
  return (e?.settings?.federated || {}).master;
}
function s(e = window.blocklet) {
  return (e?.settings?.federated || {}).config;
}
function d(e = window.blocklet) {
  if (!e)
    return !1;
  const p = i(e);
  return !p || p.appPid === e.appPid ? !1 : s(e)?.status === "approved";
}
function w(e = window.blocklet) {
  return i(e)?.appPid;
}
function c(e = window.blocklet) {
  const p = i(e);
  return !o(p) ? {
    appId: p.appId,
    appName: p.appName,
    appDescription: p.appDescription,
    appLogo: p.appLogo,
    appPid: p.appPid,
    appUrl: p.appUrl,
    version: p.version,
    sourceAppPid: p.appPid,
    provider: "wallet"
  } : null;
}
function u(e = window.blocklet) {
  return e ? {
    appId: e.appId,
    appName: e.appName,
    appDescription: e.appDescription,
    appLogo: e.appLogo,
    appPid: e.appPid,
    appUrl: e.appUrl,
    // 该判断条件用于限制非统一登录模式下，当前访问的 url 与应用设置的主 url 不一致情况时有效
    // 页面的请求优先会向当前访问的域名发送，避免产生 cors 的问题
    // 而在统一登录模式下，这个值不应该提供，因为它始终会需要跨域的请求
    currentAppUrl: e === window.blocklet ? window.location.origin : void 0,
    version: e.version,
    // NOTICE: null 代表该值置空
    sourceAppPid: null,
    provider: "wallet"
  } : window.env ? (e = window.env, {
    appId: e.appId,
    appName: e.appName,
    appDescription: e.appDescription,
    appUrl: e.baseUrl,
    // 在 server 的登录环境下，始终提供当前的 url 作为 appUrl
    currentAppUrl: window.location.origin,
    // NOTICE: null 代表该值置空
    sourceAppPid: null,
    provider: "wallet",
    type: "server"
  }) : null;
}
function g(e) {
  const p = [], r = c(e), n = u(e), t = d(e);
  return n && p.push(n), r && r?.appId !== n?.appId && t && p.push(r), p.reverse();
}
async function l(e, p = !1) {
  if (!p && a[e])
    return a[e];
  try {
    const r = new URL("__blocklet__.js", e);
    r.searchParams.set("type", "json");
    const t = await (await fetch(r.href)).json();
    return a[e] = t, t;
  } catch (r) {
    return console.error(`Failed to get blocklet data: ${e}`, r), null;
  }
}
export {
  g as getApps,
  l as getBlockletData,
  s as getConfig,
  u as getCurrentApp,
  c as getFederatedApp,
  d as getFederatedEnabled,
  i as getMaster,
  w as getSourceAppPid
};
