import { ItemType as y, CurrencyType as p } from "./accelbyte-widgets185.mjs";
import { PurchaseHelper as m } from "./accelbyte-widgets211.mjs";
import { RegionDataItemHelper as g } from "./accelbyte-widgets388.mjs";
import { ItemSortType as d } from "./accelbyte-widgets467.mjs";
import { Platform as c, PlatformClient as S } from "@accelbyte/sdk-platform";
import { Container as C } from "unstated";
const h = {
  isFetching: !1,
  wallet: null,
  error: null,
  currencyMap: null,
  coins: null
}, u = 50;
class k extends C {
  constructor(r) {
    super(), this.sdk = r, this.purchaseCoin = async (e, t, i, a) => {
      try {
        const { isFetching: n } = this.state;
        if (n || !e)
          return;
        await this.setState({ isFetching: !0 }), await c.OrderApi(this.sdk).createOrder_ByUserId(e.userId, {
          itemId: t.itemId,
          quantity: 1,
          price: i.price || 0,
          discountedPrice: g.getFinalPrice(i),
          currencyCode: i.currencyCode,
          returnUrl: a
        });
      } catch (n) {
        this.setState({ error: n });
      } finally {
        this.setState({ isFetching: !1 });
      }
    }, this.state = h;
  }
  async fetchWallet(r, e) {
    if (!e || this.state.isFetching)
      return null;
    try {
      this.setState({
        isFetching: !0,
        error: null
      });
      const t = await this.fetchUserWallet(r, e);
      return this.setState({ wallet: t }), t;
    } catch (t) {
      throw this.setState({ error: t }), t;
    } finally {
      this.setState({ isFetching: !1 });
    }
  }
  async fetchUserWallet(r, e) {
    try {
      return await c.WalletApi(this.sdk).getWallet_ByUserId_ByCurrencyCode(r, e);
    } catch (t) {
      if (t.response && t.response.status === 404)
        return null;
      throw t;
    }
  }
  async fetchCoinList(r, e) {
    if (!r)
      return null;
    let t = [], i = !0, a = 0;
    for (; i; ) {
      let n;
      try {
        const s = await c.ItemApi(this.sdk).getItemsByCriteria({
          itemType: y.enum.COINS,
          region: r.country,
          limit: u,
          sortBy: [d.DISPLAY_ORDER_ASC],
          offset: a
        });
        i = Boolean(s.paging && s.paging.next), n = s.data;
      } catch (s) {
        throw i = !1, s;
      }
      const f = n.filter((s) => {
        const { targetCurrencyCode: o } = s;
        if (!o)
          return !1;
        const l = e.get(o);
        return l && l.currencyType === p.enum.VIRTUAL && !!s.useCount && m.makePaymentOptionList(s.regionData || [], e).length > 0;
      });
      t = t.concat(f), a += u;
    }
    return t;
  }
  async fetchPricingAndListOfCoins(r) {
    try {
      await this.setState({
        isFetching: !0,
        currencyMap: null,
        coins: null,
        error: null
      });
      const e = await S.getCurrencyMap(this.sdk), t = await this.fetchCoinList(r, e);
      this.setState({ currencyMap: e, coins: t });
    } catch (e) {
      this.setState({ error: e });
    } finally {
      this.setState({ isFetching: !1 });
    }
  }
  clear() {
    this.setState(h);
  }
}
export {
  k as WalletAppManager
};
