import { E as ErrorHelper, T as Trans, H as translateServiceError, F as FeUtils, t, J as InputGroup, a as Ctx, V as VerificationCodeContext, K as Env } from "./index.js";
import React__default from "react";
import { H as HelmetExport } from "./Helmet.js";
import "./validator.js";
import "./dateFns.js";
import "./reactPlayer.js";
import "react-router-dom";
import "./reactSlick.js";
class UpgradeAccountPage extends React__default.Component {
  constructor() {
    super(...arguments);
    this.state = {
      code: "",
      password: "",
      emailAddress: "",
      committedEmailAddress: null
    };
  }
  async upgrade() {
    const { upgradeAccount } = this.props;
    const { committedEmailAddress, password, code } = this.state;
    if (committedEmailAddress) {
      await upgradeAccount(committedEmailAddress, password, code);
    }
  }
  renderUpgradeError() {
    const { upgradeError: error } = this.props;
    if (ErrorHelper.isAxiosServerError(error)) {
      return /* @__PURE__ */ React__default.createElement("div", null, /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "UpgradeAccountPage.upgradeError.serverError"
      }, /* @__PURE__ */ React__default.createElement("div", null, "Something went wrong while upgrading your account."), /* @__PURE__ */ React__default.createElement("div", null, "Please request the code again.")));
    }
    if (ErrorHelper.isAxiosNetworkError(error)) {
      return /* @__PURE__ */ React__default.createElement("div", null, /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "UpgradeAccountPage.upgradeError.networkError"
      }, /* @__PURE__ */ React__default.createElement("div", null, "Unable to upgrade your account."), /* @__PURE__ */ React__default.createElement("div", null, "Please check your internet connection and try again.")));
    }
    if (ErrorHelper.isAxiosError(error) && error.response && error.response.status) {
      const errorCode = ErrorHelper.extractServiceErrorCode(error) || 0;
      const errorMessage = translateServiceError(errorCode);
      return errorMessage;
    }
    return /* @__PURE__ */ React__default.createElement("div", null, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "UpgradeAccountPage.upgradeError.unknownError"
    }, /* @__PURE__ */ React__default.createElement("div", null, "Something went wrong while upgrading your account."), /* @__PURE__ */ React__default.createElement("div", null, "Please contact support for assistance.")));
  }
  requestUpgradeForm() {
    const { isUpgrading } = this.props;
    const { committedEmailAddress, password, code } = this.state;
    if (!committedEmailAddress)
      return;
    return /* @__PURE__ */ React__default.createElement("form", {
      onSubmit: (e) => {
        e.preventDefault();
        this.upgrade();
      }
    }, /* @__PURE__ */ React__default.createElement(HelmetExport, null, /* @__PURE__ */ React__default.createElement("title", null, FeUtils.makeTitleWithAppName(t("UpgradeAccountPage.title")))), /* @__PURE__ */ React__default.createElement(InputGroup, {
      type: "text",
      placeholder: t("UpgradeAccountPage.placeholder.emailAddress"),
      disabled: true,
      value: committedEmailAddress
    }), /* @__PURE__ */ React__default.createElement("div", null, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "UpgradeAccountPage.code.instruction"
    }, /* @__PURE__ */ React__default.createElement("div", null, "Fill in the code field using the verification code sent to your email."), /* @__PURE__ */ React__default.createElement("div", null, "Want to send a new code to a different email address?", /* @__PURE__ */ React__default.createElement("a", {
      href: "#",
      onClick: (e) => {
        e.preventDefault();
        this.setState({ committedEmailAddress: null });
      }
    }, "Change your email address here.")))), /* @__PURE__ */ React__default.createElement(InputGroup, {
      type: "text",
      placeholder: t("UpgradeAccountPage.placeholder.code"),
      disabled: isUpgrading,
      value: code,
      onChange: (e) => this.setState({ code: e.target.value })
    }), /* @__PURE__ */ React__default.createElement(InputGroup, {
      type: "password",
      placeholder: t("UpgradeAccountPage.placeholder.password"),
      disabled: isUpgrading,
      value: password,
      onChange: (e) => this.setState({ password: e.target.value })
    }), isUpgrading && /* @__PURE__ */ React__default.createElement("div", null, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "UpgradeAccountPage.upgrading"
    }, "Upgrading your account...")), this.renderUpgradeError(), /* @__PURE__ */ React__default.createElement("div", null, /* @__PURE__ */ React__default.createElement("button", {
      disabled: isUpgrading || !password || !code,
      type: "submit"
    }, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "UpgradeAccountPage.upgradeButton"
    }, "Upgrade"))));
  }
  async requestCode() {
    await this.props.requestCode(this.state.emailAddress);
    if (!this.props.requestCodeError) {
      await this.setState({ committedEmailAddress: this.state.emailAddress });
    }
  }
  renderRequestCodeError() {
    const { requestCodeError: error } = this.props;
    if (!error)
      return;
    if (ErrorHelper.isAxiosServerError(error)) {
      return /* @__PURE__ */ React__default.createElement("div", null, /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "UpgradeAccountPage.requestCodeError.serverError"
      }, /* @__PURE__ */ React__default.createElement("div", null, "Something went wrong."), /* @__PURE__ */ React__default.createElement("div", null, "Please try requesting code again")));
    }
    if (ErrorHelper.isAxiosNetworkError(error)) {
      return /* @__PURE__ */ React__default.createElement("div", null, /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "UpgradeAccountPage.requestCodeError.networkError"
      }, /* @__PURE__ */ React__default.createElement("div", null, "Unable to send the verification code."), /* @__PURE__ */ React__default.createElement("div", null, "Please make sure there are no issues with your internet connection and try again.")));
    }
    if (ErrorHelper.isAxiosError(error) && error.response && error.response.status) {
      const { status } = error.response;
      if (status === 409) {
        return /* @__PURE__ */ React__default.createElement("div", null, /* @__PURE__ */ React__default.createElement(Trans, {
          i18nKey: "UpgradeAccountPage.requestCodeError.conflict"
        }, /* @__PURE__ */ React__default.createElement("div", null, "This email address is already being used by another account."), /* @__PURE__ */ React__default.createElement("div", null, "Please use a different email address.")));
      }
      if (status === 400) {
        return /* @__PURE__ */ React__default.createElement("div", null, /* @__PURE__ */ React__default.createElement(Trans, {
          i18nKey: "UpgradeAccountPage.requestCodeError.invalidRequest"
        }, /* @__PURE__ */ React__default.createElement("div", null, "The email address you provided is invalid."), /* @__PURE__ */ React__default.createElement("div", null, "Please use a valid email address.")));
      }
      if (status === 401) {
        return /* @__PURE__ */ React__default.createElement("div", null, /* @__PURE__ */ React__default.createElement(Trans, {
          i18nKey: "UpgradeAccountPage.requestCodeError.unauthorized"
        }, /* @__PURE__ */ React__default.createElement("div", null, "You are unauthorized to upgrade your account"), /* @__PURE__ */ React__default.createElement("div", null, "Please contact administrator to resolve this issuee")));
      }
      if (status === 403) {
        return /* @__PURE__ */ React__default.createElement("div", null, /* @__PURE__ */ React__default.createElement(Trans, {
          i18nKey: "UpgradeAccountPage.requestCodeError.unauthorized"
        }, /* @__PURE__ */ React__default.createElement("div", null, "You are forbidden to upgrade your account"), /* @__PURE__ */ React__default.createElement("div", null, "Please contact administrator to resolve this issuee")));
      }
    }
    return /* @__PURE__ */ React__default.createElement("div", null, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "UpgradeAccountPage.requestCodeError.unknownError"
    }, /* @__PURE__ */ React__default.createElement("div", null, "Something went wrong while sending the verification code to your email."), /* @__PURE__ */ React__default.createElement("div", null, "Please request the code again.")));
  }
  renderRequestCodeForm() {
    const { isRequestingCode } = this.props;
    return /* @__PURE__ */ React__default.createElement("form", {
      onSubmit: (e) => {
        e.preventDefault();
        this.requestCode();
      }
    }, /* @__PURE__ */ React__default.createElement(InputGroup, {
      type: "text",
      placeholder: t("UpgradeAccountPage.placeholder.emailAddress"),
      disabled: isRequestingCode,
      value: this.state.emailAddress,
      onChange: (e) => this.setState({ emailAddress: e.target.value })
    }), isRequestingCode && /* @__PURE__ */ React__default.createElement("div", null, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "UpgradeAccountPage.requestingCode"
    }, "Sending the verification code to your email...")), this.renderRequestCodeError(), /* @__PURE__ */ React__default.createElement("div", null, /* @__PURE__ */ React__default.createElement("button", {
      disabled: isRequestingCode,
      type: "submit"
    }, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "UpgradeAccountPage.nextButton"
    }, "Next"))));
  }
  renderLoadError() {
    return /* @__PURE__ */ React__default.createElement("div", null, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "UpgradeAccountPage.loadingError"
    }, "Loading user data failed. Please refresh or go back and try again."));
  }
  renderLoadingNotice() {
    return /* @__PURE__ */ React__default.createElement("div", null, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "UpgradeAccountPage.loadingNotice"
    }, "Loading user data..."));
  }
  render() {
    const { isReady, isLoading, loadError } = this.props;
    return /* @__PURE__ */ React__default.createElement("div", {
      className: "upgradeAccountPage"
    }, /* @__PURE__ */ React__default.createElement("h1", null, "Upgrade Account"), isLoading && this.renderLoadingNotice(), loadError && this.renderLoadError(), isReady && !this.state.committedEmailAddress && this.renderRequestCodeForm(), isReady && this.state.committedEmailAddress && this.requestUpgradeForm());
  }
}
class UpgradeAccountPageContainer extends React__default.Component {
  constructor() {
    super(...arguments);
    this.appState = Ctx.getAppState();
    this.state = {
      isReady: false,
      isLoading: false,
      loadError: null
    };
    this.setStatePromise = (state) => new Promise((resolve) => this.setState(state, resolve));
  }
  componentDidMount() {
    this.init();
  }
  async init() {
    try {
      await this.setStatePromise({ isLoading: true });
      await this.appState.fetchAllUserInformation();
      await this.setStatePromise({ isReady: true, loadError: null });
    } catch (error) {
      await this.setStatePromise({ loadError: error });
    } finally {
      await this.setStatePromise({ isLoading: false });
    }
  }
  render() {
    const { isReady, isLoading, loadError } = this.state;
    const { isRequestingCode, requestCodeError, isUpgradingHeadless, upgradeHeadlessError } = this.appState.userAppManager.state;
    const { upgradeHeadlessAccount } = this.appState.linkAccountManager;
    const { sendVerificationCode } = this.appState.verificationManager;
    return /* @__PURE__ */ React__default.createElement(UpgradeAccountPage, {
      isReady,
      isLoading,
      loadError,
      requestCode: (email) => sendVerificationCode(email, VerificationCodeContext.enum.upgradeHeadlessAccount),
      isRequestingCode,
      requestCodeError,
      upgradeAccount: (email, password, code) => upgradeHeadlessAccount({ emailAddress: email, password, code, validateOnly: false }, Env.IS_DISABLE_USERNAME),
      isUpgrading: isUpgradingHeadless,
      upgradeError: upgradeHeadlessError
    });
  }
}
export { UpgradeAccountPageContainer as default };
