import { jsxs as s, jsx as r, Fragment as z } from "react/jsx-runtime";
import { useRef as T, useEffect as _ } from "react";
import { useMemoizedFn as n, useCreation as j, useReactive as G } from "ahooks";
import f from "@arcblock/bridge";
import { Box as k, IconButton as H, Popper as N, ClickAwayListener as Q, Fade as V, Paper as q, Divider as S, MenuList as J, MenuItem as K } from "@mui/material";
import { Icon as X } from "@iconify/react";
import Y from "@iconify-icons/material-symbols/person-outline-rounded";
import Z from "@iconify-icons/material-symbols/filter-vintage-outline-rounded";
import $ from "@iconify-icons/material-symbols/account-circle-off-outline-rounded";
import ee from "@iconify-icons/material-symbols/featured-seasonal-and-gifts-rounded";
import re from "copy-to-clipboard";
import h from "lodash/noop";
import te from "../../Toast/index.js";
import oe from "../../Avatar/index.js";
import { getUserAvatar as ie } from "../../Util/index.js";
import ne from "./user-info.js";
import { DASHBOARD_URL as ce, PROFILE_URL as le } from "../../Util/constant.js";
import ae from "../../SessionPermission/index.js";
import { translations as se } from "../libs/translation.js";
import { translate as pe } from "../../Locale/util.js";
import ue from "./did-space.js";
import { mergeSx as de } from "../../Util/style.js";
import { createDebug as me } from "../../Util/logger.js";
import { GA_LAST_ROLE as fe } from "../../withTracker/constant/index.js";
const he = (e) => {
  const c = new URL(window.location.href);
  return c.searchParams.set("inviter", e), c.toString();
}, g = me("did-connect");
function _e({
  session: e,
  onBindWallet: c = h,
  isBlocklet: l = !0,
  locale: u = "en",
  size: b = 24,
  popperType: I = "click",
  profileUrl: y = le,
  sx: R,
  ...C
}) {
  const a = n((t, o = {}) => pe(se, t, u, "en", o)), L = j(() => !!globalThis?.blocklet?.settings?.invite?.enabled, []), P = T(null), d = G({
    open: !1
  }), i = n((t = !d.open) => {
    d.open = t;
  }), B = I === "hover" ? { onMouseEnter: () => i(!0), onMouseLeave: () => i(!1) } : { onClick: () => i() }, D = ie(e.user?.avatar?.replace(/\s/g, encodeURIComponent(" "))), O = e.useOAuth(), E = typeof e.usePasskey == "function" ? e.usePasskey() : null, v = n(({ inArcSphere: t = !1 } = {}) => {
    localStorage.setItem(fe, e?.user?.role);
    const o = {};
    t && e?.user?.sourceAppPid && (o.sourceAppPid = e.user.sourceAppPid), i(!1), e?.user?.sourceProvider === "passkey" ? E.switchPassport(e.user) : ["google", "apple", "email", "github"].includes(e?.user?.sourceProvider) ? O.switchOAuthPassport(e.user) : e.switchPassport(h, o);
  }), x = n(({ userSession: t, inArcSphere: o = !1 } = {}) => new Promise((W) => {
    const A = {};
    o && e.user.sourceAppPid && (A.sourceAppPid = e.user.sourceAppPid);
    const m = {};
    t ? m.userSession = t : (i(!1), m.showQuickConnect = !1), e.switchDid(
      () => {
        i(!1), W();
      },
      A,
      m
    );
  })), w = n(({ inArcSphere: t = !1 } = {}) => {
    const o = {};
    t && e.user.sourceAppPid && (o.sourceAppPid = e.user.sourceAppPid), i(!1), e.switchProfile(h, o);
  }), M = n(() => {
    i(!1), e.logout();
  }), U = n(() => {
    i(!1), e.bindWallet(c);
  }), F = n(() => {
    i(!1);
    const t = he(e.user.did);
    re(t), te.success(a("inviteCopied"));
  });
  return _(() => {
    f.registerBlocklet("callSwitchPassport", () => {
      g("bridge registerBlocklet: callSwitchPassport"), v({ inArcSphere: !0 });
    }), f.registerBlocklet("callSwitchDid", () => {
      g("bridge registerBlocklet: callSwitchDid"), x({ inArcSphere: !0 });
    }), f.registerBlocklet("callSwitchProfile", () => {
      g("bridge registerBlocklet: callSwitchProfile"), w({ inArcSphere: !0 });
    });
  }, []), /* @__PURE__ */ s(
    k,
    {
      sx: de(
        {
          display: "inline-flex",
          alignItems: "center",
          justifyContent: "center"
        },
        // @ts-ignore
        R
      ),
      ...C,
      children: [
        /* @__PURE__ */ r(
          H,
          {
            ref: P,
            size: "medium",
            "data-cy": "sessionManager-logout-popup",
            className: "arc-session-user-logged-in",
            "aria-label": "User info button",
            ...B,
            children: /* @__PURE__ */ r(oe, { variant: "circle", did: e.user.did, src: D, size: b, shape: "circle" })
          }
        ),
        /* @__PURE__ */ r(
          N,
          {
            open: d.open,
            anchorEl: P.current,
            transition: !0,
            placement: "bottom-end",
            sx: {
              zIndex: 1600
            },
            children: ({ TransitionProps: t }) => /* @__PURE__ */ r(
              Q,
              {
                onClickAway: (o) => {
                  o.preventDefault(), o.stopPropagation(), i(!1);
                },
                children: /* @__PURE__ */ r(V, { ...t, timeout: 350, children: /* @__PURE__ */ s(
                  q,
                  {
                    variant: "outlined",
                    sx: {
                      borderRadius: 1.5,
                      width: 350,
                      maxWidth: "90vw"
                    },
                    children: [
                      /* @__PURE__ */ r(
                        ne,
                        {
                          locale: u,
                          isBlocklet: l,
                          session: e,
                          onSwitchPassport: v,
                          onSwitchAccount: x,
                          onSwitchProfile: w,
                          onBindWallet: U
                        }
                      ),
                      /* @__PURE__ */ r(S, { sx: { m: "0 !important" } }),
                      /* @__PURE__ */ s(J, { sx: { p: 0 }, children: [
                        l ? /* @__PURE__ */ s(z, { children: [
                          /* @__PURE__ */ r(ae, { session: e, children: /* @__PURE__ */ r(
                            p,
                            {
                              icon: Z,
                              title: a("dashboard"),
                              component: "a",
                              href: ce,
                              sx: { display: "block", textDecoration: "none", color: "inherit" }
                            }
                          ) }),
                          /* @__PURE__ */ r(
                            p,
                            {
                              icon: Y,
                              title: a("profile"),
                              component: "a",
                              href: y,
                              sx: { display: "block", textDecoration: "none", color: "inherit" }
                            }
                          ),
                          L && /* @__PURE__ */ r(
                            p,
                            {
                              icon: ee,
                              title: a("invite"),
                              component: "div",
                              onClick: F,
                              sx: { display: "block", color: "inherit" }
                            }
                          ),
                          /* @__PURE__ */ r(S, { sx: { m: "0 !important" } }),
                          /* @__PURE__ */ r(ue, { session: e, locale: u })
                        ] }) : null,
                        /* @__PURE__ */ r(
                          p,
                          {
                            icon: $,
                            title: a("logout"),
                            component: "div",
                            sx: { color: "error.main" },
                            onClick: M,
                            "data-cy": "sessionManager-logout-trigger"
                          }
                        )
                      ] })
                    ]
                  }
                ) })
              }
            )
          }
        )
      ]
    }
  );
}
function p({
  icon: e,
  title: c,
  ...l
}) {
  return /* @__PURE__ */ r(k, { ...l, sx: { p: 0.5, ...l?.sx }, children: /* @__PURE__ */ s(
    K,
    {
      sx: {
        display: "flex",
        gap: 1,
        alignItems: "center",
        borderRadius: 1,
        "&:hover": {
          backgroundColor: "action.hover"
        },
        py: 1
      },
      children: [
        /* @__PURE__ */ r(X, { icon: e, fontSize: 24 }),
        c
      ]
    }
  ) });
}
export {
  _e as default
};
