import { Ctx as c } from "./accelbyte-widgets33.mjs";
import { ErrorHelper as l } from "./accelbyte-widgets120.mjs";
import { OrderAppManager as h } from "./accelbyte-widgets403.mjs";
import { Container as d } from "unstated";
import { EligibleUser as n } from "@accelbyte/sdk-iam";
import { Platform as r } from "@accelbyte/sdk-platform";
import { Buildinfo as u } from "@accelbyte/sdk-buildinfo";
const m = 31142;
class b extends d {
  constructor(t) {
    super(), this.fetchBaseAppEntitlement = async (s) => {
      const { user: e } = this.props;
      if (!n.safeParse(e).success || !(e != null && e.userId))
        return null;
      try {
        return await r.EntitlementApi(this.sdk).getEntitlementsByAppId_ByUserId(e.userId, { appId: s }), !0;
      } catch (i) {
        if (l.extractServiceErrorCode(i) === m)
          return !1;
        throw i;
      }
    }, this.fetchAdditionalAppInfo = async () => await r.ItemApi(this.sdk).getAppLocale_ByItemId(this.props.itemId), this.props = t, this.state = {
      dlcItem: null,
      additionalAppInfo: null,
      baseApp: null,
      isBaseAppOwned: null,
      isFetching: !1,
      error: null,
      itemPublishingContent: null,
      itemPublishingContentError: null
    }, this.sdk = c.getAppState().getSdk();
  }
  async init() {
    try {
      if (this.state.isFetching)
        return;
      await this.setState({
        isFetching: !0,
        dlcItem: null,
        baseApp: null,
        error: null
      });
      const [t, s] = await Promise.all([this.fetchDlcItem(), this.fetchAdditionalAppInfo()]);
      if (!t.appId)
        throw new p("");
      await this.initPurchaseLogic(t);
      const { baseAppId: e } = t;
      if (!e)
        throw new a("");
      const [i, o] = await Promise.all([this.fetchBaseApp(e), this.fetchBaseAppEntitlement(e)]);
      await this.setState({
        dlcItem: t,
        baseApp: i,
        isBaseAppOwned: o,
        additionalAppInfo: s,
        itemPublishingContent: null
      });
    } catch (t) {
      console.error(t), this.setState({ error: t });
    } finally {
      await this.setState({ isFetching: !1 });
    }
  }
  async fetchDlcItem() {
    return await r.ItemApi(this.sdk).getLocale_ByItemId(this.props.itemId);
  }
  async fetchBaseGameId(t) {
    const e = (await u.DlcApi(this.sdk).getAppLatestByDlcAppId_ByDlcAppId(t))[0];
    if (!e || !e.appId)
      throw new a("");
    return e.appId;
  }
  async fetchBaseApp(t) {
    return await r.ItemApi(this.sdk).getItemsByAppId({ appId: t });
  }
  async initPurchaseLogic(t) {
    const { user: s } = this.props, e = this.purchaseLogic = new h(this.sdk, {
      item: t,
      userId: n.safeParse(s).success ? s == null ? void 0 : s.userId : ""
    });
    e.subscribe(() => this.setState({})), await e.init();
    const { fetchError: i } = e.state;
    if (i)
      throw i;
  }
}
class p extends Error {
  constructor(t) {
    super(t), Object.setPrototypeOf(this, p.prototype);
  }
}
class a extends Error {
  constructor(t) {
    super(t), Object.setPrototypeOf(this, a.prototype);
  }
}
export {
  b as DlcDetailPageLogic,
  p as NoAppIdError,
  a as NoBaseAppIdError
};
