import { Alert as l } from "./accelbyte-widgets123.mjs";
import { Button as C } from "./accelbyte-widgets107.mjs";
import { CommonError as u } from "./accelbyte-widgets124.mjs";
import { FormGroupError as E, Form as h, FormGroup as g } from "./accelbyte-widgets263.mjs";
import { InputGroup as p } from "./accelbyte-widgets229.mjs";
import { Loading as R } from "./accelbyte-widgets136.mjs";
import { t as y } from "./accelbyte-widgets58.mjs";
import { Ctx as f } from "./accelbyte-widgets33.mjs";
import { mockDlcItemId as A } from "./accelbyte-widgets209.mjs";
import { AlertType as s } from "./accelbyte-widgets128.mjs";
import { ErrorHelper as _ } from "./accelbyte-widgets120.mjs";
import e from "react";
import { Trans as r } from "react-i18next";
import { AccountMainSection as D, AccountMainSectionHeader as P, AccountMainSectionBody as b } from "./accelbyte-widgets122.mjs";
import { RedeemCodeLogic as v } from "./accelbyte-widgets291.mjs";
import "./accelbyte-widgets292.mjs";
import { validateAlphanumeric as L, ValidateAlphaNumericErrorType as m } from "./accelbyte-widgets293.mjs";
import { Validation as I } from "./accelbyte-widgets269.mjs";
const c = 20, i = {
  INVALID_CODE: 37142,
  CODE_NO_LONGER_AVAILABLE: 37071,
  INACTIVE_CAMPAIGN: 37172,
  MAX_REDEEM_CODE: 37174,
  CODE_ALREADY_CLAIMED: 37179
};
class j extends e.Component {
  constructor(o) {
    super(o), this.notifyRedeemCodeSuccess = () => {
      this.setState({
        alert: {
          type: s.success,
          element: /* @__PURE__ */ e.createElement(r, { i18nKey: "RedeemCodePage.successfully" }, "Your code has been redeemed")
        }
      });
    }, this.getErrorCodeRedeemMessage = (t) => {
      switch (_.extractServiceErrorCode(t)) {
        case i.INVALID_CODE:
          return /* @__PURE__ */ e.createElement(r, { i18nKey: "RedeemCodePage.error.invalidCode" }, "Invalid code. Please check the code and try again.");
        case i.CODE_NO_LONGER_AVAILABLE:
          return /* @__PURE__ */ e.createElement(r, { i18nKey: "RedeemCodePage.error.noLongerAvailable" }, "This code is no longer available.");
        case i.INACTIVE_CAMPAIGN:
          return /* @__PURE__ */ e.createElement(r, { i18nKey: "RedeemCodePage.error.inactiveCampaign" }, "Campaign is inactive check again late");
        case i.MAX_REDEEM_CODE:
          return /* @__PURE__ */ e.createElement(r, { i18nKey: "RedeemCodePage.error.maxRedeemCount" }, "This code has already been redeemed the maximum number of times allowed per user. Please try a different code.");
        case i.CODE_ALREADY_CLAIMED:
          return /* @__PURE__ */ e.createElement(r, { i18nKey: "RedeemCodePage.error.codeAlreadyClaimed" }, "This code has already been redeemed. Please enter a different code.");
        default:
          return /* @__PURE__ */ e.createElement(u, { error: t });
      }
    }, this.notifyRedeemCodeError = (t) => {
      this.setState({
        alert: {
          type: s.danger,
          element: this.getErrorCodeRedeemMessage(t)
        }
      });
    }, this.onRedeemCodeChange = (t) => {
      const a = t.target.value;
      this.validation.set("redeemCode", L(a, { maxLength: c })), this.setState({ redeemCode: a });
    }, this.submitCode = async () => {
      const { redeemCode: t } = this.state, { redeemCodeResult: a } = this.logic.state;
      await this.logic.redeemCode(t), a && await this.setState({ redeemCode: "" });
    };
    const d = f.getAppState().getUser();
    this.logic = new v({ user: d }), this.logic.subscribe(() => this.setState({})), this.state = {
      redeemCode: "",
      alert: null
    }, this.validation = new I(), this.validation.listen(() => this.setState({}));
  }
  componentDidMount() {
    this.logic.event.register("redeemCodeSuccess", this.notifyRedeemCodeSuccess), this.logic.event.register("redeemCodeError", this.notifyRedeemCodeError);
  }
  componentWillUnmount() {
    this.logic.event.unregister("redeemCodeSuccess", this.notifyRedeemCodeSuccess), this.logic.event.unregister("redeemCodeError", this.notifyRedeemCodeError);
  }
  renderRedeemCodeError(o) {
    if (!o)
      return null;
    const d = {
      [m.enum.empty]: /* @__PURE__ */ e.createElement(r, { i18nKey: "RedeemCodePage.redeemCodeForm.redeemCodeFormError.empty" }, "You must enter the product code"),
      [m.enum.exceedLengthLimit]: /* @__PURE__ */ e.createElement(r, { i18nKey: "RedeemCodePage.redeemCodeForm.redeemCodeFormError.ExceedLimit" }, "The product code must not exceed ", { MAX_REDEEM_CODE_LENGTH: c }, " characters"),
      [m.enum.invalidFormat]: /* @__PURE__ */ e.createElement(r, { i18nKey: "RedeemCodePage.redeemCodeForm.redeemCodeFormError.invalidFormat" }, "The product code must be alphanumerica and cannot contain spaces or special characters.")
    };
    return /* @__PURE__ */ e.createElement(E, null, d[o]);
  }
  render() {
    const { alert: o, redeemCode: d } = this.state, { isRedeemingCode: t } = this.logic.state, a = !this.validation.isAllValid() || !d || t, n = this.props.isPreviewMode;
    return /* @__PURE__ */ e.createElement(D, null, /* @__PURE__ */ e.createElement(P, null, /* @__PURE__ */ e.createElement(r, { i18nKey: "RedeemCodePage.title" }, "Redeem Code")), /* @__PURE__ */ e.createElement(b, { className: "redeemCodePage mainBody" }, !!o && !t && /* @__PURE__ */ e.createElement(l, { type: o.type, className: "alert", onDismiss: () => this.setState({ alert: null }) }, o.element), t && /* @__PURE__ */ e.createElement(R, null, /* @__PURE__ */ e.createElement(r, { i18nKey: "RedeemCodePage.loading.message" }, "Processing your code...")), !t && /* @__PURE__ */ e.createElement("div", { className: "redeemCodePage" }, /* @__PURE__ */ e.createElement("p", { className: "guide" }, /* @__PURE__ */ e.createElement(r, { i18nKey: "RedeemCodePage.guide" }, "Type your product code below to redeem your game, in-game item, bundle, or coins.")), /* @__PURE__ */ e.createElement(h, { onSubmit: this.submitCode, className: "form", orientation: "horizontal" }, /* @__PURE__ */ e.createElement(g, null, /* @__PURE__ */ e.createElement(
      p,
      {
        className: "inputRedeem",
        disabled: t,
        type: "text",
        value: n ? A : d,
        placeholder: y("RedeemCodePage.form.placeholder"),
        onChange: this.onRedeemCodeChange,
        readOnly: n
      }
    ), this.renderRedeemCodeError(this.validation.get("redeemCode"))), /* @__PURE__ */ e.createElement(C, { disabled: a, className: "btn btn-primary buttonRedeem", type: "submit" }, /* @__PURE__ */ e.createElement(r, { i18nKey: "RedeemCodePage.buttonAction" }, "Redeem"))))));
  }
}
export {
  j as RedeemCodePage,
  j as default
};
