import { L as h } from "./LoginIcon-409b27ef.mjs";
import e, { useMemo as N, useState as y, useRef as I, useEffect as b } from "react";
import { Trans as d } from "react-i18next";
import { h as w, B as S, b as F, a as U, e as A } from "./useRoutes-367960a3.mjs";
import { u as T, c as L, F as E } from "./Env-92498313.mjs";
import { IamUserAuthorizationClient as B, EligibleUser as R } from "@accelbyte/sdk-iam";
import { C as H } from "./CommonError-b56f12ad.mjs";
import { A as W, a as $ } from "./ArrowIcon-afdb4626.mjs";
import { a as G } from "./LoadingPage-d6d2cc0b.mjs";
import { P as V } from "./ProfileImage-64f9d3cd.mjs";
import p from "classnames";
import { a as v, d as M } from "./index-28007ca6.mjs";
import { L as O } from "./LogoutIcon-947c7425.mjs";
import { N as x } from "./index-35107a02.mjs";
import "react-feather";
import "flat";
import "i18next";
import "date-fns";
import "@accelbyte/sdk";
import "./utils.mjs";
import "zod";
import "uuid";
import "axios";
import "@accelbyte/sdk-basic";
import "./ErrorLayout-d232f96c.mjs";
import "./UrlUtils-1bad2fa2.mjs";
import "./merge-78dc87d6.mjs";
import "@accelbyte/sdk-legal";
import "react-dom";
import "buffer";
import "react-toastify";
import "@accelbyte/sdk-gdpr";
const z = ({ currentLocation: t, returnPath: s }) => {
  const { state: a } = w(), [o] = T(), i = !t.includes(a.error.index.link) && t || null, r = () => {
    const c = new B(o).createLoginURL(s || i);
    window.location.replace(c);
  };
  return /* @__PURE__ */ e.createElement("div", { className: "loginButtonContainer" }, /* @__PURE__ */ e.createElement(S, { className: "loginButton", buttonType: F.TEXT, buttonStyle: U.WHITE, onClick: r }, /* @__PURE__ */ e.createElement(h, null), /* @__PURE__ */ e.createElement(d, { i18nKey: "Navigation.links.login" }, "Login")));
};
const D = ({
  user: t,
  userProfile: s,
  isFetching: a,
  wallet: o,
  error: n,
  toggleMenu: i,
  isMenuShown: r,
  isDisableUsername: c
}) => {
  const {
    state: { flags: l },
    hasFeature: m
  } = v(), u = N(() => m("isAvatarVisible"), [l]), f = () => R.safeParse(t).success ? c ? t.displayName || t.userName : t.userName || t.displayName : null;
  return /* @__PURE__ */ e.createElement("div", { className: p("userProfileDropdown", r && "open"), onClick: () => i() }, !a && n && /* @__PURE__ */ e.createElement(H, { error: n }), !a && !n ? /* @__PURE__ */ e.createElement(e.Fragment, null, u && /* @__PURE__ */ e.createElement(V, { imageLink: s && s.avatarUrl || "", className: "userProfileImage" }), /* @__PURE__ */ e.createElement("div", { className: "userProfileContainer" }, /* @__PURE__ */ e.createElement("p", { className: "userProfileDisplayName" }, f()), o && /* @__PURE__ */ e.createElement("p", { className: "userProfileWallet" }, o.currencySymbol, " ", o.balance)), /* @__PURE__ */ e.createElement(W, { className: p($.down, "arrowSize") })) : /* @__PURE__ */ e.createElement(G, null));
};
const K = ({ onLogout: t, showMenu: s, onClick: a, userDropdownMenus: o }) => {
  const n = !s, {
    state: { flags: i },
    hasFeature: r
  } = v(), c = N(() => r("isGetLauncherVisible"), [i]);
  return /* @__PURE__ */ e.createElement("nav", { className: p("userProfileNavContainer", n && "closed", !c && "right-0") }, o == null ? void 0 : o.map((l, m) => /* @__PURE__ */ e.createElement("div", { key: `menuLink-${m}` }, /* @__PURE__ */ e.createElement(x, { className: "userProfileNavLink", to: l.pathname, onClick: a }, l.icon && /* @__PURE__ */ e.createElement("i", { className: "userProfileNavLinkIcon" }, l.icon), /* @__PURE__ */ e.createElement("span", null, l.label)), l.isWithDivider && /* @__PURE__ */ e.createElement("div", { className: "navigationDivider" }))), /* @__PURE__ */ e.createElement(
    "button",
    {
      className: "userProfileNavLink",
      onClick: () => {
        t(), a();
      }
    },
    /* @__PURE__ */ e.createElement(O, { className: "userProfileNavLinkIcon" }),
    /* @__PURE__ */ e.createElement("span", null, /* @__PURE__ */ e.createElement(d, { i18nKey: "Navigation.links.logout" }, "Logout"))
  ));
};
function Ie({ onLogout: t, returnPath: s, isDisableUsername: a, userDropdownMenus: o }) {
  const {
    state: { location: n }
  } = A(), i = n.pathname, {
    state: { user: r }
  } = L();
  return /* @__PURE__ */ e.createElement(e.Fragment, null, !r && /* @__PURE__ */ e.createElement(z, { currentLocation: i, returnPath: s }), r && /* @__PURE__ */ e.createElement(_, { user: r, userDropdownMenus: o, onLogout: t, isDisableUsername: a }));
}
function _({ userDropdownMenus: t, user: s, onLogout: a, isDisableUsername: o }) {
  const [n, i] = y(!1), r = I(null), {
    state: { userProfile: c, userProfileFetchStatus: l, userFetchStatus: m }
  } = L(), u = M(), f = l === E.FETCHING || m === E.FETCHING;
  b(() => {
    const g = (C) => {
      n && !C.composedPath().includes(r.current) && i(!1);
    };
    return window.addEventListener("click", g), () => {
      window.removeEventListener("click", g);
    };
  }, [n]);
  const P = async () => {
    await u.logout(), a && a();
  }, k = () => {
    i(!n);
  };
  return /* @__PURE__ */ e.createElement("div", { className: "userAccountContainer", ref: r }, /* @__PURE__ */ e.createElement(
    D,
    {
      user: s,
      userProfile: c,
      wallet: null,
      error: null,
      isFetching: f,
      toggleMenu: k,
      isMenuShown: n,
      isDisableUsername: o
    }
  ), /* @__PURE__ */ e.createElement(
    K,
    {
      onClick: () => i(!1),
      onLogout: P,
      showMenu: n,
      userDropdownMenus: t
    }
  ));
}
export {
  Ie as LoginWidget
};
