import { useBrowser as C } from "@arcblock/react-hooks";
import { useRequest as T, useCreation as p } from "ahooks";
import n from "lodash/isNil";
import { LOGIN_PROVIDER as s } from "@arcblock/ux/lib/Util/constant";
import { getFederatedEnabled as y, getMaster as D, getBlockletData as I } from "@arcblock/ux/lib/Util/federated";
import { useState as R } from "react";
const S = async ({ sourceAppPid: d }) => {
  const t = globalThis?.blocklet;
  if (!t)
    return {
      [s.WALLET]: {
        order: 0,
        enabled: !0,
        showQrcode: !0
      },
      [s.PASSKEY]: {
        order: 1,
        enabled: !0
      }
    };
  const l = y(t), e = D(t);
  return l && e?.appPid && d === e?.appPid ? (await I(e.appUrl))?.settings?.authentication || {} : t?.settings?.authentication || {};
}, B = async ({ sourceAppPid: d } = {}) => {
  const t = await S({ sourceAppPid: d });
  return Object.entries(t).map(([e, o]) => ({ ...o, provider: e })).filter((e) => e.enabled).sort((e, o) => !n(e?.order) && !n(o?.order) ? e.order - o.order : n(e?.order) ? 1 : -1);
};
function Y({
  allowWallet: d = !0,
  passkeyBehavior: t = "none",
  action: l,
  sourceAppPid: e,
  mode: o = "dialog",
  blocklet: P = globalThis.blocklet,
  isSmallView: L = !1,
  lastLoginMethod: a = ""
}) {
  const [m, g] = R(!1), w = ["login", "invite", "connect-to-did-space", "connect-to-did-domain", "destroy-self"], E = ["login", "invite", "connect-to-did-space", "connect-to-did-domain", "destroy-self"], c = C(), { data: v = [] } = T(
    async () => {
      g(!0);
      const i = await B({ sourceAppPid: e });
      return g(!1), i;
    },
    { refreshDeps: [e] }
  ), f = p(() => {
    const i = globalThis?.blocklet?.DID_CONNECT_ENABLED_PROVIDERS?.split(",") || [];
    return v.filter((r) => i.length > 0 && !i.includes(r.provider) ? !1 : r.provider === s.WALLET ? d : r.provider === s.PASSKEY ? t !== "none" && !c.wallet && !c.arcSphere : r.provider === s.EMAIL ? E.includes(l) && (P?.settings?.notification?.email?.enabled ?? !1) : !(r.type === "oauth" && !w.includes(l))).sort((r, u) => {
      if (a) {
        const h = r.provider === a, b = u.provider === a;
        if (h && !b)
          return -1;
        if (!h && b)
          return 1;
      }
      return !n(r?.order) && !n(u?.order) ? r.order - u.order : n(r?.order) ? n(u?.order) ? 0 : 1 : -1;
    });
  }, [v, a]), A = p(() => {
    const i = o === "drawer" || L && c.mobile.any;
    return globalThis?.blocklet ? !!(f.find((r) => r.provider === s.WALLET)?.showQrcode !== !0 || i) : !!i;
  }, [o, L, f]);
  return {
    hideChooseList: p(() => !1),
    hideQRCode: A,
    providerList: f,
    loadingProviderList: m
  };
}
export {
  Y as default
};
