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