import { V as v, X as C, W as S, y as P, h as u, i as y, T as o, E as A, a as b, k as f, l as M, aX as V, t as k, j as U, f as w, L as I, R as H, Q as L, J as N } from "./index-162fd1aa.mjs";
import { L as x, H as R } from "./LinkPlatformAccountPage-7c92cd14.mjs";
import e from "react";
import { E as F, U as W } from "./EmailVerificationForm-2518c77a.mjs";
import "./dateFns-2367ee5b.mjs";
import "./validator-61879ab7.mjs";
import "./reactPlayer-2f69f369.mjs";
import "react-router-dom";
import "./reactSlick-cb4a2d39.mjs";
import "./Tabs-8498653e.mjs";
import "./CommonError-0ae83068.mjs";
import "./starBgGradient-589e5185.mjs";
import "./CommonError-4ed993c7.mjs";
import "./InputPassword-68c495c7.mjs";
import "./Checkbox-5e314906.mjs";
const D = "logoContainer _logoContainer_82512", T = "headlessAccountPage _headlessAccountPage_45d83", q = "headlessAccountPageContainer _headlessAccountPageContainer_856a0", K = "headlessAccountPageContainerForm _headlessAccountPageContainerForm_4a641", O = "contentWrapper _contentWrapper_1c2a9", B = "formWrapper _formWrapper_916f6", z = "alert _alert_8eabd", G = "passwordValidation _passwordValidation_69c47", X = "flexCol _flexCol_1b322", Y = "passwordMismatch _passwordMismatch_efe5e", j = "title _title_0418c", J = "description _description_98165", Q = "resendCode _resendCode_5123f", $ = "tabs _tabs_9274e", Z = "tab _tab_59d8d", ee = "active _active_34cd6", te = "label _label_205e9", ae = "input _input_e0f53", se = "select _select_9c445", ie = "datePicker _datePicker_9d9b7", ne = "agreementList _agreementList_a6697", re = "agreementItem _agreementItem_fa03c", oe = "text _text_b24d1", ce = "optional _optional_2c013", le = "changePage _changePage_52387", de = "backContainer _backContainer_d13a3", i = {
  logoContainer: D,
  headlessAccountPage: T,
  headlessAccountPageContainer: q,
  headlessAccountPageContainerForm: K,
  contentWrapper: O,
  formWrapper: B,
  alert: z,
  passwordValidation: G,
  flexCol: X,
  passwordMismatch: Y,
  title: j,
  description: J,
  resendCode: Q,
  tabs: $,
  tab: Z,
  active: ee,
  label: te,
  input: ae,
  select: se,
  datePicker: ie,
  agreementList: ne,
  agreementItem: re,
  text: oe,
  optional: ce,
  changePage: le,
  backContainer: de
}, g = "registration_email";
class Ve extends e.Component {
  constructor(c) {
    super(c), this.fetchValidations = async () => {
      this.inputValidationManager.fetchValidations(v()).catch((t) => {
        this.setErrorAlert(t);
      });
    }, this.requestVerificationCode = async (t = this.state.email) => {
      try {
        if (!t)
          return;
        await this.verificationManager.sendVerificationCode(t, C.enum.upgradeHeadlessAccount), this.setState({ email: t }, this.redirectToOtherData);
      } catch {
        const { requestVerificationCodeError: s } = this.verificationManager.state;
        s && this.setErrorAlert(s);
      }
    }, this.setErrorAlert = async (t) => {
      if (await this.inputValidationManager.isInputValidationError(t)) {
        S.openValidationOutdatedModal(), this.setState({ alert: null });
        return;
      }
      const a = P.extractServiceErrorCode(t);
      this.setState({
        alert: {
          type: u.danger,
          element: (() => {
            switch (a) {
              case 10130:
                return /* @__PURE__ */ e.createElement(o, { i18nKey: "UpdateUser.error.updateErrorUnderage" }, "You are below the required age");
              default:
                return /* @__PURE__ */ e.createElement(y, { 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: u.success,
          element: /* @__PURE__ */ e.createElement(o, { 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: s,
      reachMinimumAge: n,
      verificationCode: d,
      acceptedPolicyIds: _
    }) => {
      const { email: p } = this.state, l = this.appState.getUser(), m = l && l.country ? l.country : "";
      try {
        await this.linkAccountsManager.upgradeHeadlessAccount(
          {
            displayName: a,
            password: t,
            username: s,
            country: m,
            reachMinimumAge: n,
            emailAddress: p,
            code: d
          },
          A.IS_DISABLE_USERNAME
        ), await this.bulkAcceptPolicies(_), this.resetSessionStorage();
        const r = await this.appState.userAppManager.refreshToken();
        r && this.appState.refreshSession(r.access_token, r.refresh_token), await this.appState.refreshUser();
      } catch (r) {
        this.setErrorAlert(r);
      }
    }, this.getEligibilities = (t) => {
      const a = [], { displayedPolicies: s } = this.state;
      for (const n of s)
        a.push({
          policyId: n.policyId,
          policyVersionId: n.policyVersionId,
          localizedPolicyVersionId: n.localizedPolicyVersionId,
          isAccepted: t.includes(n.policyId)
        });
      return a;
    }, this.bulkAcceptPolicies = async (t) => {
      const a = this.appState.getUser(), s = this.getEligibilities(t);
      s && a && await this.legalManager.bulkAcceptPolicies(s);
    }, 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(${M})`);
    const c = this.appState.getUser();
    c && this.legalManager.fetchPoliciesList(c.country).then(() => {
      const { policiesByCountry: a } = this.legalManager.state;
      this.setState({
        displayedPolicies: V.convertPoliciesToDisplayedPolicies(a || [], A.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: c, alert: t, step: a, email: s } = this.state, n = this.appState.getUser(), { isRequestingVerificationCode: d, isVerifying: _ } = this.verificationManager.state, { isUpgradingHeadlessAccount: p } = this.linkAccountsManager.state, { isAccepting: l } = this.legalManager.state, { isFetching: m, fetchError: r, validations: h } = this.inputValidationManager.state, E = k("Common.publisherName");
    return /* @__PURE__ */ e.createElement("div", { className: i.headlessAccountPage }, /* @__PURE__ */ e.createElement(x, { className: "fixed top-0 right-0 mt-8 mr-8" }), /* @__PURE__ */ e.createElement("div", { className: i.headlessAccountPageContainer }, /* @__PURE__ */ e.createElement(R, null), /* @__PURE__ */ e.createElement("div", { className: i.headlessAccountPageContainerForm }, /* @__PURE__ */ e.createElement("div", { className: i.contentWrapper }, /* @__PURE__ */ e.createElement("div", { className: i.title }, a === "email" && /* @__PURE__ */ e.createElement(o, { i18nKey: "UpgradeHeadlessAccountPage.UpgradeHeadlessAccountPage.title" }, "Upgrade to Full Account"), a === "otherData" && /* @__PURE__ */ e.createElement(o, { i18nKey: "UpgradeHeadlessAccountPage.UpgradeHeadlessAccountForm.title" }, "Finish Account Setup")), /* @__PURE__ */ e.createElement("div", { className: i.description }, a === "email" && /* @__PURE__ */ e.createElement(o, { i18nKey: "UpgradeHeadlessAccountPage.UpgradeHeadlessAccountPage.description" }, "Enter your email address below to start creating your ", { publisherName: E }, " account"), a === "otherData" && /* @__PURE__ */ e.createElement(o, { i18nKey: "UpgradeHeadlessAccountPage.UpgradeHeadlessAccountForm.description" }, "Enter the verification code we’ve sent to your email at ", /* @__PURE__ */ e.createElement("strong", null, { email: s }), ", and complete the form below"))), /* @__PURE__ */ e.createElement("div", { className: i.formWrapper }, !!t && /* @__PURE__ */ e.createElement("div", { className: i.alert }, /* @__PURE__ */ e.createElement(U, { type: t.type, onDismiss: this.dismissAlert }, t.element), !!r && /* @__PURE__ */ e.createElement(w, { className: "btn btn-primary mt-4 w-full", onClick: this.onReload }, /* @__PURE__ */ e.createElement(o, { i18nKey: "Common.reload" }, "Reload"))), a === "email" && /* @__PURE__ */ e.createElement(
      F,
      {
        styles: i,
        onRequestVerificationCode: this.requestVerificationCode,
        isRequesting: d
      }
    ), a === "otherData" && /* @__PURE__ */ e.createElement("div", null, h.length <= 0 && m && /* @__PURE__ */ e.createElement(I, { className: H.medium }), h.length > 0 && !r && n && /* @__PURE__ */ e.createElement(
      W,
      {
        onChangePage: this.onChangePage,
        onContinue: this.onContinue,
        onRequestVerificationCode: this.requestVerificationCode,
        policies: c,
        isUpgradingUser: p || l,
        inputValidationManager: this.inputValidationManager,
        isRequestingVerificationCode: d,
        user: n,
        isVerifying: _,
        styles: i
      }
    )))), /* @__PURE__ */ e.createElement("div", { className: i.backContainer }, /* @__PURE__ */ e.createElement(
      "button",
      {
        onClick: () => {
          this.resetSessionStorage(), this.appState.logout(L.Login.link);
        }
      },
      /* @__PURE__ */ e.createElement(o, { i18nKey: "UpgradeHeadlessAccountPage.UpgradeHeadlessAccountPage.logout" }, "Back to Login")
    )), /* @__PURE__ */ e.createElement(N, null)));
  }
}
export {
  Ve as default
};
