import { C as u, a as l, V as C, w as h, B as g, h as m, T as a, i as p, aB as E, aE as R, aC as d, aD as y, j as S, L as f, aS as v, aF as b, $ as P, t as A, f as F } from "./index-162fd1aa.mjs";
import e from "react";
import { A as w, b as x, a as L } from "./AccountMainSection-4c35f8d7.mjs";
import { u as N } from "./MockVars-1cf2eb0a.mjs";
import "./dateFns-2367ee5b.mjs";
import "./validator-61879ab7.mjs";
import "./reactPlayer-2f69f369.mjs";
import "react-router-dom";
import "./reactSlick-cb4a2d39.mjs";
class K extends g {
  redeemCodeSuccess() {
    this.emit("redeemCodeSuccess", void 0);
  }
  redeemCodeError(t) {
    this.emit("redeemCodeError", t);
  }
}
class M extends u {
  constructor(t) {
    super(), this.event = new K(), this.props = t, this.state = {
      isRedeemingCode: !1,
      redeemCodeResult: null,
      error: null
    }, this.sdk = l.getAppState().getSdk();
  }
  async redeemCode(t) {
    try {
      if (this.state.isRedeemingCode)
        return;
      await this.setState({
        isRedeemingCode: !0,
        redeemCodeResult: null,
        error: null
      });
      const r = this.props.user;
      if (!r)
        return;
      const o = C(), s = await this.sdk.Platform.Fulfillment().redeemCode({
        userId: h.safeParse(r).success && r.userId || "",
        data: {
          code: t,
          language: o,
          region: r.country
        }
      });
      if (s.error)
        throw s.error;
      this.setState({
        redeemCodeResult: s.response.data
      }), await this.event.redeemCodeSuccess();
    } catch (r) {
      this.setState({
        error: r
      }), this.event.redeemCodeError(r);
    } finally {
      this.setState({ isRedeemingCode: !1 });
    }
  }
}
const c = 20;
class H extends e.Component {
  constructor(t) {
    super(t), this.notifyRedeemCodeSuccess = () => {
      this.setState({
        alert: {
          type: m.success,
          element: /* @__PURE__ */ e.createElement(a, { i18nKey: "RedeemCodePage.successfully" }, "Your code has been redeemed")
        }
      });
    }, this.notifyRedeemCodeError = (o) => {
      this.setState({
        alert: {
          type: m.danger,
          element: /* @__PURE__ */ e.createElement(p, { error: o })
        }
      });
    }, this.onRedeemCodeChange = (o) => {
      const s = o.target.value;
      this.validation.set("redeemCode", E(s, { maxLength: c })), this.setState({ redeemCode: s });
    }, this.submitCode = async () => {
      const { redeemCode: o } = this.state, { redeemCodeResult: s } = this.logic.state;
      await this.logic.redeemCode(o), s && await this.setState({ redeemCode: "" });
    };
    const r = l.getAppState().getUser();
    this.logic = new M({ user: r }), this.logic.subscribe(() => this.setState({})), this.state = {
      redeemCode: "",
      alert: null
    }, this.validation = new R(), 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(t) {
    if (!t)
      return null;
    const r = {
      [d.empty]: /* @__PURE__ */ e.createElement(a, { i18nKey: "RedeemCodePage.redeemCodeForm.redeemCodeFormError.empty" }, "You must enter the product code"),
      [d.exceedLengthLimit]: /* @__PURE__ */ e.createElement(a, { i18nKey: "RedeemCodePage.redeemCodeForm.redeemCodeFormError.ExceedLimit" }, "The product code must not exceed ", { MAX_REDEEM_CODE_LENGTH: c }, " characters"),
      [d.invalidFormat]: /* @__PURE__ */ e.createElement(a, { i18nKey: "RedeemCodePage.redeemCodeForm.redeemCodeFormError.invalidFormat" }, "The product code must be alphanumerica and cannot contain spaces or special characters.")
    };
    return /* @__PURE__ */ e.createElement(y, null, r[t]);
  }
  render() {
    const { alert: t, redeemCode: r } = this.state, { isRedeemingCode: o } = this.logic.state, s = !this.validation.isAllValid() || !r || o, n = this.props.isPreviewMode;
    return /* @__PURE__ */ e.createElement(w, null, /* @__PURE__ */ e.createElement(x, null, /* @__PURE__ */ e.createElement(a, { i18nKey: "RedeemCodePage.title" }, "Redeem Code")), /* @__PURE__ */ e.createElement(L, { className: "redeemCodePage mainBody" }, !!t && !o && /* @__PURE__ */ e.createElement(S, { type: t.type, className: "alert", onDismiss: () => this.setState({ alert: null }) }, t.element), o && /* @__PURE__ */ e.createElement(f, null, /* @__PURE__ */ e.createElement(a, { i18nKey: "RedeemCodePage.loading.message" }, "Processing your code...")), !o && /* @__PURE__ */ e.createElement("div", { className: "redeemCodePage" }, /* @__PURE__ */ e.createElement("p", { className: "guide" }, /* @__PURE__ */ e.createElement(a, { i18nKey: "RedeemCodePage.guide" }, "Type your product code below to redeem your game, in-game item, bundle, or coins.")), /* @__PURE__ */ e.createElement(v, { onSubmit: this.submitCode, className: "form", orientation: "horizontal" }, /* @__PURE__ */ e.createElement(b, null, /* @__PURE__ */ e.createElement(
      P,
      {
        className: "inputRedeem",
        disabled: o,
        type: "text",
        value: n ? N : r,
        placeholder: A("RedeemCodePage.form.placeholder"),
        onChange: this.onRedeemCodeChange,
        readOnly: n
      }
    ), this.renderRedeemCodeError(this.validation.get("redeemCode"))), /* @__PURE__ */ e.createElement(F, { disabled: s, className: "btn btn-primary buttonRedeem", type: "submit" }, /* @__PURE__ */ e.createElement(a, { i18nKey: "RedeemCodePage.buttonAction" }, "Redeem"))))));
  }
}
export {
  H as RedeemCodePage,
  H as default
};
