import { jsx as m } from "react/jsx-runtime";
import n from "prop-types";
import { CircularProgress as T } from "@mui/material";
import { useReactive as E, useCreation as W, useMemoizedFn as a } from "ahooks";
import _ from "@arcblock/bridge";
import { useBrowser as N } from "@arcblock/react-hooks";
import q from "lodash/noop";
import { useImperativeHandle as F } from "react";
import { mergeSx as M } from "@arcblock/ux/lib/Util/style";
import j from "@arcblock/ux/lib/DIDConnect/provider-icon";
import { LOGIN_PROVIDER_ICON_SIZE as p, LOGIN_PROVIDER as z } from "@arcblock/ux/lib/Util/constant";
import { useStateContext as G } from "../../contexts/state.js";
import J from "./login-method-item.js";
import { logger as b } from "../../../utils.js";
import K from "../../hooks/auth-url.js";
function V({
  isTablet: w = !1,
  ref: C = null,
  tokenState: e,
  locale: P,
  tokenKey: y,
  onClick: R = q,
  disableSwitchApp: A = !1,
  ...f
}) {
  const l = E({
    loading: !1
  }), { isWalletWebview: d } = G(), L = "DID Wallet", c = N(), h = new URL(window.location.href), x = K({ disableSwitchApp: A, tokenState: e }), u = W(() => {
    if (!e.url)
      return "";
    h.searchParams.set("tokenKey", e.token);
    const o = new URL(x);
    c.wallet || (o.searchParams.set("callback", encodeURIComponent(h)), o.searchParams.set("callback_delay", 1500)), o.searchParams.set("locale", P);
    const g = new TextEncoder(), D = o.href.length;
    let r = "", i = "";
    try {
      if (e.appInfo) {
        const t = g.encode(JSON.stringify(e.appInfo));
        r = btoa(String.fromCharCode(...t));
      }
      if (e.memberAppInfo) {
        const t = g.encode(JSON.stringify(e.memberAppInfo));
        i = btoa(String.fromCharCode(...t));
      }
      if (D + 8 + r.length + 14 + i.length > 2e3)
        throw b.warn("URL too long, drop appInfo & memberAppInfo", { appInfo: r, memberAppInfo: i }), new Error("URL too long");
      i ? r && (o.searchParams.set("appInfo", r), o.searchParams.set("memberAppInfo", i)) : r && o.searchParams.set("appInfo", r);
    } catch (t) {
      b.warn("Failed to convert appInfo & memberAppInfo", t);
    }
    return o.href.replace(/^https?:\/\//, "abt://");
  }, [y, e.token, e.appInfo, e.memberAppInfo, e.url]), O = a(() => {
    l.loading = !0, setTimeout(() => {
      _.call("authAction", { action: "auth", deepLink: u });
    }, 600), setTimeout(() => {
      l.loading = !1;
    }, 2e3);
  }), U = a(() => {
    e.checking = !0, e.status = "scanned";
  }), s = {};
  !d && c.mobile.any && (s.component = "a", s.href = u), e.url || (s.sx = {
    cursor: "not-allowed"
  });
  const I = a((o) => {
    e.url && (d ? O() : c.mobile.any || w ? U() : o());
  }), v = a(() => {
    I(R);
  });
  return F(C, () => ({
    connect: I
  })), /* @__PURE__ */ m(
    J,
    {
      ...f,
      ...s,
      sx: M(f?.sx, s?.sx),
      title: L,
      icon: l.loading || !e.url ? /* @__PURE__ */ m(
        T,
        {
          color: "primary",
          size: p,
          sx: {
            "& svg": {
              transform: "scale(0.75)"
            }
          }
        }
      ) : /* @__PURE__ */ m(
        j,
        {
          provider: z.DID_WALLET,
          sx: {
            width: p,
            height: p,
            transform: "scale(0.9)"
          }
        }
      ),
      iconScale: 1,
      onClick: v
    }
  );
}
V.propTypes = {
  tokenState: n.object.isRequired,
  tokenKey: n.string.isRequired,
  locale: n.string.isRequired,
  onClick: n.func,
  disableSwitchApp: n.bool,
  ref: n.any,
  isTablet: n.bool
};
export {
  V as default
};
