import { Button as O } from "./accelbyte-widgets107.mjs";
import { Checkbox as L } from "./accelbyte-widgets335.mjs";
import { FormGroupError as c, FormGroup as f } from "./accelbyte-widgets263.mjs";
import { InputGroup as v } from "./accelbyte-widgets229.mjs";
import { InputGroupPassword as K } from "./accelbyte-widgets264.mjs";
import { PasswordConstraint as U } from "./accelbyte-widgets265.mjs";
import { t as r } from "./accelbyte-widgets58.mjs";
import { Ctx as H } from "./accelbyte-widgets33.mjs";
import { Env as I } from "./accelbyte-widgets108.mjs";
import { InputValidationHelper as p, ValidateableInputField as y } from "@accelbyte/sdk-iam";
import e from "react";
import { Trans as h } from "react-i18next";
import { UpgradeHeadlessVerification as B } from "./accelbyte-widgets349.mjs";
import { LegalUtils as x } from "./accelbyte-widgets147.mjs";
import { FeatureFlags as G } from "./accelbyte-widgets57.mjs";
import { LegalHelper as w } from "@accelbyte/sdk-legal";
import { ValidateRegexErrorType as C } from "./accelbyte-widgets290.mjs";
import { ValidateLengthErrorType as g } from "./accelbyte-widgets289.mjs";
import { Validation as Y } from "./accelbyte-widgets269.mjs";
class ue extends e.Component {
  constructor(i) {
    super(i), this.updatePolicies = () => {
      const { policies: t } = this.props;
      t && this.setState({
        requiredPolicies: t.filter((a) => a.isMandatory),
        optionalPolicies: t.filter((a) => !a.isMandatory)
      });
    }, this.isDisplayNameDisabled = () => I.IS_DISABLE_DISPLAY_NAME, this.isUserNameDisabled = () => I.IS_DISABLE_USERNAME, this.onChangeDisplayName = (t) => {
      const a = t.target.value, { validations: n } = this.props.inputValidationManager.state;
      this.validation.set("displayName", p.validateDisplayName(a, this.isUserNameDisabled(), n)), this.setState({ displayName: a });
    }, this.onChangeUsername = (t) => {
      const a = t.target.value, { validations: n } = this.props.inputValidationManager.state;
      this.validation.set("username", p.validateUserName(a, n)), this.setState({ username: a });
    }, this.onPasswordChange = (t) => {
      const a = t.target.value, { validations: n } = this.props.inputValidationManager.state;
      this.validation.set("password", p.validatePassword(a, n)), this.setState({
        password: a
      });
    }, this.onVerificationCodeChange = (t) => {
      this.setState({ verificationCode: t });
    }, this.renderPasswordError = ({ min: t, max: a } = {}) => {
      const n = this.validation.get("password");
      if (!n)
        return null;
      switch (n) {
        case g.enum.empty:
          return /* @__PURE__ */ e.createElement(c, null, r("UpgradeHeadlessAccountPage.UpgradeHeadlessAccountForm.form.passwordEmptyError"));
        case g.enum.lessThanLengthLimit:
          return /* @__PURE__ */ e.createElement(c, null, r("UpgradeHeadlessAccountPage.UpgradeHeadlessAccountForm.form.passwordLessThanLengthLimit", { PASSWORD_MIN_LIMIT: t }));
        case g.enum.exceedLengthLimit:
          return /* @__PURE__ */ e.createElement(c, null, r("UpgradeHeadlessAccountPage.UpgradeHeadlessAccountForm.form.passwordExceedLimit", { PASSWORD_MAX_LENGTH: a }));
        case C.enum.invalidFormat:
          return /* @__PURE__ */ e.createElement(c, null, r("UpgradeHeadlessAccountPage.UpgradeHeadlessAccountForm.form.passwordInvalidFormat"));
        default:
          return r("CommonError.unknownError");
      }
    }, this.renderMinimumAgeCheckbox = () => {
      const { reachMinimumAge: t } = this.state, { age: a } = this.userManager.state.ageRestriction, { styles: n } = this.props;
      if (a)
        return /* @__PURE__ */ e.createElement("div", { className: n.agreementList }, /* @__PURE__ */ e.createElement("div", { className: n.agreementItem }, /* @__PURE__ */ e.createElement(
          L,
          {
            isChecked: t,
            onChange: () => {
              this.setState({ reachMinimumAge: !t });
            }
          }
        ), /* @__PURE__ */ e.createElement("div", { className: n.text }, /* @__PURE__ */ e.createElement(h, { i18nKey: "UpgradeHeadlessAccountPage.UpgradeHeadlessAccountForm.minimumAge" }, "I confirm that I am ", { age: a }, " years of age or older."))));
    }, this.renderRequiredPolicies = () => {
      const { requiredPolicies: t, isRequiredPolicyAccepted: a } = this.state, { styles: n } = this.props;
      if (t.length)
        return /* @__PURE__ */ e.createElement("div", { className: n.agreementList }, /* @__PURE__ */ e.createElement("div", { className: n.agreementItem }, /* @__PURE__ */ e.createElement(
          L,
          {
            isChecked: a,
            onChange: () => {
              this.setState({ isRequiredPolicyAccepted: !a });
            }
          }
        ), /* @__PURE__ */ e.createElement("div", { className: n.text }, r("UpgradeHeadlessAccountPage.UpgradeHeadlessAccountForm.tos.haveRead"), t.map((s, o) => {
          const l = w.createLegalURL(x.getLegalUrl(), s.localizedPolicyVersionId);
          return /* @__PURE__ */ e.createElement(e.Fragment, null, o === t.length - 1 && t.length > 1 && r("UpgradeHeadlessAccountPage.UpgradeHeadlessAccountForm.tos.and"), /* @__PURE__ */ e.createElement("span", { key: o }, /* @__PURE__ */ e.createElement("a", { href: l, target: "_blank", rel: "noreferrer" }, s.policyName)), t.length !== 1 && o !== t.length - 1 && o !== t.length - 2 ? ", " : " ");
        }))));
    }, this.renderOptionalPolicies = () => {
      const { optionalPolicies: t, acceptedOptionalPolicyIdMap: a } = this.state, { styles: n } = this.props;
      return !t || !t.length ? null : t.map((s) => {
        const o = w.createLegalURL(x.getLegalUrl(), s.localizedPolicyVersionId);
        return /* @__PURE__ */ e.createElement("div", { className: n.agreementList, key: s.policyId }, /* @__PURE__ */ e.createElement("div", { className: n.agreementItem }, /* @__PURE__ */ e.createElement(
          L,
          {
            isChecked: a.get(s.policyId),
            onChange: () => {
              this.onClickOptionalPolicyCheckbox(s);
            }
          }
        ), /* @__PURE__ */ e.createElement("span", { className: `${n.text} ${n.optional}` }, /* @__PURE__ */ e.createElement(h, { i18nKey: "linkPlatformAccountPage.LinkNewAccount.optional" }, "[Optional]")), /* @__PURE__ */ e.createElement("div", { className: n.text }, s.policyName, /* @__PURE__ */ e.createElement("a", { href: o, target: "_blank", rel: "noreferrer" }, /* @__PURE__ */ e.createElement(h, { i18nKey: "UpgradeHeadlessAccountPage.UpgradeHeadlessAccountPage.learnMore" }, "Learn more.")))));
      });
    }, this.onClickOptionalPolicyCheckbox = (t) => {
      const { acceptedOptionalPolicyIdMap: a } = this.state;
      a.set(t.policyId, !a.get(t.policyId)), this.setState({ acceptedOptionalPolicyIdMap: a });
    }, this.onClickContinue = () => {
      const { isRequiredPolicyAccepted: t, requiredPolicies: a, acceptedOptionalPolicyIdMap: n } = this.state, s = [];
      if (t)
        for (const o of a)
          s.push(o.policyId);
      n.forEach((o, l) => {
        o && s.push(l);
      }), this.props.onContinue({
        ...this.state,
        acceptedPolicyIds: s,
        reachMinimumAge: !0
      });
    }, this.state = {
      verificationCode: "",
      displayName: "",
      username: "",
      password: "",
      requiredPolicies: [],
      optionalPolicies: [],
      isRequiredPolicyAccepted: !1,
      reachMinimumAge: !1,
      acceptedOptionalPolicyIdMap: /* @__PURE__ */ new Map()
    }, this.userManager = H.getAppState().userAppManager, this.validation = new Y(), this.validation.listen(() => this.setState({}));
  }
  componentDidMount() {
    const { country: i } = this.props.user;
    this.userManager.getAgeRestrictionByCountry(i), this.updatePolicies();
  }
  componentDidUpdate(i) {
    const { policies: t } = this.props;
    JSON.stringify(t) !== JSON.stringify(i.policies) && this.updatePolicies();
  }
  renderDisplayNameError(i, t = {}) {
    if (!i)
      return null;
    switch (i) {
      case g.enum.empty:
        return /* @__PURE__ */ e.createElement(c, null, r("AccountMyProfilePage.form.displayNameEmptyError"));
      case g.enum.lessThanLengthLimit:
        return /* @__PURE__ */ e.createElement(c, null, r("AccountMyProfilePage.form.lessThanLengthLimit", { DISPLAY_NAME_MIN_LIMIT: t.min }));
      case g.enum.exceedLengthLimit:
        return /* @__PURE__ */ e.createElement(c, null, r("AccountMyProfilePage.form.displayNameExceedLimit", { MAX_DISPLAY_NAME_LENGTH: t.max }));
      case C.enum.invalidFormat:
        return /* @__PURE__ */ e.createElement(c, null, r("AccountMyProfilePage.form.displayNameInvalidFormat"));
      default:
        return r("CommonError.unknownError");
    }
  }
  renderUserNameError(i, t = {}) {
    if (!i)
      return null;
    switch (i) {
      case g.enum.empty:
        return /* @__PURE__ */ e.createElement(c, null, /* @__PURE__ */ e.createElement(h, { i18nKey: "AccountMyProfilePage.form.userName.emptyError" }, "The username cannot be empty"));
      case g.enum.lessThanLengthLimit:
        return /* @__PURE__ */ e.createElement(c, null, /* @__PURE__ */ e.createElement(h, { i18nKey: "AccountMyProfilePage.form.userName.lessThanLengthLimit" }, "Your username must contain at least ", { MIN_USERNAME_LENGTH: t.min }, " characters"));
      case g.enum.exceedLengthLimit:
        return /* @__PURE__ */ e.createElement(c, null, /* @__PURE__ */ e.createElement(h, { i18nKey: "AccountMyProfilePage.form.userName.exceedLimit" }, "Your username must not exceed ", { MAX_USERNAME_LENGTH: t.max }, " characters"));
      case C.enum.invalidFormat:
        return /* @__PURE__ */ e.createElement(c, null, /* @__PURE__ */ e.createElement(h, { i18nKey: "AccountMyProfilePage.form.userName.invalidFormat" }, "Please enter a valid username format"));
      default:
        return /* @__PURE__ */ e.createElement(h, { i18nKey: "CommonError.unknownError" }, "Something went wrong. Please contact administrator.");
    }
  }
  render() {
    const { user: i, onChangePage: t, inputValidationManager: a, onRequestVerificationCode: n, isRequestingVerificationCode: s, isUpgradingUser: o, styles: l } = this.props, { validations: E } = a.state, { displayName: N, username: P, password: A, requiredPolicies: S, isRequiredPolicyAccepted: b, reachMinimumAge: F, verificationCode: M } = this.state, { countryName: V, age: D } = this.userManager.state.ageRestriction, m = p.getValidationByKey(y.enum.username, E), d = p.getValidationByKey(y.enum.displayName, E), u = p.getValidationByKey(y.enum.password, E), R = this.isUserNameDisabled() ? N.length > 0 : !0, _ = this.isUserNameDisabled() || P.length > 0, k = M.length > 0, T = H.hasFeature(G.isLegalEnabled), q = this.validation.isAllValid() && (b || S.length === 0) && (F || !D) && A.length > 0 && R && _ && k;
    if (i)
      return /* @__PURE__ */ e.createElement("div", null, /* @__PURE__ */ e.createElement(
        B,
        {
          verificationCode: M,
          onChangePage: t,
          isRequestingVerificationCode: s,
          onRequestVerificationCode: n,
          onChangeVerificationCode: this.onVerificationCodeChange,
          validation: this.validation,
          styles: l
        }
      ), /* @__PURE__ */ e.createElement("hr", null), /* @__PURE__ */ e.createElement(f, null, /* @__PURE__ */ e.createElement("div", { className: l.label }, r("UpgradeHeadlessAccountPage.UpgradeHeadlessAccountForm.country")), /* @__PURE__ */ e.createElement(v, { type: "text", value: V || i.country, disabled: !0, className: l.input })), !this.isUserNameDisabled() && /* @__PURE__ */ e.createElement(f, null, /* @__PURE__ */ e.createElement("div", { className: l.label }, /* @__PURE__ */ e.createElement("div", null, r("UpgradeHeadlessAccountPage.UpgradeHeadlessAccountForm.userName")), /* @__PURE__ */ e.createElement("div", null, P.length, "/", m == null ? void 0 : m.maxLength)), /* @__PURE__ */ e.createElement(
        v,
        {
          className: l.input,
          value: P,
          onChange: this.onChangeUsername,
          placeholder: r("UpgradeHeadlessAccountPage.UpgradeHeadlessAccountForm.placeholder.userName"),
          disabled: !!this.props.inputValidationManager.state.fetchError,
          popoverContent: /* @__PURE__ */ e.createElement(
            U,
            {
              descriptions: p.getValidationDescription(y.enum.username, E)
            }
          ),
          maxLength: m == null ? void 0 : m.maxLength
        }
      ), this.renderUserNameError(this.validation.get("username"), {
        min: m && m.minLength,
        max: m && m.maxLength
      })), !this.isDisplayNameDisabled() && /* @__PURE__ */ e.createElement(f, null, /* @__PURE__ */ e.createElement("div", { className: l.label }, /* @__PURE__ */ e.createElement("div", null, `${r("UpgradeHeadlessAccountPage.UpgradeHeadlessAccountForm.displayName")} ${r(
        "UpgradeHeadlessAccountPage.UpgradeHeadlessAccountForm.tos.optional"
      )}`), /* @__PURE__ */ e.createElement("div", null, N.length, "/", d == null ? void 0 : d.maxLength)), /* @__PURE__ */ e.createElement(
        v,
        {
          className: l.input,
          value: N,
          onChange: this.onChangeDisplayName,
          placeholder: r("UpgradeHeadlessAccountPage.UpgradeHeadlessAccountForm.placeholder.displayName"),
          popoverContent: /* @__PURE__ */ e.createElement(
            U,
            {
              descriptions: p.getValidationDescription(y.enum.displayName, E)
            }
          ),
          maxLength: d == null ? void 0 : d.maxLength
        }
      ), this.renderDisplayNameError(this.validation.get("displayName"), {
        min: d && d.minLength,
        max: d && d.maxLength
      })), /* @__PURE__ */ e.createElement(f, null, /* @__PURE__ */ e.createElement("div", { className: l.label }, /* @__PURE__ */ e.createElement("div", null, r("UpgradeHeadlessAccountPage.UpgradeHeadlessAccountForm.password")), /* @__PURE__ */ e.createElement("div", null, A.length, "/", u == null ? void 0 : u.maxLength)), /* @__PURE__ */ e.createElement(
        K,
        {
          className: l.input,
          value: A,
          placeholder: r("UpgradeHeadlessAccountPage.UpgradeHeadlessAccountForm.placeholder.password"),
          togglePassword: !0,
          onChange: this.onPasswordChange,
          popoverContent: /* @__PURE__ */ e.createElement(
            U,
            {
              descriptions: p.getValidationDescription(y.enum.password, E)
            }
          ),
          maxLength: u == null ? void 0 : u.maxLength,
          hasStrengthMeter: !this.validation.get("password") && !!A
        }
      ), this.renderPasswordError({
        min: u && u.minLength,
        max: u && u.maxLength
      })), /* @__PURE__ */ e.createElement(f, null, this.renderMinimumAgeCheckbox(), this.renderRequiredPolicies(), T && this.renderOptionalPolicies()), /* @__PURE__ */ e.createElement(
        O,
        {
          loadingIndicator: "SPINNER",
          className: "btn btn-primary w-full uppercase",
          onClick: () => this.onClickContinue(),
          disabled: !q || o,
          isLoading: o
        },
        r("common.complete")
      ));
  }
}
export {
  ue as UpgradeHeadlessAccountForm
};
