import { x as getCurrentLanguage, h as AlertType, T as Trans, y as validationOutdatedModalEvent, i as CommonError, U as UrlUtils, D as DesktopChecker, V as VerificationCodeContext, a as Ctx, k as UIEvent, l as bgCommon, g as AccPath, z as VerifyCodeForm } from "./index.js";
import { B as BackLinkIcon } from "./BackLinkIcon.js";
import React__default from "react";
import { Link } from "react-router-dom";
import "./dateFns.js";
import "./validator.js";
import "./reactPlayer.js";
import "./reactSlick.js";
var ChangeEmailPage$1 = "";
class ChangeEmailPage extends React__default.Component {
  constructor(props) {
    super(props);
    this.getInputValidations = () => {
      this.inputValidationManager.fetchValidations(getCurrentLanguage()).then(() => {
        const { fetchError } = this.inputValidationManager.state;
        if (fetchError) {
          this.setErrorAlert(fetchError);
        }
      });
    };
    this.dismissAlert = () => {
      this.setState({ alert: null });
    };
    this.setSentSuccessAlert = () => {
      this.setState({
        alert: {
          type: AlertType.success,
          element: /* @__PURE__ */ React__default.createElement(Trans, {
            i18nKey: "ChangeEmailPage.verifyCodeForm.codeSent"
          }, "A verification code has been sent to your email")
        }
      });
    };
    this.setErrorAlert = async (error) => {
      if (await this.inputValidationManager.isInputValidationError(error)) {
        validationOutdatedModalEvent.openValidationOutdatedModal();
        this.setState({ alert: null });
        return;
      }
      this.setState({
        alert: {
          type: AlertType.danger,
          element: /* @__PURE__ */ React__default.createElement(CommonError, {
            error
          })
        }
      });
    };
    this.notifyFirstVerificationCodeSent = () => {
      const { location } = this.appState.state.appHistory;
      const codeSent = UrlUtils.silentSearchParamsBuilder(location.search).has("codeSent");
      if (codeSent) {
        this.setSentSuccessAlert();
        this.appState.state.appHistory.replace(location.pathname);
      }
    };
    this.notifyVerificationCodeSent = () => {
      this.setSentSuccessAlert();
    };
    this.onStepChanged = () => this.setState({ alert: null });
    this.updateEmailAndRedirectToVerifyPage = async (code, newEmail) => {
      const { appHistory } = this.appState.state;
      this.setState({ isUpdating: true });
      try {
        await this.userAppManager.updateEmail(code, newEmail);
        if (!DesktopChecker.isDesktopApp()) {
          await this.appState.fetchAllUserInformation();
        } else {
          await this.appState.refreshUser();
          appHistory.replace(appHistory.location.pathname);
        }
      } catch (error) {
        this.setErrorAlert(error);
      } finally {
        this.setState({ isUpdating: false });
      }
    };
    this.sendVerificationCode = async () => {
      const user = this.appState.getUser();
      this.verificationManager.sendVerificationCode((user == null ? void 0 : user.emailAddress) || "", VerificationCodeContext.enum.UpdateEmailAddress).then(() => {
        this.notifyVerificationCodeSent();
      }).catch((error) => {
        this.setErrorAlert(error);
      });
    };
    this.state = {
      alert: null,
      step: "verifyCode",
      isUpdating: false
    };
    this.appState = Ctx.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() {
    UIEvent.updateBackground(`url(${bgCommon})`);
    this.getInputValidations();
    this.notifyFirstVerificationCodeSent();
  }
  componentWillUnmount() {
    UIEvent.updateBackground(null);
  }
  render() {
    const user = this.appState.getUser();
    const { isRequestingVerificationCode, isVerifying } = this.verificationManager.state;
    return /* @__PURE__ */ React__default.createElement("div", {
      className: "changeEmailPage"
    }, /* @__PURE__ */ React__default.createElement("div", {
      className: "changeEmailPageContainer"
    }, /* @__PURE__ */ React__default.createElement("main", null, /* @__PURE__ */ React__default.createElement("div", {
      className: "navigationContainer"
    }, /* @__PURE__ */ React__default.createElement(Link, {
      to: AccPath.Profile.makePath(),
      className: "btnBack"
    }, /* @__PURE__ */ React__default.createElement(BackLinkIcon, null), /* @__PURE__ */ React__default.createElement("p", {
      className: "backText"
    }, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "Navigation.editHistory.backlink"
    }, "Back to Profile")))), /* @__PURE__ */ React__default.createElement("div", {
      className: "changeEmailSubPage"
    }, /* @__PURE__ */ React__default.createElement(VerifyCodeForm, {
      email: user && user.emailAddress || "",
      isRequestingCode: isRequestingVerificationCode,
      isVerifying,
      onResendCode: this.sendVerificationCode,
      onUpdateEmail: this.updateEmailAndRedirectToVerifyPage,
      alert: this.state.alert,
      onDismissAlert: this.dismissAlert,
      oldEmail: user && user.emailAddress || "",
      isUpdatingEmail: this.state.isUpdating,
      inputValidationManager: this.inputValidationManager
    })))));
  }
}
export { ChangeEmailPage as default };
