import { Ctx as c } from "./accelbyte-widgets33.mjs";
import { AppType as h } from "./accelbyte-widgets185.mjs";
import { Container as p } from "unstated";
import { Platform as m } from "@accelbyte/sdk-platform";
class w extends p {
  constructor(t) {
    super(), this.props = t, this.state = {
      isFetching: !1,
      gameList: [],
      gameItems: null,
      error: null,
      codeItems: null
    }, this.sdk = c.getAppState().getSdk();
  }
  async init() {
    await this.fetchGames();
  }
  async fetchGames() {
    try {
      if (this.state.isFetching || (await this.setState({
        isFetching: !0,
        error: null
      }), !this.props.userId))
        return;
      const { userId: t, limit: i } = this.props, e = (await m.EntitlementApi(this.sdk).getEntitlements_ByUserId(t, {
        appType: h.enum.GAME,
        offset: 0,
        limit: i
      })).data, a = /* @__PURE__ */ new Map();
      if (!e)
        return;
      await Promise.all(
        e.map(async (o) => {
          const { itemId: s, itemSnapshot: r } = o;
          if (s) {
            const n = await this.fetchItem(s) || r && r;
            n && a.set(s, n);
          }
        })
      ), this.setState({
        gameList: e,
        gameItems: a
      });
    } catch (t) {
      this.setState({ error: t });
    } finally {
      this.setState({ isFetching: !1 });
    }
  }
  async fetchItem(t) {
    try {
      return await m.ItemApi(this.sdk).getLocale_ByItemId(t);
    } catch {
      return null;
    }
  }
}
export {
  w as PurchasedItemsListLogic
};
