import { CommonError as p } from "./accelbyte-widgets124.mjs";
import { BackLinkIcon as c } from "./accelbyte-widgets126.mjs";
import { getCurrentLanguage as m } from "./accelbyte-widgets58.mjs";
import { UIEvent as n } from "./accelbyte-widgets138.mjs";
import { Ctx as d } from "./accelbyte-widgets33.mjs";
import { VerificationCodeContext as h } from "./accelbyte-widgets139.mjs";
import { AlertType as r } from "./accelbyte-widgets128.mjs";
import { AccPath as f } from "./accelbyte-widgets34.mjs";
import { UrlUtils as u } from "./accelbyte-widgets90.mjs";
import { DesktopChecker as g } from "@accelbyte/sdk";
import S from "./accelbyte-widgets140.mjs";
import e from "react";
import { Trans as o } from "react-i18next";
import { Link as E } from "react-router-dom";
import { validationOutdatedModalEvent as C } from "./accelbyte-widgets141.mjs";
import "./accelbyte-widgets142.mjs";
import { VerifyCodeForm as V } from "./accelbyte-widgets143.mjs";
class L extends e.Component {
  constructor(a) {
    super(a), this.getInputValidations = () => {
      this.inputValidationManager.fetchValidations(m()).then(() => {
        const { fetchError: t } = this.inputValidationManager.state;
        t && this.setErrorAlert(t);
      });
    }, this.dismissAlert = () => {
      this.setState({ alert: null });
    }, this.setSentSuccessAlert = () => {
      this.setState({
        alert: {
          type: r.success,
          element: /* @__PURE__ */ e.createElement(o, { i18nKey: "ChangeEmailPage.verifyCodeForm.codeSent" }, "A verification code has been sent to your email")
        }
      });
    }, this.setErrorAlert = async (t) => {
      if (await this.inputValidationManager.isInputValidationError(t)) {
        C.openValidationOutdatedModal(), this.setState({ alert: null });
        return;
      }
      this.setState({
        alert: {
          type: r.danger,
          element: /* @__PURE__ */ e.createElement(p, { error: t })
        }
      });
    }, this.notifyFirstVerificationCodeSent = () => {
      const { location: t } = this.appState.state.appHistory;
      u.silentSearchParamsBuilder(t.search).has("codeSent") && (this.setSentSuccessAlert(), this.appState.state.appHistory.replace(t.pathname));
    }, this.notifyVerificationCodeSent = () => {
      this.setSentSuccessAlert();
    }, this.onStepChanged = () => this.setState({ alert: null }), this.updateEmailAndRedirectToVerifyPage = async (t, i) => {
      const { appHistory: s } = this.appState.state;
      this.setState({ isUpdating: !0 });
      try {
        await this.userAppManager.updateEmail(t, i), g.isDesktopApp() ? (await this.appState.refreshUser(), s.replace(s.location.pathname)) : await this.appState.fetchAllUserInformation();
      } catch (l) {
        this.setErrorAlert(l);
      } finally {
        this.setState({ isUpdating: !1 });
      }
    }, this.sendVerificationCode = async () => {
      const t = this.appState.getUser();
      this.verificationManager.sendVerificationCode((t == null ? void 0 : t.emailAddress) || "", h.enum.UpdateEmailAddress).then(() => {
        this.notifyVerificationCodeSent();
      }).catch((i) => {
        this.setErrorAlert(i);
      });
    }, this.state = {
      alert: null,
      step: "verifyCode",
      isUpdating: !1
    }, this.appState = d.getAppState(), this.userAppManager = this.appState.getUserManager(), this.verificationManager = this.appState.verificationManager, this.verificationManager.subscribe(() => this.setState({})), this.inputValidationManager = this.appState.inputValidationAppManager, this.inputValidationManager.subscribe(() => this.setState({}));
  }
  componentDidMount() {
    n.updateBackground(`url(${S})`), this.getInputValidations(), this.notifyFirstVerificationCodeSent();
  }
  componentWillUnmount() {
    n.updateBackground(null);
  }
  render() {
    const a = this.appState.getUser(), { isRequestingVerificationCode: t, isVerifying: i } = this.verificationManager.state;
    return /* @__PURE__ */ e.createElement("div", { className: "changeEmailPage" }, /* @__PURE__ */ e.createElement("div", { className: "changeEmailPageContainer" }, /* @__PURE__ */ e.createElement("main", null, /* @__PURE__ */ e.createElement("div", { className: "navigationContainer" }, /* @__PURE__ */ e.createElement(E, { to: f.Profile.makePath(), className: "btnBack" }, /* @__PURE__ */ e.createElement(c, null), /* @__PURE__ */ e.createElement("p", { className: "backText" }, /* @__PURE__ */ e.createElement(o, { i18nKey: "Navigation.editHistory.backlink" }, "Back to Profile")))), /* @__PURE__ */ e.createElement("div", { className: "changeEmailSubPage" }, /* @__PURE__ */ e.createElement(
      V,
      {
        email: a && a.emailAddress || "",
        isRequestingCode: t,
        isVerifying: i,
        onResendCode: this.sendVerificationCode,
        onUpdateEmail: this.updateEmailAndRedirectToVerifyPage,
        alert: this.state.alert,
        onDismissAlert: this.dismissAlert,
        oldEmail: a && a.emailAddress || "",
        isUpdatingEmail: this.state.isUpdating,
        inputValidationManager: this.inputValidationManager
      }
    )))));
  }
}
export {
  L as default
};
