import { Container as g } from "unstated";
import { Platform as u, PlatformClient as I } from "@accelbyte/sdk-platform";
import { RegionDataItemHelper as l } from "./accelbyte-widgets388.mjs";
class b extends g {
  constructor(t) {
    super(), this.sdk = t, this.state = {
      isFetching: !1,
      platformItemsList: null,
      currencyMap: null,
      error: null
    };
  }
  getOffset(t) {
    return t ? (t - 1) * this.getLimit() : 0;
  }
  getLimit(t) {
    return t || 16;
  }
  async getItemsByCriteria({
    itemType: t,
    page: i,
    country: m,
    pageSize: c,
    plan: o,
    language: f
  }) {
    try {
      if (this.state.isFetching)
        return;
      await this.setState({
        isFetching: !0,
        platformItemsList: null,
        currencyMap: null,
        error: null
      });
      const s = u.ItemApi(this.sdk), r = await s.getItemsByCriteria({
        itemType: t,
        offset: this.getOffset(i),
        limit: this.getLimit(c),
        region: m,
        language: f,
        features: o || void 0
      }), n = r.data.filter((e) => e.flexible).map((e) => e.itemId);
      if (n.length) {
        const e = /* @__PURE__ */ new Map();
        (await s.getItemsEstimatedPrice({ itemIds: n.join() })).forEach((a) => {
          const { itemId: h, estimatedPrices: p } = a, d = l.getEstimatedPriceMap(p);
          e.set(h, d);
        }), r.data = r.data.map((a) => l.buildRegionDataWithEstimatedPrice(a, e.get(a.itemId)));
      }
      await this.setState({ platformItemsList: r });
    } catch (s) {
      this.setState({ error: s });
    } finally {
      this.setState({ isFetching: !1 });
    }
  }
  getCurrencyMap() {
    const { currencyMap: t } = this.state;
    t || I.getCurrencyMap(this.sdk).then((i) => {
      this.setState({ currencyMap: i });
    }).catch((i) => {
      console.error(i);
    });
  }
}
export {
  b as ItemListingAppManager
};
