import { CommonError as d } from "./accelbyte-widgets124.mjs";
import { getCurrentLanguage as h } from "./accelbyte-widgets58.mjs";
import { Ctx as E } from "./accelbyte-widgets33.mjs";
import { VerificationCodeContext as m } from "./accelbyte-widgets139.mjs";
import { AlertType as p } from "./accelbyte-widgets128.mjs";
import { AccPath as f } from "./accelbyte-widgets34.mjs";
import { LoginPath as S } from "./accelbyte-widgets35.mjs";
import { EligibleUser as V } from "@accelbyte/sdk-iam";
import r from "react";
import { Trans as y } from "react-i18next";
import { VerifyCodeForm as C } from "./accelbyte-widgets143.mjs";
import { validationOutdatedModalEvent as A } from "./accelbyte-widgets141.mjs";
import "./accelbyte-widgets351.mjs";
import { UpdatingEmailVerificationForm as v } from "./accelbyte-widgets352.mjs";
class D extends r.Component {
  constructor(o) {
    super(o), this.getInputValidations = () => {
      this.inputValidationManager.fetchValidations(h()).then(() => {
        const { fetchError: t } = this.inputValidationManager.state;
        t && this.setErrorAlert(t);
      });
    }, this.onVerificationCodeSent = () => {
      this.setState({
        alert: {
          type: p.success,
          element: /* @__PURE__ */ r.createElement(y, { i18nKey: "ChangeEmailPage.verifyCodeForm.codeSent" }, "A verification code has been sent to your email")
        }
      });
    }, this.setErrorAlert = async (t) => {
      if (await this.inputValidationManager.isInputValidationError(t)) {
        A.openValidationOutdatedModal(), this.setState({ alert: null });
        return;
      }
      this.setState({
        alert: {
          type: p.danger,
          element: /* @__PURE__ */ r.createElement(d, { error: t })
        }
      });
    }, this.alertSuccessAndRedirectToProfile = async () => {
      await this.setState({ isVerificationSuccess: !0 }), await this.appState.fetchAllUserInformation(), this.appState.state.appHistory.push(f.Profile.link, { updateEmailStatus: "success" });
    }, this.verifyCode = async (t) => {
      try {
        await this.verificationManager.verify(t, h());
        const e = this.appState;
        this.setState({ isVerificationSuccess: !0 }), await e.fetchAllUserInformation(), e.state.appHistory.push(f.Profile.link, { updateEmailStatus: "success" });
      } catch (e) {
        this.setState({
          alert: {
            type: p.danger,
            element: /* @__PURE__ */ r.createElement(d, { error: e })
          },
          isVerificationSuccess: !1
        }), console.error(e);
      }
    }, this.logout = () => {
      this.appState.state.appHistory.push(S.Logout.link);
    }, this.onVerificationCodeChange = (t) => {
      this.setState({
        verificationCode: t.target.value.toString().toUpperCase()
      });
    }, this.dismissAlert = () => {
      this.setState({ alert: null });
    }, this.goToChangeEmail = async () => {
      const t = this.appState.state.appHistory, { setIsLogoutAvailable: e } = this.props, { location: a } = t, i = this.appState.getUser(), n = (i == null ? void 0 : i.oldEmailAddress) || (i == null ? void 0 : i.emailAddress) || "";
      try {
        await this.verificationManager.sendVerificationCode(n, m.enum.UpdateEmailAddress), this.onVerificationCodeSent(), this.setState({ step: "inputNewEmail" }), this.dismissAlert(), t.push(`${a.pathname}?isChangingEmail=true`), e(!1);
      } catch {
        const { requestVerificationCodeError: l } = this.verificationManager.state;
        this.setErrorAlert(l);
      }
    }, this.backToVerifyCode = () => {
      const t = this.appState.state.appHistory, { setIsLogoutAvailable: e } = this.props, { location: a } = t;
      this.setState({ step: "verifyCodeNewEmail" }), this.dismissAlert(), t.push(a.pathname), e(!0);
    }, this.updateEmailAndRedirectToVerifyPage = async (t, e) => {
      const a = this.appState.state.appHistory, { pathname: i } = a.location;
      try {
        await this.userAppManager.updateEmail(t, e), a.push(i), location.reload();
      } catch (n) {
        this.setErrorAlert(n);
      }
    }, this.sendVerificationCode = async (t) => {
      this.verificationManager.sendVerificationCode(t, m.enum.UpdateEmailAddress).then(() => {
        this.onVerificationCodeSent();
      }).catch((e) => {
        this.setErrorAlert(e);
      });
    }, this.appState = E.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({})), this.state = {
      step: "verifyCodeNewEmail",
      verificationCode: "",
      alert: null,
      isVerificationSuccess: !1
    };
  }
  componentDidMount() {
    const o = this.appState, { location: t } = o.state.appHistory, e = new URLSearchParams(t.search);
    this.getInputValidations(), e.get("isChangingEmail") && this.setState({ step: "inputNewEmail" });
  }
  render() {
    const { step: o, isVerificationSuccess: t } = this.state, { isRequestingVerificationCode: e, isVerifying: a } = this.verificationManager.state, { isUpdating: i } = this.userAppManager.state, n = this.appState.getUser(), s = V.safeParse(n);
    if (!s.success)
      return null;
    const l = s.data.newEmailAddress || s.data.emailAddress, u = s.data.emailVerified, c = s.data.oldEmailAddress || s.data.emailAddress;
    return /* @__PURE__ */ r.createElement("div", { className: "newEmailVerification" }, o === "verifyCodeNewEmail" && /* @__PURE__ */ r.createElement(
      v,
      {
        isRequestingCode: e,
        isVerifying: a,
        onResend: () => this.sendVerificationCode(l),
        onVerify: async (g) => {
          await this.verifyCode(g);
        },
        onChangeEmail: this.goToChangeEmail,
        alert: this.state.alert,
        onDismissAlert: this.dismissAlert,
        isSuccess: t
      }
    ), o === "inputNewEmail" && /* @__PURE__ */ r.createElement(
      C,
      {
        email: c,
        isRequestingCode: e,
        isVerifying: a,
        onResendCode: () => this.sendVerificationCode(c),
        onUpdateEmail: this.updateEmailAndRedirectToVerifyPage,
        alert: this.state.alert,
        onDismissAlert: this.dismissAlert,
        oldEmail: c,
        isUpdatingEmail: i,
        isFromEmailVerification: !0,
        onCancel: this.backToVerifyCode,
        emailVerified: u,
        newEmail: l,
        inputValidationManager: this.inputValidationManager
      }
    ));
  }
}
export {
  D as UpdatingEmailVerification
};
