import { jsxs as y, jsx as t } from "react/jsx-runtime";
import { Typography as g, Box as m, Grid as C, useTheme as k } from "@mui/material";
import { useCreation as h, useMemoizedFn as L } from "ahooks";
import T from "@emotion/styled";
import _ from "lodash/isArray";
import { Icon as $ } from "@iconify/react";
import j from "@iconify-icons/tabler/info-circle";
import S from "@arcblock/icons/lib/Link";
import E from "@arcblock/icons/lib/Phone";
import M from "@arcblock/icons/lib/Location";
import P from "@arcblock/icons/lib/Email";
import A from "@arcblock/icons/lib/Timezone";
import { withoutProtocol as V } from "ufo";
import { useCallback as R, useMemo as D } from "react";
import G from "./clock.js";
const Z = {
  timezone: A,
  email: P,
  phone: E,
  location: M,
  link: S
};
function q(e) {
  return V(e);
}
function x(e) {
  return e ? {
    filter: "brightness(0) saturate(100%) invert(1)"
  } : {};
}
const p = {
  width: 16,
  height: 16
};
function H({ value: e }) {
  const a = k().palette.mode === "dark";
  return /* @__PURE__ */ y(
    m,
    {
      className: "user-card__timezone-field",
      sx: {
        display: "flex",
        alignItems: "center",
        gap: 1
      },
      children: [
        /* @__PURE__ */ t(A, { ...p, style: x(a) }),
        /* @__PURE__ */ t(G, { value: e, variant: "body2", color: "grey.800" })
      ]
    }
  );
}
function J({ value: e }) {
  const a = k().palette.mode === "dark";
  return /* @__PURE__ */ y(m, { display: "flex", alignItems: "center", gap: 1, className: "user-card__link-field", children: [
    /* @__PURE__ */ t(S, { ...p, style: x(a) }),
    /* @__PURE__ */ t(v, { variant: "body2", color: "grey.800", children: /* @__PURE__ */ t(
      g,
      {
        component: "a",
        href: e,
        style: { textDecoration: "none", color: "inherit", fontSize: "inherit" },
        target: "_blank",
        variant: "body2",
        rel: "noopener noreferrer",
        sx: {
          color: "grey.800"
        },
        children: q(e)
      }
    ) })
  ] });
}
function N({ field: e, value: s, children: a = null }) {
  const d = Z[e], u = k().palette.mode === "dark", c = x(u);
  return /* @__PURE__ */ y(
    m,
    {
      className: `user-card__${e}-field`,
      sx: {
        display: "flex",
        alignItems: "center",
        gap: 1
      },
      children: [
        d ? /* @__PURE__ */ t(d, { ...p, style: c }) : /* @__PURE__ */ t($, { icon: j, ...p, ...c }),
        /* @__PURE__ */ t(v, { variant: "body2", color: "grey.800", children: a ?? s })
      ]
    },
    e
  );
}
function le({ user: e, renderFields: s = void 0 }) {
  const a = h(() => s ?? ["bio", "email", "phone", "location", "timezone", "link"], [s]), d = h(() => a.includes("bio"), [a]), l = h(() => e.metadata ?? {
    joinedAt: e?.createdAt,
    email: e?.email,
    phone: {
      country: "cn",
      phoneNumber: e?.phone ?? ""
    }
  }, [e]), u = h(() => e.address, [e.address]), c = R(
    (r) => {
      if (!r) return "";
      switch (r) {
        case "bio":
          return e.metadata?.bio || "";
        case "email":
          return l.email || e.email || "";
        case "phone":
          return l.phone?.phoneNumber || e.phone || "";
        case "location":
          return u?.city || l.location || "";
        case "timezone":
          return l.timezone || "";
        case "link":
          return l.links?.map((i) => i.url).filter(Boolean) || [];
        default:
          return e[r] || "";
      }
    },
    [e, l, u]
  ), { effectiveRenderCount: B } = D(() => {
    const r = a.filter((n) => {
      if (n === "bio") return !1;
      const o = c(n);
      return o == null || o === "" ? !1 : _(o) ? o.length > 0 : String(o).trim().length > 0;
    }), i = r.reduce((n, o) => {
      const f = c(o);
      return n + (Array.isArray(f) ? f.filter((w) => w && String(w).trim()).length : 1);
    }, 0);
    return { visibleFields: r, effectiveRenderCount: i };
  }, [a, c]), F = B > 4, b = L(
    (r) => r != null && String(r).trim().length > 0
  ), I = D(() => {
    const r = [];
    return a.forEach((i) => {
      if (i === "bio") return;
      const n = c(i);
      n && (_(n) ? n.forEach((o, f) => {
        b(o) && r.push({
          field: i,
          value: o,
          key: `${i}-${f}`
        });
      }) : b(n) && r.push({
        field: i,
        value: n,
        key: i
      }));
    }), r;
  }, [a, c, b]);
  if (a.length === 0)
    return null;
  const z = (r) => {
    const { field: i, value: n, key: o } = r;
    switch (i) {
      case "link":
        return /* @__PURE__ */ t(J, { value: n }, o);
      case "timezone":
        return /* @__PURE__ */ t(H, { value: n }, o);
      case "email":
        return /* @__PURE__ */ t(
          N,
          {
            field: i,
            value: n,
            children: /* @__PURE__ */ t(
              g,
              {
                component: "a",
                href: `mailto:${n}`,
                rel: "noopener noreferrer",
                referrerPolicy: "no-referrer",
                style: {
                  color: "inherit",
                  textDecoration: "none",
                  fontSize: "inherit"
                },
                children: n
              }
            )
          }
        );
      default:
        return /* @__PURE__ */ t(N, { field: i, value: n }, o);
    }
  };
  return /* @__PURE__ */ y(
    m,
    {
      className: "user-card__basic-content",
      sx: {
        mt: 1,
        display: "flex",
        flexDirection: "column",
        gap: 1.5
      },
      children: [
        d && e.metadata?.bio && /* @__PURE__ */ t(v, { variant: "body2", color: "grey.800", className: "user-card__bio-field", children: e.metadata.bio }),
        F ? /* @__PURE__ */ t(C, { container: !0, spacing: 0.5, children: I.map((r) => /* @__PURE__ */ t(C, { size: 6, children: z(r) }, r.key)) }) : /* @__PURE__ */ t(
          m,
          {
            sx: {
              display: "flex",
              flexDirection: "column",
              gap: 0.5
            },
            children: I.map((r) => z(r))
          }
        )
      ]
    }
  );
}
const v = T(g)`
  flex: 1;
  display: -webkit-box;
  line-height: 1.5;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-word;
`;
export {
  le as default
};
