import { getCurrentLanguage as o } from "./accelbyte-widgets58.mjs";
import { BaseEvent as i } from "./accelbyte-widgets333.mjs";
import { Ctx as d } from "./accelbyte-widgets33.mjs";
import { EligibleUser as m } from "@accelbyte/sdk-iam";
import { Container as n } from "unstated";
import { Platform as a } from "@accelbyte/sdk-platform";
class l extends i {
  redeemCodeSuccess() {
    this.emit("redeemCodeSuccess", void 0);
  }
  redeemCodeError(t) {
    this.emit("redeemCodeError", t);
  }
}
class p extends n {
  constructor(t) {
    super(), this.event = new l(), this.props = t, this.state = {
      isRedeemingCode: !1,
      redeemCodeResult: null,
      error: null
    }, this.sdk = d.getAppState().getSdk();
  }
  async redeemCode(t) {
    try {
      if (this.state.isRedeemingCode)
        return;
      await this.setState({
        isRedeemingCode: !0,
        redeemCodeResult: null,
        error: null
      });
      const e = this.props.user;
      if (!e)
        return;
      const s = await a.FulfillmentApi(this.sdk).createFulfillmentCode_ByUserId(
        m.safeParse(e).success && e.userId || "",
        {
          code: t,
          language: o(),
          region: e.country
        }
      );
      this.setState({ redeemCodeResult: s }), await this.event.redeemCodeSuccess();
    } catch (e) {
      this.setState({
        error: e
      }), this.event.redeemCodeError(e);
    } finally {
      this.setState({ isRedeemingCode: !1 });
    }
  }
}
export {
  p as RedeemCodeLogic
};
