import { Ctx as n } from "./accelbyte-widgets33.mjs";
import { OrderAppManager as o } from "./accelbyte-widgets403.mjs";
import { Container as p } from "unstated";
import { EligibleUser as c } from "@accelbyte/sdk-iam";
import { Platform as a } from "@accelbyte/sdk-platform";
class f extends p {
  constructor(t) {
    super(), this.props = t, this.state = {
      inGameItem: null,
      baseApp: null,
      isFetching: !1,
      error: null,
      itemPublishingContent: null,
      itemPublishingContentError: null
    }, this.sdk = n.getAppState().getSdk();
  }
  async init() {
    try {
      if (this.state.isFetching)
        return;
      await this.setState({
        isFetching: !0,
        inGameItem: null,
        baseApp: null,
        error: null
      });
      const t = await this.fetchInGameItem();
      if (!t.baseAppId)
        throw new i("");
      await this.initPurchaseLogic(t);
      const e = await this.fetchBaseApp(t.baseAppId);
      await this.setState({
        inGameItem: t,
        baseApp: e,
        itemPublishingContent: null
      });
    } catch (t) {
      console.error(t), this.setState({ error: t });
    } finally {
      await this.setState({ isFetching: !1 });
    }
  }
  async fetchInGameItem() {
    const { itemId: t, country: e, language: s } = this.props;
    return await a.ItemApi(this.sdk).getLocale_ByItemId(t, { language: s, region: e });
  }
  async fetchBaseApp(t) {
    return await a.ItemApi(this.sdk).getItemsByAppId({
      appId: t,
      region: this.props.country,
      language: this.props.language
    });
  }
  async initPurchaseLogic(t) {
    const { user: e } = this.props, s = this.purchaseLogic = new o(this.sdk, {
      item: t,
      userId: c.safeParse(e).success ? e == null ? void 0 : e.userId : ""
    });
    s.subscribe(() => this.setState({})), await s.init();
    const { fetchError: r } = s.state;
    if (r)
      throw r;
  }
}
class i extends Error {
  constructor(t) {
    super(t), Object.setPrototypeOf(this, i.prototype);
  }
}
export {
  f as InGameItemDetailPageLogic,
  i as NoBaseAppIdError
};
