import { jsx as p } from "react/jsx-runtime";
import o from "prop-types";
import { CircularProgress as _ } from "@mui/material";
import { useReactive as w, useMemoizedFn as I, useCreation as f } from "ahooks";
import { detectWalletExtension as C, openWebWallet as E } from "@arcblock/ux/lib/Util";
import { mergeSx as O } from "@arcblock/ux/lib/Util/style";
import P from "lodash/noop";
import v from "lodash/omit";
import { useImperativeHandle as A } from "react";
import y from "@arcblock/ux/lib/DIDConnect/provider-icon";
import { LOGIN_PROVIDER_ICON_SIZE as a, LOGIN_PROVIDER as T } from "@arcblock/ux/lib/Util/constant";
import N from "./login-method-item.js";
import { useStateContext as U } from "../../contexts/state.js";
import { EXT_DOWNLOAD_URL as g } from "../../../constant.js";
import j from "../../hooks/auth-url.js";
function q({
  ref: x = null,
  tokenState: r,
  locale: D = "en",
  webWalletUrl: b,
  onClick: h = P,
  disableSwitchApp: W = !1,
  autoConnect: t = !0,
  ...u
}) {
  const { browserBrand: l } = U(), s = C(), n = w({
    loading: !1
  }), R = j({ disableSwitchApp: W, tokenState: r }), c = I(() => {
    if (!t)
      return;
    n.loading = !0, E({
      webWalletUrl: b,
      url: R,
      locale: D,
      appInfo: r.appInfo,
      memberAppInfo: r.memberAppInfo
    })?.type === "web" ? n.loading = !1 : setTimeout(() => {
      n.loading = !1;
    }, 3e3);
  }), m = I((e) => {
    r.url && (h(e), e?.preventDefault(), c());
  });
  A(x, () => ({
    connect: c
  }));
  const i = f(() => l === "edge" ? g.edge : l === "chrome" ? g.chrome : null, [l]), d = f(() => {
    const e = {};
    return s || !t ? e.onClick = m : i && t ? (e.component = "a", e.href = i, e.target = "_blank", e.rel = "noopener") : e.onClick = m, r.url || (e.sx = {
      cursor: "not-allowed"
    }), e;
  }, [m, r.url]), L = f(() => t ? s || i ? "DID Wallet (Extension)" : "DID Wallet (Web)" : "DID Wallet", [s, i]);
  return /* @__PURE__ */ p(
    N,
    {
      ...u,
      sx: O(
        {
          textDecoration: "none"
        },
        u?.sx,
        d.sx
      ),
      title: L,
      icon: n.loading || !r.url ? /* @__PURE__ */ p(
        _,
        {
          color: "primary",
          size: a,
          sx: {
            "& svg": {
              transform: "scale(0.75)"
            }
          }
        }
      ) : /* @__PURE__ */ p(
        y,
        {
          sx: {
            width: a,
            height: a,
            transform: "scale(0.9)"
          },
          provider: T.DID_WALLET
        }
      ),
      ...v(d, "sx")
    }
  );
}
q.propTypes = {
  tokenState: o.object.isRequired,
  locale: o.string,
  webWalletUrl: o.string.isRequired,
  onClick: o.func,
  disableSwitchApp: o.bool,
  autoConnect: o.bool,
  // 点击后是否自动连接，默认true
  ref: o.any
};
export {
  q as default
};
