import { jsxs as w, Fragment as Q, jsx as t } from "react/jsx-runtime";
import { getDIDMotifInfo as O } from "@arcblock/did-motif";
import { types as E } from "@ocap/mcrypto";
import j from "qrcode.react";
import { Icon as q } from "@iconify/react";
import X from "@iconify-icons/material-symbols/qr-code-rounded";
import { Box as H, Dialog as Y, DialogContent as G, Typography as P } from "@mui/material";
import { useMemoizedFn as b, useCreation as D } from "ahooks";
import { useRef as J, useState as K, useImperativeHandle as V } from "react";
import { toAddress as Z } from "@arcblock/did";
import ee from "lodash/noop";
import W from "../Address/index.js";
import oe from "../Avatar/index.js";
import { DID_PREFIX as re } from "../Util/constant.js";
import { translate as te } from "../Locale/util.js";
import { isEthereumDid as ne, getDIDColor as se } from "../Util/index.js";
const ie = {
  en: {
    scanQrcode: "Scan with DID Wallet to view the {role}",
    download: "Download",
    downloadTips: "Don't have DID Wallet ?"
  },
  zh: {
    scanQrcode: "使用 DID Wallet 扫码查看该 {role}",
    download: "下载",
    downloadTips: "没有 DID Wallet ？"
  }
}, y = "xenon-2020-01-15", S = {
  main: y,
  "main.abtnetwork.io": y,
  "beta.abtnetwork.io": "beta"
}, ae = (e, r, o) => o ? se(e) : r.color, de = (e) => !{
  0: !0,
  // ACCOUNT
  6: !0,
  // ASSET
  17: !0
  // TOKEN
}[e], le = (e) => {
  if (e === E.RoleType.ROLE_ANY)
    return "DID";
  const [r] = Object.entries(E.RoleType).find((o) => o[1] === e) || [];
  return r ? r.toLowerCase().replace(/role_(\S)/g, (o, n) => n.toUpperCase()) : "DID";
}, ce = (e, r, o) => r || de(e.roleType) ? o * 0.75 : o;
function M({ did: e, showAvatar: r }) {
  const o = ne(e), n = o ? void 0 : O(e), u = D(() => o ? e : `${re}${e}`, [e, o]), c = b((s, a) => [
    /* @__PURE__ */ t(
      "span",
      {
        style: { flex: "0 0 auto", color: ae(e, n, o) },
        children: "DID:"
      },
      `${a}-prefix-did-${s}`
    ),
    /* @__PURE__ */ w("span", { className: "did-address-text", children: [
      o ? "ETH" : "ABT",
      ":"
    ] }, `${a}-prefix-abt-${s}`),
    r && /* @__PURE__ */ t(
      oe,
      {
        src: "",
        did: e,
        size: ce(n, o, s || 18),
        style: { display: "inline-flex", alignItems: "center", marginLeft: 2, marginRight: 4 },
        blockiesPadding: !1,
        className: "did-address-avatar",
        sx: { flexShrink: 0 }
      },
      `${a}-avatar-${s}`
    )
  ]);
  return {
    isEthDid: o,
    didMotifInfo: n,
    content: u,
    getPrepend: c
  };
}
function $e({
  ref: e = void 0,
  ...r
}) {
  const {
    did: o,
    chainId: n,
    roleType: u,
    showAvatar: c = !0,
    showQrcode: s = !1,
    locale: a = "en",
    size: v = 0,
    component: d = "span",
    copyable: A = !0,
    responsive: m = !0,
    showCopyButtonInTooltip: p = !1,
    inline: f = !1,
    append: R = null,
    compact: T = !1,
    startChars: g = 6,
    endChars: $ = 6,
    style: h,
    className: _,
    sx: l
  } = r, i = Z(o), x = {
    size: v,
    component: d,
    copyable: A,
    responsive: m,
    showCopyButtonInTooltip: p,
    inline: f,
    append: R,
    compact: T,
    startChars: g,
    endChars: $,
    style: h,
    className: _,
    sx: l
  }, N = J(null), { content: k, getPrepend: F } = M({ did: i, showAvatar: c }), [L, I] = K(!1), U = b(() => {
    I(!1);
  }), B = b((C) => {
    C.stopPropagation(), C.preventDefault(), I(!0);
  });
  return V(e, () => new Proxy(
    {
      openQRCode: () => I(!0),
      closeQRCode: () => I(!1)
    },
    {
      get(C, z) {
        return C[z] || N?.current?.[z];
      }
    }
  )), /* @__PURE__ */ w(Q, { children: [
    /* @__PURE__ */ t(
      W,
      {
        locale: a,
        content: k,
        ...x,
        ref: N,
        prepend: F(x.size, "address"),
        append: /* @__PURE__ */ w(Q, { children: [
          s ? /* @__PURE__ */ t(
            H,
            {
              id: "did-qrcode-button",
              component: q,
              icon: X,
              onClick: B,
              sx: {
                flexShrink: 0,
                cursor: "pointer",
                ml: 0.5,
                color: "text.secondary"
              }
            }
          ) : null,
          x.append
        ] }),
        children: i
      },
      "address"
    ),
    /* @__PURE__ */ t(
      pe,
      {
        did: i,
        chainId: n,
        roleType: u,
        showAvatar: c,
        open: L,
        onClose: U,
        ...x
      }
    )
  ] });
}
function pe({
  did: e,
  chainId: r = void 0,
  roleType: o = void 0,
  showAvatar: n = !0,
  // DialogProps
  open: u,
  fullScreen: c = !1,
  fullWidth: s = !1,
  scroll: a = "paper",
  onClose: v = ee,
  // AddressProps
  locale: d = "en",
  ...A
}) {
  const m = b((l, i = {}) => te(ie, l, d, "en", i)), { isEthDid: p, content: f, didMotifInfo: R, getPrepend: T } = M({ did: e, showAvatar: n }), g = D(() => ["zh", "en"].includes(d) ? `https://www.didwallet.io/${d}` : "https://www.didwallet.io/en", [d]), $ = D(() => /* @__PURE__ */ w(Q, { children: [
    m("downloadTips"),
    " ",
    /* @__PURE__ */ t(
      H,
      {
        component: "a",
        href: g,
        target: "_blank",
        rel: "noreferrer",
        sx: {
          color: "secondary.main",
          textDecoration: "none",
          "&:hover": {
            textDecoration: "underline"
          }
        },
        children: m("download")
      }
    )
  ] }), [g]), h = D(() => {
    if (r)
      return S[r] || r;
    if (p) return "1";
    try {
      const l = window.blocklet?.CHAIN_HOST, i = new URL(l).hostname;
      if (i)
        return S[i] || y;
    } catch (l) {
      console.warn("Failed to parse chainHost:", l);
    }
    return y;
  }, [p, r]), _ = D(() => p ? `ethereum:abtwallet.io/i?address=${f}&action=didRecognize&chainID=${h}` : `abt://abtwallet.io/i?did=${f}&action=didRecognize&chainID=${h}`, [f, p, h]);
  return /* @__PURE__ */ t(
    Y,
    {
      open: u,
      onClose: v,
      fullScreen: c,
      fullWidth: s,
      scroll: a,
      maxWidth: "sm",
      PaperProps: {
        sx: {
          boxShadow: "none",
          borderRadius: "12px"
        }
      },
      children: /* @__PURE__ */ w(G, { align: "center", sx: { p: 3 }, children: [
        /* @__PURE__ */ t(P, { sx: { mb: 2, fontWeight: 500, fontSize: 18 }, children: m("scanQrcode", { role: le(o || R?.roleType) }) }),
        /* @__PURE__ */ t(
          j,
          {
            value: _,
            size: 256,
            renderAs: "svg",
            level: "M"
          }
        ),
        /* @__PURE__ */ t(H, { sx: { mt: 1.5, textAlign: "center" }, children: /* @__PURE__ */ t(
          W,
          {
            locale: d,
            content: f,
            prepend: T(16, "dialog"),
            ...A,
            size: 16,
            copyable: !0,
            children: e
          },
          "dialog-address"
        ) }),
        /* @__PURE__ */ t(
          P,
          {
            variant: "body2",
            sx: {
              color: "text.secondary",
              fontSize: "12px",
              mt: 1
            },
            children: $
          }
        )
      ] })
    }
  );
}
export {
  $e as DID,
  pe as DIDDialog,
  $e as default
};
