import { y as i, T as a, Z as A, F as y, t as d, $ as l, a as f, X as P, E as U } from "./index-162fd1aa.mjs";
import e from "react";
import { H as C } from "./Helmet-ed6d5573.mjs";
import "./dateFns-2367ee5b.mjs";
import "./validator-61879ab7.mjs";
import "./reactPlayer-2f69f369.mjs";
import "react-router-dom";
import "./reactSlick-cb4a2d39.mjs";
class w extends e.Component {
  constructor() {
    super(...arguments), this.state = {
      code: "",
      password: "",
      emailAddress: "",
      committedEmailAddress: null
    };
  }
  async upgrade() {
    const { upgradeAccount: t } = this.props, { committedEmailAddress: r, password: n, code: s } = this.state;
    r && await t(r, n, s);
  }
  renderUpgradeError() {
    const { upgradeError: t } = this.props;
    if (i.isAxiosServerError(t))
      return /* @__PURE__ */ e.createElement("div", null, /* @__PURE__ */ e.createElement(a, { i18nKey: "UpgradeAccountPage.upgradeError.serverError" }, /* @__PURE__ */ e.createElement("div", null, "Something went wrong while upgrading your account."), /* @__PURE__ */ e.createElement("div", null, "Please request the code again.")));
    if (i.isAxiosNetworkError(t))
      return /* @__PURE__ */ e.createElement("div", null, /* @__PURE__ */ e.createElement(a, { i18nKey: "UpgradeAccountPage.upgradeError.networkError" }, /* @__PURE__ */ e.createElement("div", null, "Unable to upgrade your account."), /* @__PURE__ */ e.createElement("div", null, "Please check your internet connection and try again.")));
    if (i.isAxiosError(t) && t.response && t.response.status) {
      const r = i.extractServiceErrorCode(t) || 0;
      return A(r);
    }
    return /* @__PURE__ */ e.createElement("div", null, /* @__PURE__ */ e.createElement(a, { i18nKey: "UpgradeAccountPage.upgradeError.unknownError" }, /* @__PURE__ */ e.createElement("div", null, "Something went wrong while upgrading your account."), /* @__PURE__ */ e.createElement("div", null, "Please contact support for assistance.")));
  }
  requestUpgradeForm() {
    const { isUpgrading: t } = this.props, { committedEmailAddress: r, password: n, code: s } = this.state;
    if (r)
      return /* @__PURE__ */ e.createElement(
        "form",
        {
          onSubmit: (o) => {
            o.preventDefault(), this.upgrade();
          }
        },
        /* @__PURE__ */ e.createElement(C, null, /* @__PURE__ */ e.createElement("title", null, y.makeTitleWithAppName(d("UpgradeAccountPage.title")))),
        /* @__PURE__ */ e.createElement(l, { type: "text", placeholder: d("UpgradeAccountPage.placeholder.emailAddress"), disabled: !0, value: r }),
        /* @__PURE__ */ e.createElement("div", null, /* @__PURE__ */ e.createElement(a, { i18nKey: "UpgradeAccountPage.code.instruction" }, /* @__PURE__ */ e.createElement("div", null, "Fill in the code field using the verification code sent to your email."), /* @__PURE__ */ e.createElement("div", null, "Want to send a new code to a different email address?", /* @__PURE__ */ e.createElement(
          "a",
          {
            href: "#",
            onClick: (o) => {
              o.preventDefault(), this.setState({ committedEmailAddress: null });
            }
          },
          "Change your email address here."
        )))),
        /* @__PURE__ */ e.createElement(
          l,
          {
            type: "text",
            placeholder: d("UpgradeAccountPage.placeholder.code"),
            disabled: t,
            value: s,
            onChange: (o) => this.setState({ code: o.target.value })
          }
        ),
        /* @__PURE__ */ e.createElement(
          l,
          {
            type: "password",
            placeholder: d("UpgradeAccountPage.placeholder.password"),
            disabled: t,
            value: n,
            onChange: (o) => this.setState({ password: o.target.value })
          }
        ),
        t && /* @__PURE__ */ e.createElement("div", null, /* @__PURE__ */ e.createElement(a, { i18nKey: "UpgradeAccountPage.upgrading" }, "Upgrading your account...")),
        this.renderUpgradeError(),
        /* @__PURE__ */ e.createElement("div", null, /* @__PURE__ */ e.createElement("button", { disabled: t || !n || !s, type: "submit" }, /* @__PURE__ */ e.createElement(a, { i18nKey: "UpgradeAccountPage.upgradeButton" }, "Upgrade")))
      );
  }
  async requestCode() {
    await this.props.requestCode(this.state.emailAddress), this.props.requestCodeError || await this.setState({ committedEmailAddress: this.state.emailAddress });
  }
  renderRequestCodeError() {
    const { requestCodeError: t } = this.props;
    if (t) {
      if (i.isAxiosServerError(t))
        return /* @__PURE__ */ e.createElement("div", null, /* @__PURE__ */ e.createElement(a, { i18nKey: "UpgradeAccountPage.requestCodeError.serverError" }, /* @__PURE__ */ e.createElement("div", null, "Something went wrong."), /* @__PURE__ */ e.createElement("div", null, "Please try requesting code again")));
      if (i.isAxiosNetworkError(t))
        return /* @__PURE__ */ e.createElement("div", null, /* @__PURE__ */ e.createElement(a, { i18nKey: "UpgradeAccountPage.requestCodeError.networkError" }, /* @__PURE__ */ e.createElement("div", null, "Unable to send the verification code."), /* @__PURE__ */ e.createElement("div", null, "Please make sure there are no issues with your internet connection and try again.")));
      if (i.isAxiosError(t) && t.response && t.response.status) {
        const { status: r } = t.response;
        if (r === 409)
          return /* @__PURE__ */ e.createElement("div", null, /* @__PURE__ */ e.createElement(a, { i18nKey: "UpgradeAccountPage.requestCodeError.conflict" }, /* @__PURE__ */ e.createElement("div", null, "This email address is already being used by another account."), /* @__PURE__ */ e.createElement("div", null, "Please use a different email address.")));
        if (r === 400)
          return /* @__PURE__ */ e.createElement("div", null, /* @__PURE__ */ e.createElement(a, { i18nKey: "UpgradeAccountPage.requestCodeError.invalidRequest" }, /* @__PURE__ */ e.createElement("div", null, "The email address you provided is invalid."), /* @__PURE__ */ e.createElement("div", null, "Please use a valid email address.")));
        if (r === 401)
          return /* @__PURE__ */ e.createElement("div", null, /* @__PURE__ */ e.createElement(a, { i18nKey: "UpgradeAccountPage.requestCodeError.unauthorized" }, /* @__PURE__ */ e.createElement("div", null, "You are unauthorized to upgrade your account"), /* @__PURE__ */ e.createElement("div", null, "Please contact administrator to resolve this issuee")));
        if (r === 403)
          return /* @__PURE__ */ e.createElement("div", null, /* @__PURE__ */ e.createElement(a, { i18nKey: "UpgradeAccountPage.requestCodeError.unauthorized" }, /* @__PURE__ */ e.createElement("div", null, "You are forbidden to upgrade your account"), /* @__PURE__ */ e.createElement("div", null, "Please contact administrator to resolve this issuee")));
      }
      return /* @__PURE__ */ e.createElement("div", null, /* @__PURE__ */ e.createElement(a, { i18nKey: "UpgradeAccountPage.requestCodeError.unknownError" }, /* @__PURE__ */ e.createElement("div", null, "Something went wrong while sending the verification code to your email."), /* @__PURE__ */ e.createElement("div", null, "Please request the code again.")));
    }
  }
  renderRequestCodeForm() {
    const { isRequestingCode: t } = this.props;
    return /* @__PURE__ */ e.createElement(
      "form",
      {
        onSubmit: (r) => {
          r.preventDefault(), this.requestCode();
        }
      },
      /* @__PURE__ */ e.createElement(
        l,
        {
          type: "text",
          placeholder: d("UpgradeAccountPage.placeholder.emailAddress"),
          disabled: t,
          value: this.state.emailAddress,
          onChange: (r) => this.setState({ emailAddress: r.target.value })
        }
      ),
      t && /* @__PURE__ */ e.createElement("div", null, /* @__PURE__ */ e.createElement(a, { i18nKey: "UpgradeAccountPage.requestingCode" }, "Sending the verification code to your email...")),
      this.renderRequestCodeError(),
      /* @__PURE__ */ e.createElement("div", null, /* @__PURE__ */ e.createElement("button", { disabled: t, type: "submit" }, /* @__PURE__ */ e.createElement(a, { i18nKey: "UpgradeAccountPage.nextButton" }, "Next")))
    );
  }
  renderLoadError() {
    return /* @__PURE__ */ e.createElement("div", null, /* @__PURE__ */ e.createElement(a, { i18nKey: "UpgradeAccountPage.loadingError" }, "Loading user data failed. Please refresh or go back and try again."));
  }
  renderLoadingNotice() {
    return /* @__PURE__ */ e.createElement("div", null, /* @__PURE__ */ e.createElement(a, { i18nKey: "UpgradeAccountPage.loadingNotice" }, "Loading user data..."));
  }
  render() {
    const { isReady: t, isLoading: r, loadError: n } = this.props;
    return /* @__PURE__ */ e.createElement("div", { className: "upgradeAccountPage" }, /* @__PURE__ */ e.createElement("h1", null, "Upgrade Account"), r && this.renderLoadingNotice(), n && this.renderLoadError(), t && !this.state.committedEmailAddress && this.renderRequestCodeForm(), t && this.state.committedEmailAddress && this.requestUpgradeForm());
  }
}
class N extends e.Component {
  constructor() {
    super(...arguments), this.appState = f.getAppState(), this.state = {
      isReady: !1,
      isLoading: !1,
      loadError: null
    }, this.setStatePromise = (t) => new Promise((r) => this.setState(t, r));
  }
  componentDidMount() {
    this.init();
  }
  async init() {
    try {
      await this.setStatePromise({ isLoading: !0 }), await this.appState.fetchAllUserInformation(), await this.setStatePromise({ isReady: !0, loadError: null });
    } catch (t) {
      await this.setStatePromise({ loadError: t });
    } finally {
      await this.setStatePromise({ isLoading: !1 });
    }
  }
  render() {
    const { isReady: t, isLoading: r, loadError: n } = this.state, { isRequestingCode: s, requestCodeError: o, isUpgradingHeadless: m, upgradeHeadlessError: g } = this.appState.userAppManager.state, { upgradeHeadlessAccount: p } = this.appState.linkAccountManager, { sendVerificationCode: E } = this.appState.verificationManager;
    return /* @__PURE__ */ e.createElement(
      w,
      {
        isReady: t,
        isLoading: r,
        loadError: n,
        requestCode: (c) => E(c, P.enum.upgradeHeadlessAccount),
        isRequestingCode: s,
        requestCodeError: o,
        upgradeAccount: (c, h, v) => p({ emailAddress: c, password: h, code: v, validateOnly: !1 }, U.IS_DISABLE_USERNAME),
        isUpgrading: m,
        upgradeError: g
      }
    );
  }
}
export {
  N as default
};
