import { Alert as f } from "./accelbyte-widgets123.mjs";
import { CommonError as p } from "./accelbyte-widgets124.mjs";
import { getCurrentLanguage as h, t } from "./accelbyte-widgets58.mjs";
import { UIEvent as g } from "./accelbyte-widgets138.mjs";
import { Ctx as y } from "./accelbyte-widgets33.mjs";
import { AlertType as v } from "./accelbyte-widgets128.mjs";
import { ErrorPath as E } from "./accelbyte-widgets69.mjs";
import { GlobalPath as _ } from "./accelbyte-widgets104.mjs";
import { LoginPath as k } from "./accelbyte-widgets35.mjs";
import U from "./accelbyte-widgets140.mjs";
import e from "react";
import { Trans as P } from "react-i18next";
import { z as u } from "zod";
import { VerifyUserMessage as a } from "./accelbyte-widgets180.mjs";
import "./accelbyte-widgets181.mjs";
import { Iam as S } from "@accelbyte/sdk-iam";
import { AppName as L } from "./accelbyte-widgets78.mjs";
const l = u.enum([
  "failed_to_verify_invalid_link",
  "failed_to_verify_server_error",
  "failed_to_verify_user_not_found",
  "failed_to_verify_expired_link"
]), m = u.enum(["message", "checkEmail"]);
class O extends e.Component {
  constructor(n) {
    super(n), this.fetchConfigs = async (r) => {
      try {
        await this.appState.fetchConfigs(r);
      } catch (s) {
        console.error(s);
      } finally {
        this.setState({ isLoadingLogo: !1 });
      }
    }, this.redirectToLogin = () => {
      const { appHistory: r } = this.appState.state;
      r.push(k.Login.link);
    }, this.redirectToHome = () => {
      const { appHistory: r } = this.appState.state;
      r.push(_.Home.link);
    }, this.logoutAndRedirectToLogin = async () => {
      await this.appState.logout(), this.redirectToLogin();
    }, this.resendVerificationLink = async () => {
      try {
        if (this.state.isResending)
          return;
        this.setState({ isResending: !0 }), await S.UsersApi(this.appState.sdk).createUserMeVerifyLinkRequest({ languageTag: h() }), this.setState({ page: m.enum.checkEmail, alert: null });
      } catch (r) {
        console.error(r), this.setState({
          alert: {
            type: v.danger,
            element: /* @__PURE__ */ e.createElement(p, { error: r, defaultMessage: t("verifyUserPage.error.alert.resend.failed") })
          }
        });
      } finally {
        this.setState({ isResending: !1 });
      }
    }, this.dismissAlert = () => {
      this.setState({ alert: null });
    }, this.renderAlert = (r) => {
      const { alert: s } = this.state;
      return s ? /* @__PURE__ */ e.createElement(f, { type: s.type, onDismiss: this.dismissAlert, className: "mb-4" }, s.element) : null;
    }, this.renderSuccessMessage = () => /* @__PURE__ */ e.createElement(
      a,
      {
        isSuccess: !0,
        title: t("verifyUserPage.success.title"),
        description: t("verifyUserPage.success.desc"),
        buttonText: t("navigation.links.login"),
        onClickButton: this.logoutAndRedirectToLogin
      }
    ), this.renderErrorMessage = (r, s) => {
      const i = this.appState.getUser(), d = !!(i != null && i.userId), o = i == null ? void 0 : i.emailAddress, { isResending: c } = this.state;
      switch (!0) {
        case r === l.enum.failed_to_verify_user_not_found:
          return /* @__PURE__ */ e.createElement(
            a,
            {
              isSuccess: !1,
              title: t("verifyUserPage.error.failed.userNotFound.title"),
              description: t("verifyUserPage.error.failed.userNotFound.desc"),
              buttonText: t("ErrorPage401.buttonAction"),
              onClickButton: this.redirectToHome
            }
          );
        case r === l.enum.failed_to_verify_server_error:
          this.appState.state.appHistory.push(E.Error500.makePath());
          return;
        case !d:
          return /* @__PURE__ */ e.createElement(
            a,
            {
              isSuccess: !1,
              title: t("verifyUserPage.error.failed.title"),
              description: t("verifyUserPage.error.failed.notLogIn.desc"),
              buttonText: t("navigation.links.login"),
              onClickButton: this.redirectToLogin
            }
          );
        case (!!s && o !== s):
          return /* @__PURE__ */ e.createElement(
            a,
            {
              isSuccess: !1,
              title: t("verifyUserPage.error.failed.title"),
              description: t("verifyUserPage.error.failed.wrongAccount.desc"),
              buttonText: t("navigation.links.logout"),
              onClickButton: this.logoutAndRedirectToLogin
            }
          );
        case r === l.enum.failed_to_verify_expired_link:
          return /* @__PURE__ */ e.createElement(
            a,
            {
              isSuccess: !1,
              title: t("verifyUserPage.error.expired.title"),
              description: t("verifyUserPage.error.expired.desc"),
              buttonText: t("verifyUserPage.error.button.resend"),
              onClickButton: this.resendVerificationLink,
              isLoading: c
            }
          );
        case r === l.enum.failed_to_verify_invalid_link:
        default:
          return /* @__PURE__ */ e.createElement(
            a,
            {
              isSuccess: !1,
              title: t("verifyUserPage.error.invalid.title"),
              description: t("verifyUserPage.error.invalid.desc"),
              buttonText: t("verifyUserPage.error.button.resend"),
              onClickButton: this.resendVerificationLink,
              isLoading: c
            }
          );
      }
    }, this.renderCheckEmail = () => {
      const r = new URLSearchParams(location.search).get("email");
      return /* @__PURE__ */ e.createElement("div", { className: "text-center" }, /* @__PURE__ */ e.createElement("p", { className: "messageTitle mb-4" }, t("verifyUserPage.checkEmail.title")), /* @__PURE__ */ e.createElement("p", { className: "messageDesc" }, /* @__PURE__ */ e.createElement(P, { i18nKey: "verifyUserPage.checkEmail.desc" }, "We have sent a verification email to", /* @__PURE__ */ e.createElement("span", { className: "email" }, { email: r }))), /* @__PURE__ */ e.createElement("hr", { className: "my-5 text-[color:rgb(var(--od-rgb-text),0.65)]" }), /* @__PURE__ */ e.createElement("p", { className: "messageDesc" }, t("verifyUserPage.checkEmail.question")), /* @__PURE__ */ e.createElement("a", { onClick: this.resendVerificationLink, className: "cursor-pointer text-sm" }, t("verifyUserPage.checkEmail.resend")));
    }, this.appState = y.getAppState(), this.state = {
      page: m.enum.message,
      alert: null,
      isResending: !1,
      isLoadingLogo: !0
    };
  }
  componentDidMount() {
    this.fetchConfigs(L.LOGIN), g.updateBackground(`url(${U})`);
  }
  componentWillUnmount() {
    g.updateBackground(null);
  }
  render() {
    const { page: n, isLoadingLogo: r } = this.state, s = new URLSearchParams(location.search), i = s.get("success") === "true", d = s.get("email") || "", o = s.get("error_message") || "", c = this.appState.getCompanyLogo("header");
    return /* @__PURE__ */ e.createElement("div", { className: "verifyUserPage" }, /* @__PURE__ */ e.createElement("div", { className: "verifyUserContainer" }, /* @__PURE__ */ e.createElement("div", { className: "flex justify-center" }, !r && /* @__PURE__ */ e.createElement("img", { src: c, loading: "lazy" })), /* @__PURE__ */ e.createElement("div", { className: "verifyUserCard" }, this.renderAlert(o), n === m.enum.message && /* @__PURE__ */ e.createElement(e.Fragment, null, i && this.renderSuccessMessage(), !i && this.renderErrorMessage(o, d)), n === m.enum.checkEmail && this.renderCheckEmail())));
  }
}
export {
  O as default
};
