import { Ctx as m } from "./accelbyte-widgets33.mjs";
import { OrderAppManager as u } from "./accelbyte-widgets403.mjs";
import { Container as g } from "unstated";
import { EligibleUser as d } from "@accelbyte/sdk-iam";
import { Platform as p } from "@accelbyte/sdk-platform";
class b extends g {
  constructor(t) {
    super(), this.props = t, this.state = {
      isFetching: !1,
      appItem: null,
      additionalAppInfo: null,
      itemPublishingContent: null,
      error: null,
      itemPublishingContentError: null
    }, this.sdk = m.getAppState().getSdk();
  }
  async init() {
    try {
      if (this.state.isFetching)
        return;
      await this.setState({
        isFetching: !0,
        error: null,
        appItem: null,
        additionalAppInfo: null
      });
      const { itemId: t, country: h, language: c, user: e } = this.props, [
        s,
        r,
        { response: n, error: a }
      ] = await Promise.all([
        p.ItemApi(this.sdk).getLocale_ByItemId(t, { region: h, language: c }),
        p.ItemApi(this.sdk).getAppLocale_ByItemId(t),
        { response: null, error: null }
      ]);
      if (r && s) {
        const o = s, i = this.purchaseLogic = new u(this.sdk, {
          item: o,
          userId: d.safeParse(e).success ? e == null ? void 0 : e.userId : null
        });
        i.subscribe(() => this.setState({})), await i.init();
        const { fetchError: l } = i.state;
        if (l)
          throw l;
        await this.setState({
          appItem: o,
          additionalAppInfo: r
        });
      }
      a ? await this.setState({
        itemPublishingContentError: a
      }) : n && await this.setState({
        itemPublishingContent: n.data
      });
    } catch (t) {
      console.error(t), this.setState({ error: t });
    } finally {
      await this.setState({ isFetching: !1 });
    }
  }
}
export {
  b as AppDetailPageLogic
};
