import { jsx as o } from "react/jsx-runtime";
import { useState as I, useMemo as P } from "react";
import a from "prop-types";
import { ErrorBoundary as z } from "react-error-boundary";
import { Shape as D } from "@arcblock/did-motif";
import { Box as u } from "@mui/material";
import n from "lodash/isUndefined";
import m from "../Img/index.js";
import { mergeProps as E, isEthereumDid as R } from "../Util/index.js";
import M from "./did-motif.js";
import T from "./etherscan-blockies.js";
import { DID_PREFIX as f } from "../Util/constant.js";
function d({ ...s }) {
  const r = Object.assign({}, s);
  n(r.did) && (r.did = ""), n(r.size) && (r.size = 36), n(r.variant) && (r.variant = "default"), n(r.animation) && (r.animation = !1), n(r.shape) && (r.shape = ""), n(r.blockiesPadding) && (r.blockiesPadding = !0), n(r.responsive) && (r.responsive = !1);
  const [l, h] = I(!1), g = E(r, d, []), { did: i = "", size: e, src: p, variant: b, animation: v, shape: x, blockiesPadding: y, responsive: k, ...t } = g, c = P(() => R(i) ? T.createIcon({
    seed: i.replace(f, "").toLowerCase(),
    size: 8,
    scale: 16
  }).toDataURL() : null, [i]);
  if (p && !l)
    return /* @__PURE__ */ o(
      m,
      {
        width: e,
        src: p,
        alt: i,
        onError: () => h(!0),
        ...t,
        className: `avatar-img--${b} ${t?.className || ""}`,
        useProxyFallback: !0,
        sx: {
          "& .avatar-img--rounded": {
            borderRadius: "4px",
            overflow: "hidden"
          },
          "& .avatar-img--circle": {
            borderRadius: "100%",
            overflow: "hidden"
          },
          ...t.sx
        }
      }
    );
  if (c) {
    if (y) {
      const w = e > 24 ? 4 : 2;
      return /* @__PURE__ */ o(
        u,
        {
          ...t,
          sx: {
            display: "flex",
            alignItems: "center",
            width: e,
            height: e,
            ".blocky-icon-inner": {
              boxSizing: "border-box",
              display: "flex",
              justifyContent: "center",
              alignItems: "center",
              width: e,
              height: e * 0.7,
              borderRadius: `${Math.min(10, Math.floor(0.1 * e + 2))}px`,
              background: "#ddd"
            },
            ...t.sx
          },
          children: /* @__PURE__ */ o("div", { className: "blocky-icon-inner", children: /* @__PURE__ */ o(m, { width: e * 0.7 - w * 2, src: c, alt: i }) })
        }
      );
    }
    return /* @__PURE__ */ o(m, { ...t, width: e, src: c, alt: i, style: { marginRight: 4 } });
  }
  if (i)
    return /* @__PURE__ */ o(
      M,
      {
        did: i.replace(f, ""),
        size: e,
        animation: v,
        shape: D[(x || "").toUpperCase()],
        responsive: k,
        ...t
      }
    );
  throw new Error(`Invalid DID: ${i}`);
}
d.propTypes = {
  did: a.string,
  size: a.number,
  variant: a.oneOf(["circle", "rounded", "default"]),
  // animation 仅对 did motif 有效
  animation: a.bool,
  // shape 仅对 did motif 有效, 明确指定 motif shape, 而非由 did role type 自动推断 shape
  shape: a.oneOf(["", "rectangle", "square", "hexagon", "circle"]),
  blockiesPadding: a.bool,
  responsive: a.bool,
  useProxyFallback: a.bool
};
function $(s) {
  const r = s.size || 36, l = { rounded: "4px", circle: "100%" }[s.variant] || 0;
  return /* @__PURE__ */ o(
    z,
    {
      fallbackRender: () => /* @__PURE__ */ o(
        u,
        {
          sx: {
            width: r,
            height: r,
            bgcolor: "grey.300",
            borderRadius: l
          }
        }
      ),
      children: /* @__PURE__ */ o(d, { ...s })
    }
  );
}
$.propTypes = d.propTypes;
export {
  d as AvatarProps,
  $ as default
};
