import { IamUserAuthorizationClient as S, IamHelper as E } from "@accelbyte/sdk-iam";
import { Button as g, ButtonFontSize as m, ButtonStyle as h, ButtonType as u } from "./accelbyte-widgets107.mjs";
import { CommonError as P } from "./accelbyte-widgets204.mjs";
import { HeaderLogo as A, LogoType as R } from "./accelbyte-widgets362.mjs";
import { Loading as y } from "./accelbyte-widgets136.mjs";
import { t as c } from "./accelbyte-widgets58.mjs";
import { IpcChannels as f } from "./accelbyte-widgets148.mjs";
import { Ctx as _ } from "./accelbyte-widgets33.mjs";
import { Env as l } from "./accelbyte-widgets108.mjs";
import { AccPath as L } from "./accelbyte-widgets34.mjs";
import { GlobalPath as d } from "./accelbyte-widgets104.mjs";
import { GameHelper as x } from "./accelbyte-widgets205.mjs";
import { UrlUtils as C } from "./accelbyte-widgets90.mjs";
import t from "react";
import { Redirect as U } from "react-router-dom";
import "./accelbyte-widgets363.mjs";
class Y extends t.Component {
  constructor(a) {
    super(a), this.onPasteRedirectionUrl = (e) => {
      if (e.clipboardData) {
        const n = e.clipboardData.getData("Text"), r = C.silentUrlBuilder(n);
        if (r && r.protocol === `${String(l.REDIRECT_URL).split(":")[0]}:`) {
          const o = new URLSearchParams(new URL(r).search), i = o.get("code"), s = o.get("state"), p = o.get("error");
          this.exchangeAuthorizationCode(i, s, p);
        }
      }
    }, this.exchangeAuthorizationCode = async (e, n, r) => {
      if (!this.state.isExchanging) {
        if (this.setState({ isExchanging: !0 }), this.clearParams(), r) {
          this.appState.onError(new Error(r)), console.error("exchangeAuthorizationCode errorParams:", r);
          return;
        }
        try {
          const i = await new S(this.appState.getSdk()).exchangeAuthorizationCode({
            code: e,
            state: n
          });
          if (!i)
            return;
          const { mfaData: s, response: p } = i;
          if (s) {
            await this.appState.userAppManager.updateMFAData(s), this.appState.state.appHistory.replace(L.TwoFA.link);
            return;
          }
          p && this.appState.refreshSession(p.data.access_token, p.data.refresh_token || void 0);
        } catch {
          this.setState({ error: !0, isExchanging: !1 });
          return;
        }
        try {
          await this.appState.fetchAllUserInformation(), await x.loadGameInfo(), this.appState.state.appHistory.replace(d.Game.link);
        } catch (o) {
          console.error(o), this.setState({ error: o });
        } finally {
          this.setState({ isExchanging: !1 });
        }
      }
    }, this.pageConfig = (e) => this.appState.getPageConfig("loginPage", e), this.renderError = () => {
      const { error: e } = this.state;
      return this.appState.userAppManager.isSessionValid() ? /* @__PURE__ */ t.createElement(U, { to: d.Home.link }) : /* @__PURE__ */ t.createElement("div", { className: "login-progress" }, /* @__PURE__ */ t.createElement("div", { className: "login-progress__info" }, /* @__PURE__ */ t.createElement(P, { error: e })), /* @__PURE__ */ t.createElement(
        g,
        {
          buttonFontSize: m.SMALL,
          buttonStyle: h.PRIMARY,
          buttonType: u.FLAT,
          className: "login-progress__button",
          onClick: this.props.onRetry
        },
        c("loginPage.loginInProgress.tryAgain")
      ));
    }, this.state = {
      error: null,
      isExchanging: !1
    }, this.appState = _.getAppState();
  }
  componentDidMount() {
    (l.IS_DEV_MODE || l.IS_DEBUG_PROD) && window.addEventListener("paste", this.onPasteRedirectionUrl), Device.receive(f.OPEN_URL, (a) => {
      const e = new URL(a), n = e.search, r = e.pathname.replaceAll("/", "");
      if (r) {
        this.props.onRetry(), this.appState.state.appHistory.replace(`/${r}${n}`);
        return;
      }
      const { code: o, error: i, state: s } = E.getAuthorizationCodeParams(n);
      this.exchangeAuthorizationCode(o, s, i);
    });
  }
  componentWillUnmount() {
    Device.remove(f.OPEN_URL);
  }
  clearParams() {
    const { appHistory: a } = this.appState.state;
    a.replace(`${a.location.pathname}`);
  }
  render() {
    const { error: a, isExchanging: e } = this.state;
    return e ? /* @__PURE__ */ t.createElement(y, { loadingType: "SPINNER" }) : a ? this.renderError() : /* @__PURE__ */ t.createElement("div", { className: "login-progress" }, /* @__PURE__ */ t.createElement(A, { className: "login-progress__header", logoType: R.TEXT, logo: this.pageConfig("boxLogo") }), /* @__PURE__ */ t.createElement("div", { className: "login-progress__title" }, c("loginPage.loginInProgress.title")), /* @__PURE__ */ t.createElement("div", { className: "login-progress__info" }, c("loginPage.loginInProgress.info")), /* @__PURE__ */ t.createElement(
      g,
      {
        buttonFontSize: m.SMALL,
        buttonStyle: h.PRIMARY,
        buttonType: u.FLAT,
        className: "login-progress__button",
        onClick: this.props.onRetry
      },
      c("loginPage.loginInProgress.tryAgain")
    ));
  }
}
export {
  Y as LoginInProgress
};
