import { InputGroup as s } from "./accelbyte-widgets229.mjs";
import { t as l } from "./accelbyte-widgets58.mjs";
import { ErrorHelper as i } from "./accelbyte-widgets120.mjs";
import { PageTitleUtils as c } from "./accelbyte-widgets188.mjs";
import { translateServiceError as u } from "@accelbyte/error-translator";
import e from "react";
import { Helmet as m } from "react-helmet";
import { Trans as a } from "react-i18next";
import { Ctx as g } from "./accelbyte-widgets33.mjs";
class C extends e.Component {
  constructor() {
    super(...arguments), this.state = {
      code: "",
      password: "",
      emailAddress: "",
      committedEmailAddress: null
    };
  }
  async upgrade() {
    const { upgradeAccount: r } = this.props, { committedEmailAddress: t, password: n, code: d } = this.state;
    t && await r(t, n, d);
  }
  renderUpgradeError() {
    const { upgradeError: r } = this.props;
    if (i.isAxiosServerError(r))
      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(r))
      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(r) && r.response && r.response.status) {
      const t = i.extractServiceErrorCode(r) || 0;
      return u(t);
    }
    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: r } = this.props, { committedEmailAddress: t, password: n, code: d } = this.state;
    if (t)
      return /* @__PURE__ */ e.createElement(
        "form",
        {
          onSubmit: (o) => {
            o.preventDefault(), this.upgrade();
          }
        },
        /* @__PURE__ */ e.createElement(m, null, /* @__PURE__ */ e.createElement("title", null, c.getPageTitle(
          g.getAppState().state.discovery || null,
          l("UpgradeAccountPage.title"),
          l("UpgradeAccountPage.title")
        ))),
        /* @__PURE__ */ e.createElement(s, { type: "text", placeholder: l("UpgradeAccountPage.placeholder.emailAddress"), disabled: !0, value: t }),
        /* @__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(
          s,
          {
            type: "text",
            placeholder: l("UpgradeAccountPage.placeholder.code"),
            disabled: r,
            value: d,
            onChange: (o) => this.setState({ code: o.target.value })
          }
        ),
        /* @__PURE__ */ e.createElement(
          s,
          {
            type: "password",
            placeholder: l("UpgradeAccountPage.placeholder.password"),
            disabled: r,
            value: n,
            onChange: (o) => this.setState({ password: o.target.value })
          }
        ),
        r && /* @__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: r || !n || !d, 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: r } = this.props;
    if (r) {
      if (i.isAxiosServerError(r))
        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(r))
        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(r) && r.response && r.response.status) {
        const { status: t } = r.response;
        if (t === 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 (t === 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 (t === 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 (t === 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: r } = this.props;
    return /* @__PURE__ */ e.createElement(
      "form",
      {
        onSubmit: (t) => {
          t.preventDefault(), this.requestCode();
        }
      },
      /* @__PURE__ */ e.createElement(
        s,
        {
          type: "text",
          placeholder: l("UpgradeAccountPage.placeholder.emailAddress"),
          disabled: r,
          value: this.state.emailAddress,
          onChange: (t) => this.setState({ emailAddress: t.target.value })
        }
      ),
      r && /* @__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: r, 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: r, isLoading: t, loadError: n } = this.props;
    return /* @__PURE__ */ e.createElement("div", { className: "upgradeAccountPage" }, /* @__PURE__ */ e.createElement("h1", null, "Upgrade Account"), t && this.renderLoadingNotice(), n && this.renderLoadError(), r && !this.state.committedEmailAddress && this.renderRequestCodeForm(), r && this.state.committedEmailAddress && this.requestUpgradeForm());
  }
}
export {
  C as UpgradeAccountPage
};
