import { Ctx as a } from "./accelbyte-widgets33.mjs";
import { OrderAppManager as c } from "./accelbyte-widgets403.mjs";
import { Container as h } from "unstated";
import { EligibleUser as l } from "@accelbyte/sdk-iam";
import { Platform as n } from "@accelbyte/sdk-platform";
class b extends h {
  constructor(e) {
    super(), this.props = e, this.state = {
      optionboxItem: null,
      itemsInOptionbox: [],
      isFetching: !1,
      error: null,
      itemPublishingContent: null,
      itemPublishingContentError: null
    }, this.sdk = a.getAppState().getSdk();
  }
  async init() {
    var e, i;
    try {
      if (this.state.isFetching)
        return;
      this.setState({
        isFetching: !0,
        optionboxItem: null,
        itemsInOptionbox: [],
        error: null
      });
      const t = await this.fetchOptionboxItem();
      await this.initPurchaseLogic(t);
      const s = (i = (e = t.optionBoxConfig) == null ? void 0 : e.boxItems) == null ? void 0 : i.filter(({ itemId: o }) => !!o).map(({ itemId: o }) => o), r = await this.fetchItemsInOptionbox(s || []);
      this.setState({
        optionboxItem: t,
        itemsInOptionbox: r,
        itemPublishingContent: null
      });
    } catch (t) {
      console.error(t), this.setState({ error: t });
    } finally {
      this.setState({ isFetching: !1 });
    }
  }
  fetchOptionboxItem() {
    const { itemId: e, country: i, language: t } = this.props;
    return n.ItemApi(this.sdk).getLocale_ByItemId(e, { language: t, region: i });
  }
  async fetchItemsInOptionbox(e) {
    if (e.length === 0)
      return [];
    const { language: i, country: t } = this.props;
    return await n.ItemApi(this.sdk).getItemsLocaleByIds({
      language: i,
      region: t,
      itemIds: e.toString()
    });
  }
  async initPurchaseLogic(e) {
    const { user: i } = this.props, t = this.purchaseLogic = new c(this.sdk, {
      item: e,
      userId: l.safeParse(i).success && (i == null ? void 0 : i.userId) || null
    });
    t.subscribe(() => this.setState({})), await t.init();
    const { fetchError: s } = t.state;
    if (s)
      throw s;
  }
}
export {
  b as OptionboxItemDetailPageLogic
};
