import { TWOFA_PAGE as a } from "@accelbyte/sdk-iam";
import { Alert as w } from "./accelbyte-widgets123.mjs";
import { Alerts as F } from "./accelbyte-widgets76.mjs";
import { CommonError as P } from "./accelbyte-widgets124.mjs";
import { AuthAppIcon as L } from "./accelbyte-widgets166.mjs";
import { BackupCodeIcon as S } from "./accelbyte-widgets167.mjs";
import { EmailIcon as v } from "./accelbyte-widgets168.mjs";
import { t as i } from "./accelbyte-widgets58.mjs";
import { headerAlertsQueue as f } from "./accelbyte-widgets155.mjs";
import { UIEvent as _ } from "./accelbyte-widgets138.mjs";
import { Ctx as M } from "./accelbyte-widgets33.mjs";
import { AlertType as m } from "./accelbyte-widgets128.mjs";
import { LoginPath as b } from "./accelbyte-widgets35.mjs";
import { ErrorHelper as y } from "./accelbyte-widgets120.mjs";
import T from "./accelbyte-widgets140.mjs";
import e from "react";
import { Trans as n } from "react-i18next";
import { z as g } from "zod";
import { AppName as p } from "./accelbyte-widgets78.mjs";
import "./accelbyte-widgets169.mjs";
import { CodeInput as D } from "./accelbyte-widgets170.mjs";
import { MethodOption as A } from "./accelbyte-widgets171.mjs";
import { PageContent as h } from "./accelbyte-widgets172.mjs";
import { DesktopChecker as C } from "@accelbyte/sdk";
const E = "mfaPage", N = 6, x = 8, I = 6, d = g.enum(["invalid_code", "invalid_factor", "invalid_mfa_token", "code_expired"]), H = g.object({
  error: g.string(),
  error_description: g.string()
}), R = (c) => {
  var o;
  try {
    if (y.isAxiosError(c))
      switch (H.parse((o = c == null ? void 0 : c.response) == null ? void 0 : o.data).error) {
        case d.Enum.invalid_code:
          return /* @__PURE__ */ e.createElement(n, { i18nKey: "twoFAPage.error.invalidCode" }, "Sorry, the code you entered is invalid. Please try again.");
        case d.Enum.code_expired:
          return /* @__PURE__ */ e.createElement(n, { i18nKey: "twoFAPage.error.codeExpired" }, "Sorry, the code you entered is expired. Please try again.");
        case d.Enum.invalid_factor:
        case d.Enum.invalid_mfa_token:
          return /* @__PURE__ */ e.createElement(n, { i18nKey: "twoFAPage.error.invalidFactor" }, "Failed to complete the request. Please click the back to login button below, and try logging in again.");
      }
    throw c;
  } catch {
    return /* @__PURE__ */ e.createElement(P, { error: c });
  }
};
class ce extends e.Component {
  constructor(o) {
    super(o), this.fetchConfigs = async (t) => {
      try {
        await this.appState.fetchConfigs(t);
      } catch (r) {
        console.error(r);
      } finally {
        this.setState({ isLoadingLogo: !1 });
      }
    }, this.changePage = (t) => {
      this.setState({ page: t }), sessionStorage.setItem(E, t);
    }, this.onTryAnother = () => {
      this.changePage(a.enum.options), this.setState({ alert: null });
    }, this.dismissAlert = () => {
      this.setState({ alert: null });
    }, this.submitCode = async (t) => {
      if (this.state.isLoading)
        return;
      const r = this.appState.state.appHistory, l = new URLSearchParams(r.location.search).get("redirectTo");
      try {
        this.setState({ isLoading: !0 });
        const s = await this.userManager.verify2FA(t);
        s && (l && r.replace(l), this.appState.refreshSession(s.access_token, s.refresh_token), await this.appState.refreshUser(), this.userManager.removeMFAData(), C.isDesktopApp() ? this.fetchConfigs(p.LAUNCHER) : this.fetchConfigs(p.PP), this.userManager.state.isLinkingAccount && (f.push({
          type: m.success,
          element: /* @__PURE__ */ e.createElement(n, { i18nKey: "LinkPlatformAccountPage.notification.accountLinked" }, "Account linked")
        }), this.userManager.setState({ isLinkingAccount: !1 })));
      } catch (s) {
        if (console.error(s), y.isAxiosNetworkError(s)) {
          f.push({
            type: m.danger,
            element: i("CommonError.networkError")
          });
          return;
        }
        const k = R(s);
        this.setState({ alert: { type: m.danger, element: k } });
      } finally {
        this.setState({ isLoading: !1 });
      }
    }, this.requestEmailCode = async (t) => {
      if (!this.state.isLoading)
        try {
          this.setState({ isLoading: !0 }), await this.userManager.request2FAEmailCode({ factor: "email" }), t && this.setState({ alert: { type: m.success, element: i("twoFAPage.email.resend.success") } }), this.changePage(a.enum.email);
        } catch (r) {
          if (y.isAxiosNetworkError(r)) {
            f.push({
              type: m.danger,
              element: i("CommonError.networkError")
            });
            return;
          }
          this.setState({ alert: { type: m.danger, element: /* @__PURE__ */ e.createElement(P, { error: r }) } });
        } finally {
          this.setState({ isLoading: !1 });
        }
    }, this.getCodeLength = (t) => {
      switch (t) {
        case a.enum.backupCode:
          return x;
        case a.enum.authenticator:
          return N;
        default:
          return I;
      }
    }, this.renderHelperTextEmail = () => /* @__PURE__ */ e.createElement("div", { className: "mt-3 text-[13px]" }, /* @__PURE__ */ e.createElement("span", null, i("AccountPasswordSecurity.2fa.emailModal.helperText")), /* @__PURE__ */ e.createElement("span", { className: "ml-1 cursor-pointer text-blue-50", onClick: () => this.requestEmailCode(!0) }, i("AccountPasswordSecurity.2fa.emailModal.resendBtn"))), this.renderCodeInput = (t, r) => {
      var l;
      const u = ((l = this.userManager.state.mfaData) == null ? void 0 : l.factors) || [];
      return /* @__PURE__ */ e.createElement(
        D,
        {
          onTryAnother: u.length > 1 ? this.onTryAnother : void 0,
          codeLength: this.getCodeLength(t),
          factor: t,
          onSubmit: this.submitCode,
          isSubmitting: this.state.isLoading,
          helperText: r
        }
      );
    }, this.resetMFADataAndLogout = () => {
      sessionStorage.removeItem(E), this.userManager.removeMFAData(), this.appState.logout(), C.isDesktopApp() ? this.fetchConfigs(p.LAUNCHER) : this.appState.state.appHistory.push(b.Login.link);
    }, this.state = {
      page: null,
      isLoading: !1,
      isLoadingLogo: !0,
      alert: null
    }, this.appState = M.getAppState(), this.userManager = this.appState.userAppManager;
  }
  componentDidMount() {
    var r;
    this.fetchConfigs(p.LOGIN), _.updateBackground(`url(${T})`);
    const o = (r = this.userManager.state.mfaData) == null ? void 0 : r.defaultFactor, t = sessionStorage.getItem(E);
    this.setState({ page: t || o || a.enum.options });
  }
  render() {
    var s;
    const o = ((s = this.userManager.state.mfaData) == null ? void 0 : s.factors) || [], { alert: t, page: r, isLoadingLogo: u } = this.state, l = this.appState.getCompanyLogo("header");
    return /* @__PURE__ */ e.createElement("div", { className: "twoFAPage" }, /* @__PURE__ */ e.createElement(F, { className: "twoFAAlert" }), /* @__PURE__ */ e.createElement("div", { className: "twoFAContainer" }, /* @__PURE__ */ e.createElement("div", { className: "flex justify-center" }, !u && /* @__PURE__ */ e.createElement("img", { src: l, loading: "lazy" })), /* @__PURE__ */ e.createElement("div", { className: "twoFAForm" }, r === a.enum.authenticator && /* @__PURE__ */ e.createElement(
      h,
      {
        title: i("twoFAPage.authenticator.title"),
        description: /* @__PURE__ */ e.createElement(n, { i18nKey: "twoFAPage.authenticator.description" }, "Please enter the security code from your authenticator app."),
        withAlert: !0,
        alert: t,
        onDismissAlert: this.dismissAlert
      },
      this.renderCodeInput(a.enum.authenticator)
    ), r === a.enum.backupCode && /* @__PURE__ */ e.createElement(
      h,
      {
        title: i("twoFAPage.backupCode.title"),
        description: /* @__PURE__ */ e.createElement(n, { i18nKey: "twoFAPage.backupCode.description" }, "Please enter one of your backup codes to continue."),
        withAlert: !0,
        alert: t,
        onDismissAlert: this.dismissAlert
      },
      this.renderCodeInput(a.enum.backupCode)
    ), r === a.enum.email && /* @__PURE__ */ e.createElement(
      h,
      {
        title: i("twoFAPage.email.title"),
        description: /* @__PURE__ */ e.createElement(n, { i18nKey: "twoFAPage.email.description" }, "To continue, please enter the verification code we’ve sent to your email at", /* @__PURE__ */ e.createElement("strong", null, { email: this.userManager.getEmailUserFromMfaData() }), ".", /* @__PURE__ */ e.createElement("a", { href: "https://docs.accelbyte.io/faq/faq.html", target: "_blank", rel: "noreferrer" }, "Learn more")),
        withAlert: !0,
        alert: this.state.alert,
        onDismissAlert: this.dismissAlert
      },
      this.renderCodeInput(a.enum.email, this.renderHelperTextEmail())
    ), r === a.enum.options && /* @__PURE__ */ e.createElement(h, { title: i("twoFAPage.verify.title"), description: i("twoFAPage.verify.description") }, !!t && /* @__PURE__ */ e.createElement(w, { type: t.type, onDismiss: this.dismissAlert, className: "text-left" }, t.element), o.includes(a.enum.authenticator) && /* @__PURE__ */ e.createElement(
      A,
      {
        icon: /* @__PURE__ */ e.createElement(L, null),
        description: i("twoFAPage.verify.authApp.description"),
        onClick: () => this.changePage(a.enum.authenticator)
      }
    ), o.includes(a.enum.backupCode) && /* @__PURE__ */ e.createElement(
      A,
      {
        icon: /* @__PURE__ */ e.createElement(S, null),
        description: i("twoFAPage.verify.backupCode.description"),
        onClick: () => this.changePage(a.enum.backupCode)
      }
    ), o.includes(a.enum.email) && /* @__PURE__ */ e.createElement(
      A,
      {
        icon: /* @__PURE__ */ e.createElement(v, null),
        description: i("twoFAPage.verify.email.description"),
        onClick: () => this.requestEmailCode(!1)
      }
    ))), /* @__PURE__ */ e.createElement("div", { className: "backContainer" }, /* @__PURE__ */ e.createElement("button", { onClick: this.resetMFADataAndLogout }, /* @__PURE__ */ e.createElement(n, { i18nKey: "twoFAPage.backToLogin" }, "Back to Login")))));
  }
}
export {
  ce as default
};
