import { LoginButtonContainer as h } from "./accelbyte-widgets101.mjs";
import { UserProfileContainer as u } from "./accelbyte-widgets102.mjs";
import { UserProfileNavContainer as f } from "./accelbyte-widgets103.mjs";
import { Ctx as i } from "./accelbyte-widgets33.mjs";
import { ErrorPath as d } from "./accelbyte-widgets69.mjs";
import { GlobalPath as m } from "./accelbyte-widgets104.mjs";
import { IamUserAuthorizationClient as w } from "@accelbyte/sdk-iam";
import t from "react";
import "./accelbyte-widgets105.mjs";
import { isInIframe as P } from "./accelbyte-widgets106.mjs";
class I extends t.Component {
  constructor(r) {
    super(r), this.onLoginClick = (e) => {
      let o;
      e && (o = !d.ErrorRoot.is(e) && e || void 0);
      const n = new w(i.getAppState().getSdk()).createLoginURL(o);
      if (P()) {
        window.parent.location.replace(n);
        return;
      }
      window.location.replace(n);
    }, this.onLogoutClick = async () => {
      const { onLogout: e } = this.props;
      await this.userAppManager.logout(), await this.userAppManager.clear(), i.getAppState().state.appHistory.replace(m.Home.link), e && e();
    }, this.toggleMenuOff = (e) => {
      this.state.dropdownProfileOpen && !e.composedPath().includes(this.dropdownToggleProfileRef.current) && this.setState({ dropdownProfileOpen: !1 });
    }, this.toggleProfileMenu = () => {
      this.setState({ dropdownProfileOpen: !this.state.dropdownProfileOpen });
    }, this.state = {
      dropdownProfileOpen: !1
    }, this.previousUser = null, this.userAppManager = i.getAppState().userAppManager, this.userAppManager.subscribe(() => {
      this.previousUser !== this.userAppManager.state.user && r.onUpdateUser && (r.onUpdateUser(this.userAppManager.state.user), this.previousUser = this.userAppManager.state.user), this.setState({});
    }), this.dropdownToggleProfileRef = t.createRef();
  }
  componentDidMount() {
    this.userAppManager.state.user && this.props.onUpdateUser && this.props.onUpdateUser(this.userAppManager.state.user), window.addEventListener("click", this.toggleMenuOff);
  }
  componentWillUnmount() {
    window.removeEventListener("click", this.toggleMenuOff);
  }
  render() {
    const { isDisableUsername: r } = this.props, { dropdownProfileOpen: e } = this.state, { user: o, isFetchingProfile: n, errorFetchingProfile: s, userProfile: l } = this.userAppManager.state, g = new URL(i.getAppState().sdkOptions.redirectURI).pathname, a = window.location.pathname, c = a.replace(g, ""), p = i.getAppState().walletAppManager;
    return /* @__PURE__ */ t.createElement("div", { className: "loginWidget" }, !o && /* @__PURE__ */ t.createElement(h, { currentLocation: a, onLogin: () => this.onLoginClick(c) }), o && /* @__PURE__ */ t.createElement("div", { className: "userAccountContainer", ref: this.dropdownToggleProfileRef }, /* @__PURE__ */ t.createElement(
      u,
      {
        user: o,
        isFetching: n || p.state.isFetching,
        userProfile: l,
        wallet: p.state.wallet,
        error: s,
        toggleMenu: this.toggleProfileMenu,
        isMenuShown: e,
        isDisableUsername: r
      }
    ), /* @__PURE__ */ t.createElement(
      f,
      {
        onClick: () => this.setState({ dropdownProfileOpen: !1 }),
        onLogout: this.onLogoutClick,
        showMenu: e
      }
    )));
  }
}
export {
  I as LoginWidget
};
