"use strict";const y=require("./LoginIcon-ac34cdc4.js"),e=require("react"),N=require("react-i18next"),u=require("./FetchConfigsUtils-c73b687c.js"),m=require("./Button-44f4db23.js"),L=require("@accelbyte/sdk-iam"),h=require("./CommonError-c282a0a2.js"),d=require("./ArrowIcon-2acb52ce.js"),q=require("./Loading-d3cd7ee0.js"),S=require("./ProfileImage-5c51b03f.js"),g=require("classnames"),w=require("./LogoutIcon-bcb108a7.js"),U=require("./useDiscoveryConfigs-367fb38f.js"),b=require("./index-99b8957b.js"),f=require("./useUserInfo-dc3105ae.js"),F=require("./useAppState-f8f05e41.js");const T=({currentLocation:t,returnPath:i})=>{const{state:a}=u.useRoutes(),[o]=u.useSdk(),r=!t.includes(a.error.index.link)&&t||null,s=()=>{const c=new L.IamUserAuthorizationClient(o).createLoginURL(i||r);window.location.replace(c)};return e.createElement("div",{className:"loginButtonContainer"},e.createElement(m.Button,{className:"loginButton",buttonType:m.ButtonType.TEXT,buttonStyle:m.ButtonStyle.WHITE,onClick:s},e.createElement(y.LoginIcon,null),e.createElement(N.Trans,{i18nKey:"Navigation.links.login"},"Login")))};const A=({user:t,userProfile:i,isFetching:a,wallet:o,error:n,toggleMenu:r,isMenuShown:s,isDisableUsername:c})=>{const l=()=>L.EligibleUser.safeParse(t).success?c?t.displayName||t.userName:t.userName||t.displayName:null;return e.createElement("div",{className:g("userProfileDropdown",s&&"open"),onClick:()=>r()},!a&&n&&e.createElement(h.CommonError,{error:n}),!a&&!n&&i?e.createElement(e.Fragment,null,e.createElement(S.ProfileImage,{imageLink:i&&i.avatarUrl||"",className:"userProfileImage"}),e.createElement("div",{className:"userProfileContainer"},e.createElement("p",{className:"userProfileDisplayName"},l()),o&&e.createElement("p",{className:"userProfileWallet"},o.currencySymbol," ",o.balance)),e.createElement(d.ArrowIcon,{className:g(d.ArrowIconStyles.down,"arrowSize")})):e.createElement(q.Loading,null))};const B=({onLogout:t,showMenu:i,onClick:a,userDropdownMenus:o})=>{const n=!i,{state:{flags:r}}=U.useDiscoveryConfigs(),s=r==null?void 0:r.isGetLauncherVisible;return e.createElement("nav",{className:g("userProfileNavContainer",n&&"closed",!s&&"right-0")},o==null?void 0:o.map((c,l)=>e.createElement("div",{key:`menuLink-${l}`},e.createElement(b.NavLink,{className:"userProfileNavLink",to:c.pathname,onClick:a},c.icon&&e.createElement("i",{className:"userProfileNavLinkIcon"},c.icon),e.createElement("span",null,c.label)),c.isWithDivider&&e.createElement("div",{className:"navigationDivider"}))),e.createElement("button",{className:"userProfileNavLink",onClick:()=>{t(),a()}},e.createElement(w.LogoutIcon,{className:"userProfileNavLinkIcon"}),e.createElement("span",null,e.createElement(N.Trans,{i18nKey:"Navigation.links.logout"},"Logout"))))};function R({onLogout:t,returnPath:i,isDisableUsername:a,userDropdownMenus:o}){const{state:{location:n}}=f.useHistory(),r=n.pathname,{state:{user:s}}=f.useUserInfo();return e.createElement(e.Fragment,null,!s&&e.createElement(T,{currentLocation:r,returnPath:i}),s&&e.createElement(W,{user:s,userDropdownMenus:o,onLogout:t,isDisableUsername:a}))}function W({userDropdownMenus:t,user:i,onLogout:a,isDisableUsername:o}){const[n,r]=e.useState(!1),s=e.useRef(null),{state:{userProfile:c,userProfileFetchStatus:l,userFetchStatus:v}}=f.useUserInfo(),P=F.useAppState(),p=l===u.FetchStatus.FETCHING||v===u.FetchStatus.FETCHING;e.useEffect(()=>{const E=k=>{n&&!k.composedPath().includes(s.current)&&r(!1)};return window.addEventListener("click",E),()=>{window.removeEventListener("click",E)}},[n]);const I=async()=>{await P.logout(),a&&a()},C=()=>{r(!n)};return e.createElement("div",{className:"userAccountContainer",ref:s},e.createElement(A,{user:i,userProfile:c,wallet:null,error:null,isFetching:p,toggleMenu:C,isMenuShown:n,isDisableUsername:o}),e.createElement(B,{onClick:()=>r(!1),onLogout:I,showMenu:n,userDropdownMenus:t}))}exports.LoginWidget=R;
