import { C as Container, a as Ctx, x as getCurrentLanguage, ac as EligibleUser, B as BaseEvent, h as AlertType, T as Trans, i as CommonError, ay as validateAlphanumeric, aB as Validation, az as ValidateAlphaNumericErrorType, aA as FormGroupError, j as Alert, L as Loading, aO as Form, aC as FormGroup, J as InputGroup, t, f as Button } from "./index.js";
import React__default from "react";
import { A as AccountMainSection, b as AccountMainSectionHeader, a as AccountMainSectionBody } from "./AccountMainSection.js";
import "./validator.js";
import "./dateFns.js";
import "./reactPlayer.js";
import "react-router-dom";
import "./reactSlick.js";
class RedeemCodeLogicEvent extends BaseEvent {
  redeemCodeSuccess() {
    this.emit("redeemCodeSuccess", void 0);
  }
  redeemCodeError(error) {
    this.emit("redeemCodeError", error);
  }
}
class RedeemCodeLogic extends Container {
  constructor(props) {
    super();
    this.event = new RedeemCodeLogicEvent();
    this.props = props;
    this.state = {
      isRedeemingCode: false,
      redeemCodeResult: null,
      error: null
    };
    this.sdk = Ctx.getAppState().getSdk();
  }
  async redeemCode(code) {
    try {
      if (this.state.isRedeemingCode)
        return;
      await this.setState({
        isRedeemingCode: true,
        redeemCodeResult: null,
        error: null
      });
      const user = this.props.user;
      if (!user)
        return;
      const language = getCurrentLanguage();
      const result = await this.sdk.Platform.Fulfillment().redeemCode({
        userId: EligibleUser.safeParse(user).success && user.userId || "",
        data: {
          code,
          language,
          region: user.country
        }
      });
      if (result.error)
        throw result.error;
      this.setState({
        redeemCodeResult: result.response.data
      });
      await this.event.redeemCodeSuccess();
    } catch (error) {
      this.setState({
        error
      });
      this.event.redeemCodeError(error);
    } finally {
      this.setState({ isRedeemingCode: false });
    }
  }
}
var RedeemCodePage$1 = "";
const MAX_REDEEM_CODE_LENGTH = 20;
class RedeemCodePage extends React__default.Component {
  constructor(props) {
    super(props);
    this.notifyRedeemCodeSuccess = () => {
      this.setState({
        alert: {
          type: AlertType.success,
          element: /* @__PURE__ */ React__default.createElement(Trans, {
            i18nKey: "RedeemCodePage.successfully"
          }, "Your code has been redeemed")
        }
      });
    };
    this.notifyRedeemCodeError = (error) => {
      this.setState({
        alert: {
          type: AlertType.danger,
          element: /* @__PURE__ */ React__default.createElement(CommonError, {
            error
          })
        }
      });
    };
    this.onRedeemCodeChange = (event) => {
      const redeemCode = event.target.value;
      this.validation.set("redeemCode", validateAlphanumeric(redeemCode, { maxLength: MAX_REDEEM_CODE_LENGTH }));
      this.setState({ redeemCode });
    };
    this.submitCode = async () => {
      const { redeemCode } = this.state;
      const { redeemCodeResult } = this.logic.state;
      await this.logic.redeemCode(redeemCode);
      if (redeemCodeResult) {
        await this.setState({ redeemCode: "" });
      }
    };
    const user = Ctx.getAppState().getUser();
    this.logic = new RedeemCodeLogic({ user });
    this.logic.subscribe(() => this.setState({}));
    this.state = {
      redeemCode: "",
      alert: null
    };
    this.validation = new Validation();
    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(errorType) {
    if (!errorType)
      return null;
    const redeemCodeErrorMap = {
      [ValidateAlphaNumericErrorType.empty]: /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "RedeemCodePage.redeemCodeForm.redeemCodeFormError.empty"
      }, "You must enter the product code"),
      [ValidateAlphaNumericErrorType.exceedLengthLimit]: /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "RedeemCodePage.redeemCodeForm.redeemCodeFormError.ExceedLimit"
      }, "The product code must not exceed ", { MAX_REDEEM_CODE_LENGTH }, " characters"),
      [ValidateAlphaNumericErrorType.invalidFormat]: /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "RedeemCodePage.redeemCodeForm.redeemCodeFormError.invalidFormat"
      }, "The product code must be alphanumerica and cannot contain spaces or special characters.")
    };
    return /* @__PURE__ */ React__default.createElement(FormGroupError, null, redeemCodeErrorMap[errorType]);
  }
  render() {
    const { alert, redeemCode } = this.state;
    const { isRedeemingCode } = this.logic.state;
    const isSubmitBtnDisabled = !this.validation.isAllValid() || !redeemCode || isRedeemingCode;
    return /* @__PURE__ */ React__default.createElement(AccountMainSection, null, /* @__PURE__ */ React__default.createElement(AccountMainSectionHeader, null, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "RedeemCodePage.title"
    }, "Redeem Code")), /* @__PURE__ */ React__default.createElement(AccountMainSectionBody, {
      className: "redeemCodePage mainBody"
    }, !!alert && !isRedeemingCode && /* @__PURE__ */ React__default.createElement(Alert, {
      type: alert.type,
      className: "alert",
      onDismiss: () => this.setState({ alert: null })
    }, alert.element), isRedeemingCode && /* @__PURE__ */ React__default.createElement(Loading, null, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "RedeemCodePage.loading.message"
    }, "Processing your code...")), !isRedeemingCode && /* @__PURE__ */ React__default.createElement("div", {
      className: "redeemCodePage"
    }, /* @__PURE__ */ React__default.createElement("p", {
      className: "guide"
    }, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "RedeemCodePage.guide"
    }, "Type your product code below to redeem your game, in-game item, bundle, or coins.")), /* @__PURE__ */ React__default.createElement(Form, {
      onSubmit: this.submitCode,
      className: "form",
      orientation: "horizontal"
    }, /* @__PURE__ */ React__default.createElement(FormGroup, null, /* @__PURE__ */ React__default.createElement(InputGroup, {
      className: "inputRedeem",
      disabled: isRedeemingCode,
      type: "text",
      value: redeemCode,
      placeholder: t("RedeemCodePage.form.placeholder"),
      onChange: this.onRedeemCodeChange
    }), this.renderRedeemCodeError(this.validation.get("redeemCode"))), /* @__PURE__ */ React__default.createElement(Button, {
      disabled: isSubmitBtnDisabled,
      className: "btn btn-primary buttonRedeem",
      type: "submit"
    }, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "RedeemCodePage.buttonAction"
    }, "Redeem"))))));
  }
}
export { RedeemCodePage, RedeemCodePage as default };
