import { jsxs as l, jsx as r, Fragment as T } from "react/jsx-runtime";
import { Box as s, Typography as y, Divider as F, IconButton as K, Collapse as W } from "@mui/material";
import O from "@arcblock/ux/lib/Avatar";
import Q from "@arcblock/ux/lib/DID";
import { useMemoizedFn as V } from "ahooks";
import { translate as Y } from "@arcblock/ux/lib/Locale/util";
import { useLocaleContext as $ } from "@arcblock/ux/lib/Locale/context";
import q from "lodash/noop";
import { useState as z, useEffect as C } from "react";
import j from "@arcblock/ux/lib/Toast";
import { parseURL as G, joinURL as H } from "ufo";
import { KeyboardArrowUp as J, KeyboardArrowDown as X } from "@mui/icons-material";
import { translations as Z } from "../../libs/locales.js";
import { formatAxiosError as I } from "../../libs/utils.js";
import { currentTimezone as _, getStatusDuration as P, isValidUrl as M, isDuplicateUrl as tt } from "./utils.js";
import rt from "./switch-role.js";
import et from "./metadata.js";
import ot from "./user-status.js";
import at from "./user-info.js";
import { client as R } from "../../../libs/client.js";
import nt from "./social-actions/index.js";
function Rt({
  user: t,
  isMyself: o = !0,
  showFullDid: f = !0,
  switchPassport: A,
  switchProfile: L,
  isMobile: a = !1,
  onlyProfile: c = !1,
  refreshProfile: u,
  isShowSocialActions: k = !1,
  ...m
}) {
  const { locale: h } = $(), [x, g] = z(void 0), v = V((i, e = {}) => Y(Z, i, h, "en", e)), [d, w] = z(!a || c);
  C(() => {
    g(t?.metadata?.status);
  }, [t]), C(() => {
    w(!a || c);
  }, [a, c]);
  const D = async (i) => {
    if (o)
      try {
        if (i) {
          const e = P(i);
          i.dateRange = e.length > 0 ? e : x?.dateRange ?? [];
        }
        g(i), await R.user.saveProfile({
          // @ts-ignore
          metadata: {
            ...t?.metadata ?? { joinedAt: t?.createdAt, email: t?.email, phone: t?.phone },
            status: i || {}
          }
        }), u();
      } catch (e) {
        console.error(e), j.error(I(e));
      }
  }, b = () => {
    w(!d);
  }, E = async (i) => {
    if (!o)
      return;
    const { metadata: e, address: B } = i;
    try {
      const p = e?.links?.map((n, S) => {
        if (!n.url || !M(n.url) || S > 0 && e.links?.slice(0, S)?.some((N) => tt(N.url, n.url)))
          return null;
        try {
          return G(n.url).protocol || (n.url = H("https://", n.url)), n;
        } catch {
          return console.error("Invalid URL:", n.url), null;
        }
      }).filter((n) => !!n) || [];
      e.links = p, await R.user.saveProfile({ metadata: e, address: B }), u();
    } catch (p) {
      console.error(p), j.error(I(p));
    }
  };
  return /* @__PURE__ */ l(
    s,
    {
      ...m,
      sx: {
        position: "relative",
        ...m.sx ?? {}
      },
      children: [
        /* @__PURE__ */ l(
          s,
          {
            className: "user-info",
            sx: {
              display: "flex",
              flexDirection: a ? "row" : "column",
              gap: 2
            },
            children: [
              /* @__PURE__ */ l(
                s,
                {
                  className: "user-avatar",
                  sx: {
                    position: "relative",
                    display: "flex",
                    alignItems: "center",
                    justifyContent: "space-between"
                  },
                  children: [
                    /* @__PURE__ */ r(
                      O,
                      {
                        src: t?.avatar,
                        did: t?.did,
                        size: m.size || (a ? 64 : 100),
                        variant: "circle",
                        shape: "circle",
                        sx: {
                          borderRadius: "100%",
                          backgroundColor: "#fff",
                          position: "relative",
                          overflow: "hidden",
                          flexShrink: 0,
                          ...o ? {
                            cursor: "pointer",
                            "&::after": {
                              content: `"${v("switchProfile")}"`,
                              color: "white",
                              position: "absolute",
                              fontSize: "12px",
                              bottom: 0,
                              left: 0,
                              right: 0,
                              height: "25%",
                              backgroundColor: "rgba(0, 0, 0, 0.3)",
                              display: "flex",
                              justifyContent: "center",
                              alignItems: "center"
                            }
                          } : {}
                        },
                        onClick: o ? L : q
                      }
                    ),
                    /* @__PURE__ */ r(
                      ot,
                      {
                        isMobile: a,
                        size: m.size || (a ? 64 : 100),
                        isMyself: o,
                        timezone: t?.metadata?.timezone || _,
                        status: x,
                        onChange: D
                      }
                    )
                  ]
                }
              ),
              /* @__PURE__ */ l(
                s,
                {
                  sx: {
                    flex: 1,
                    overflow: "hidden"
                  },
                  children: [
                    /* @__PURE__ */ l(
                      y,
                      {
                        variant: "h6",
                        component: "div",
                        sx: {
                          flex: 1,
                          fontWeight: 600,
                          display: "flex",
                          alignItems: "center",
                          gap: 1,
                          fontSize: "24px !important",
                          flexWrap: "wrap"
                        },
                        children: [
                          /* @__PURE__ */ r(
                            "span",
                            {
                              style: {
                                overflow: "hidden",
                                textOverflow: "ellipsis",
                                whiteSpace: "nowrap"
                              },
                              children: t?.fullName
                            }
                          ),
                          o ? /* @__PURE__ */ r(rt, { user: t, switchPassport: A }) : null
                        ]
                      }
                    ),
                    /* @__PURE__ */ r(
                      Q,
                      {
                        did: t.did,
                        size: 16,
                        showQrcode: !0,
                        copyable: !0,
                        compact: !f,
                        responsive: !f,
                        locale: h
                      }
                    )
                  ]
                }
              )
            ]
          }
        ),
        !o && k ? /* @__PURE__ */ r(s, { sx: { mt: 2 }, children: /* @__PURE__ */ r(nt, { user: t }) }) : null,
        /* @__PURE__ */ r(et, { isMobile: a, isMyself: o, user: t, onSave: E }),
        o ? /* @__PURE__ */ l(T, { children: [
          /* @__PURE__ */ r(F, { sx: { my: a ? 1 : 3 } }),
          a && !c ? /* @__PURE__ */ r(
            s,
            {
              sx: {
                display: "flex",
                justifyContent: "center",
                mb: 0
              },
              children: /* @__PURE__ */ r(
                K,
                {
                  size: "small",
                  onClick: b,
                  sx: {
                    backgroundColor: "grey.50",
                    "&:hover": {
                      backgroundColor: "grey.50",
                      opacity: 0.8
                    }
                  },
                  children: d ? /* @__PURE__ */ r(J, {}) : /* @__PURE__ */ r(X, {})
                }
              )
            }
          ) : null,
          /* @__PURE__ */ l(W, { in: d, timeout: "auto", children: [
            /* @__PURE__ */ r(
              y,
              {
                component: "p",
                sx: {
                  color: "text.secondary",
                  fontSize: "14px",
                  mb: 2
                },
                children: v("profile.justForYou")
              }
            ),
            /* @__PURE__ */ r(at, { user: t, isMySelf: o })
          ] })
        ] }) : null
      ]
    }
  );
}
export {
  Rt as default
};
