import { Ctx as f } from "./accelbyte-widgets33.mjs";
import { ItemType as I } from "./accelbyte-widgets185.mjs";
import { Container as y } from "unstated";
import { Platform as g, PlatformClient as d } from "@accelbyte/sdk-platform";
const i = class extends y {
  constructor(t) {
    super(), this.props = t, this.state = {
      isFetching: !1,
      items: null,
      error: null,
      currencyMap: null
    }, this.sdk = f.getAppState().getSdk();
  }
  async init() {
    try {
      if (this.state.isFetching)
        return;
      await this.setState({
        isFetching: !0,
        error: null,
        items: null,
        currencyMap: null
      });
      const t = await this.fetchItems(), e = await this.fetchCurrencyMap();
      this.setState({ items: t, currencyMap: e });
    } catch (t) {
      console.error(t), this.setState({ error: t });
    } finally {
      await this.setState({ isFetching: !1 });
    }
  }
  async fetchItems() {
    const { baseItem: t, itemType: e, appType: p, country: l, language: h } = this.props, a = t.baseAppId || t.appId;
    if (!a && e !== I.enum.OPTIONBOX)
      return [];
    let n = !1, c = 0, s = [];
    do {
      const m = await g.ItemApi(this.sdk).getItemsByCriteria({
        baseAppId: a,
        itemType: e,
        appType: p,
        region: l,
        language: h,
        offset: c,
        limit: i.ITEMS_PER_FETCH
      }).then((r) => {
        var o;
        return n = !!((o = r.paging) != null && o.next), c += i.ITEMS_PER_FETCH, r.data.filter((u) => u.itemId !== t.itemId);
      }).catch((r) => {
        throw r;
      });
      s = s.concat(m);
    } while (n);
    return s;
  }
  async fetchCurrencyMap() {
    return d.getCurrencyMap(this.sdk);
  }
};
let S = i;
S.ITEMS_PER_FETCH = 100;
export {
  S as InAppStoreLogic
};
