import { Alert as y } from "./accelbyte-widgets123.mjs";
import { Button as C } from "./accelbyte-widgets107.mjs";
import { CommonError as M } from "./accelbyte-widgets124.mjs";
import { LanguageSelector as P } from "./accelbyte-widgets162.mjs";
import { Loading as v, LoadingSize as V } from "./accelbyte-widgets136.mjs";
import { getCurrentLanguage as U } from "./accelbyte-widgets58.mjs";
import { UIEvent as f } from "./accelbyte-widgets138.mjs";
import { Ctx as b } from "./accelbyte-widgets33.mjs";
import { Env as E } from "./accelbyte-widgets108.mjs";
import { VerificationCodeContext as k } from "./accelbyte-widgets139.mjs";
import { AlertType as S } from "./accelbyte-widgets128.mjs";
import { LoginPath as H } from "./accelbyte-widgets35.mjs";
import { ErrorHelper as I } from "./accelbyte-widgets120.mjs";
import N from "./accelbyte-widgets140.mjs";
import { LegalHelper as w } from "@accelbyte/sdk-legal";
import e from "react";
import { Trans as n } from "react-i18next";
import { validationOutdatedModalEvent as R, InputValidationOutdatedModal as L } from "./accelbyte-widgets141.mjs";
import { HeaderLogoContainer as D } from "./accelbyte-widgets53.mjs";
import { UpgradeHeadlessAccountForm as T } from "./accelbyte-widgets163.mjs";
import s from "./accelbyte-widgets164.mjs";
import { EmailVerificationForm as _ } from "./accelbyte-widgets165.mjs";
const g = "registration_email";
class ne extends e.Component {
  constructor(l) {
    super(l), this.fetchValidations = async () => {
      this.inputValidationManager.fetchValidations(U()).catch((t) => {
        this.setErrorAlert(t);
      });
    }, this.requestVerificationCode = async (t = this.state.email) => {
      try {
        if (!t)
          return;
        await this.verificationManager.sendVerificationCode(t, k.enum.upgradeHeadlessAccount), this.setState({ email: t }, this.redirectToOtherData);
      } catch {
        const { requestVerificationCodeError: i } = this.verificationManager.state;
        i && this.setErrorAlert(i);
      }
    }, this.setErrorAlert = async (t) => {
      if (await this.inputValidationManager.isInputValidationError(t)) {
        R.openValidationOutdatedModal(), this.setState({ alert: null });
        return;
      }
      const a = I.extractServiceErrorCode(t);
      this.setState({
        alert: {
          type: S.danger,
          element: (() => {
            switch (a) {
              case 10130:
                return /* @__PURE__ */ e.createElement(n, { i18nKey: "UpdateUser.error.updateErrorUnderage" }, "You are below the required age");
              default:
                return /* @__PURE__ */ e.createElement(M, { error: t });
            }
          })()
        }
      });
    }, this.upgradeHeadlessError = (t) => {
      this.setErrorAlert(t), window.scrollTo({ top: 0, behavior: "smooth" });
    }, this.dismissAlert = () => {
      this.setState({ alert: null });
    }, this.redirectToOtherData = () => {
      sessionStorage.setItem(g, this.state.email), this.setState({
        step: "otherData",
        alert: {
          type: S.success,
          element: /* @__PURE__ */ e.createElement(n, { i18nKey: "ChangeEmailPage.verifyCodeForm.codeSent" }, "A verification code has been sent to your email")
        }
      });
    }, this.onReload = () => {
      this.setState({ alert: null }), this.fetchValidations();
    }, this.onChangePage = () => {
      this.setState({ step: "email", email: "" }), this.resetSessionStorage();
    }, this.onContinue = async ({
      password: t,
      displayName: a,
      username: i,
      reachMinimumAge: r,
      verificationCode: d,
      acceptedPolicyIds: m
    }) => {
      const { email: p } = this.state, c = this.appState.getUser(), h = c && c.country ? c.country : "";
      try {
        await this.linkAccountsManager.upgradeHeadlessAccount(
          {
            displayName: a,
            password: t,
            username: i,
            country: h,
            reachMinimumAge: r,
            emailAddress: p,
            code: d
          },
          E.IS_DISABLE_USERNAME
        ), await this.bulkAcceptPolicies(m), this.resetSessionStorage();
        const o = await this.appState.userAppManager.refreshToken();
        o && this.appState.refreshSession(o.access_token, o.refresh_token), await this.appState.refreshUser();
      } catch (o) {
        this.setErrorAlert(o);
      }
    }, this.getEligibilities = (t) => {
      const a = [], { displayedPolicies: i } = this.state;
      for (const r of i)
        a.push({
          policyId: r.policyId,
          policyVersionId: r.policyVersionId,
          localizedPolicyVersionId: r.localizedPolicyVersionId,
          isAccepted: t.includes(r.policyId)
        });
      return a;
    }, this.bulkAcceptPolicies = async (t) => {
      const a = this.appState.getUser(), i = this.getEligibilities(t);
      i && a && await this.legalManager.bulkAcceptPolicies(i);
    }, this.state = {
      email: "",
      alert: null,
      displayedPolicies: [],
      step: "email"
    }, this.appState = b.getAppState(), this.verificationManager = this.appState.verificationManager, this.verificationManager.subscribe(() => this.setState({})), this.linkAccountsManager = this.appState.linkAccountManager, this.linkAccountsManager.subscribe(() => this.setState({})), this.legalManager = this.appState.legalAppManager, this.legalManager.subscribe(() => this.setState({})), this.inputValidationManager = this.appState.inputValidationAppManager, this.inputValidationManager.subscribe(() => this.setState({}));
  }
  componentDidMount() {
    f.updateBackground(`url(${N})`);
    const l = this.appState.getUser();
    l && this.legalManager.fetchPoliciesList(l.country).then(() => {
      const { policiesByCountry: a } = this.legalManager.state;
      this.setState({
        displayedPolicies: w.convertPoliciesToDisplayedPolicies(a || [], E.NAMESPACE)
      });
    }).catch((a) => {
      this.setErrorAlert(a);
    }), this.fetchValidations();
    const t = sessionStorage.getItem(g);
    this.setState({ email: t || "" }), t && this.setState({ step: "otherData" });
  }
  componentWillUnmount() {
    f.updateBackground(null), this.resetSessionStorage();
  }
  resetSessionStorage() {
    sessionStorage.removeItem(g);
  }
  render() {
    const { displayedPolicies: l, alert: t, step: a, email: i } = this.state, r = this.appState.getUser(), { isRequestingVerificationCode: d, isVerifying: m } = this.verificationManager.state, { isUpgradingHeadlessAccount: p } = this.linkAccountsManager.state, { isAccepting: c } = this.legalManager.state, { isFetching: h, fetchError: o, validations: u } = this.inputValidationManager.state, A = this.appState.getBrandName();
    return /* @__PURE__ */ e.createElement("div", { className: s.headlessAccountPage }, /* @__PURE__ */ e.createElement(P, { className: "fixed top-0 right-0 mt-8 mr-8" }), /* @__PURE__ */ e.createElement("div", { className: s.headlessAccountPageContainer }, /* @__PURE__ */ e.createElement(D, null), /* @__PURE__ */ e.createElement("div", { className: s.headlessAccountPageContainerForm }, /* @__PURE__ */ e.createElement("div", { className: s.contentWrapper }, /* @__PURE__ */ e.createElement("div", { className: s.title }, a === "email" && /* @__PURE__ */ e.createElement(n, { i18nKey: "UpgradeHeadlessAccountPage.UpgradeHeadlessAccountPage.title" }, "Upgrade to Full Account"), a === "otherData" && /* @__PURE__ */ e.createElement(n, { i18nKey: "UpgradeHeadlessAccountPage.UpgradeHeadlessAccountForm.title" }, "Finish Account Setup")), /* @__PURE__ */ e.createElement("div", { className: s.description }, a === "email" && /* @__PURE__ */ e.createElement(n, { i18nKey: "UpgradeHeadlessAccountPage.UpgradeHeadlessAccountPage.description" }, "Enter your email address below to start creating your ", { publisherName: A }, " account"), a === "otherData" && /* @__PURE__ */ e.createElement(n, { i18nKey: "UpgradeHeadlessAccountPage.UpgradeHeadlessAccountForm.description" }, "Enter the verification code we’ve sent to your email at ", /* @__PURE__ */ e.createElement("strong", null, { email: i }), ", and complete the form below"))), /* @__PURE__ */ e.createElement("div", { className: s.formWrapper }, !!t && /* @__PURE__ */ e.createElement("div", { className: s.alert }, /* @__PURE__ */ e.createElement(y, { type: t.type, onDismiss: this.dismissAlert }, t.element), !!o && /* @__PURE__ */ e.createElement(C, { className: "btn btn-primary mt-4 w-full", onClick: this.onReload }, /* @__PURE__ */ e.createElement(n, { i18nKey: "Common.reload" }, "Reload"))), a === "email" && /* @__PURE__ */ e.createElement(
      _,
      {
        styles: s,
        onRequestVerificationCode: this.requestVerificationCode,
        isRequesting: d
      }
    ), a === "otherData" && /* @__PURE__ */ e.createElement("div", null, u.length <= 0 && h && /* @__PURE__ */ e.createElement(v, { className: V.medium }), u.length > 0 && !o && r && /* @__PURE__ */ e.createElement(
      T,
      {
        onChangePage: this.onChangePage,
        onContinue: this.onContinue,
        onRequestVerificationCode: this.requestVerificationCode,
        policies: l,
        isUpgradingUser: p || c,
        inputValidationManager: this.inputValidationManager,
        isRequestingVerificationCode: d,
        user: r,
        isVerifying: m,
        styles: s
      }
    )))), /* @__PURE__ */ e.createElement("div", { className: s.backContainer }, /* @__PURE__ */ e.createElement(
      "button",
      {
        onClick: () => {
          this.resetSessionStorage(), this.appState.logout(H.Login.link);
        }
      },
      /* @__PURE__ */ e.createElement(n, { i18nKey: "UpgradeHeadlessAccountPage.UpgradeHeadlessAccountPage.logout" }, "Back to Login")
    )), /* @__PURE__ */ e.createElement(L, null)));
  }
}
export {
  ne as default
};
