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