import { O as B, f as C, I as k, C as G, B as O, A as S, L as N, a as v, b as T, D as x, c as M, d as F } from "./ItemDetailPurchaseAlert-3cdb1ea7.mjs";
import { T as l, D as K, p as q, q as R, ab as g, C as U, w as D, y as f, a as b, L as H, R as W, G as _, ag as $, K as j, F as z, H as Y, N as J } from "./index-162fd1aa.mjs";
import { w as Q } from "./WithPropsGuard-9dd19ecc.mjs";
import { I as V } from "./ItemHelper-37a12c2c.mjs";
import e from "react";
import { H as X } from "./Helmet-ed6d5573.mjs";
import { Link as I } from "react-router-dom";
import { P as Z } from "./PlatformRequirements-fc8525b3.mjs";
import { ItemDetailPageError as ee } from "./InGameItemDetailPage-2ff05429.mjs";
import { d as te, e as ae } from "./MockVars-1cf2eb0a.mjs";
import "./reactSlick-cb4a2d39.mjs";
import "./dateFns-2367ee5b.mjs";
import "./reactPlayer-2f69f369.mjs";
import "./default-item-image-9aab0e29.mjs";
import "./SuccessFlatIcon-54f5f907.mjs";
import "./WarningIcon-c5874dd2.mjs";
import "./PendingOrdersAppManager-4782fef4.mjs";
import "./PaymentStatusDialog-3afb3b20.mjs";
import "./Radio-b897ba55.mjs";
import "./validator-61879ab7.mjs";
import "./Tabs-8498653e.mjs";
import "./CommonError-0ae83068.mjs";
import "./starBgGradient-589e5185.mjs";
import "./CommonError-4ed993c7.mjs";
const re = ({ baseApp: i, onClose: t }) => /* @__PURE__ */ e.createElement(K, { isOpen: !0, className: "purchaseBaseAppDialog" }, /* @__PURE__ */ e.createElement(q, null, /* @__PURE__ */ e.createElement(l, { i18nKey: "PurchaseBaseApp.lockedDownloadableContent.content" }, "This expansion requires the base game ", { appTitle: i.title }, " to play")), /* @__PURE__ */ e.createElement(R, { className: "dialogFooter" }, /* @__PURE__ */ e.createElement("button", { onClick: t, className: "btn small btn-ghost btnCancel" }, /* @__PURE__ */ e.createElement(l, { i18nKey: "PurchaseBaseApp.cancel" }, "Cancel")), /* @__PURE__ */ e.createElement(
  I,
  {
    to: g.GameItem.makePath({
      itemId: i.itemId
    }),
    className: "btn small btn-primary btnGoToGamePage"
  },
  /* @__PURE__ */ e.createElement(l, { i18nKey: "PurchaseBaseApp.goToGamePage" }, "Go to the game page")
)));
class se extends e.Component {
  constructor(t) {
    super(t), this.state = {
      isDownloadBaseAppDialogOpen: !1
    };
  }
  render() {
    const { baseApp: t } = this.props, { isDownloadBaseAppDialogOpen: r } = this.state;
    return /* @__PURE__ */ e.createElement(e.Fragment, null, /* @__PURE__ */ e.createElement("button", { className: "btn big btn-primary btnGoToLibrary", onClick: () => this.setState({ isDownloadBaseAppDialogOpen: !0 }) }, /* @__PURE__ */ e.createElement(l, { i18nKey: "PurchaseBaseApp.goToLibrary" }, "GO TO LIBRARY")), r && /* @__PURE__ */ e.createElement(re, { baseApp: t, onClose: () => this.setState({ isDownloadBaseAppDialogOpen: !1 }) }));
  }
}
const ie = 31142;
class ne extends U {
  constructor(t) {
    super(), this.fetchBaseAppEntitlement = async (r) => {
      const { user: a } = this.props;
      if (!D.safeParse(a).success || !(a != null && a.userId))
        return null;
      const { error: s } = await this.sdk.Platform.Entitlement().getEntitlementByAppId({ userId: a.userId, appId: r });
      if (!s)
        return !0;
      if (f.extractServiceErrorCode(s) === ie)
        return !1;
      throw s;
    }, this.fetchAdditionalAppInfo = async () => await this.sdk.Platform.Item().getAppInfoByItemId({ itemId: this.props.itemId }).then((r) => {
      if (r.error)
        throw r.error;
      return r.response.data;
    }), this.props = t, this.state = {
      dlcItem: null,
      additionalAppInfo: null,
      baseApp: null,
      isBaseAppOwned: null,
      isFetching: !1,
      error: null,
      itemPublishingContent: null,
      itemPublishingContentError: null
    }, this.sdk = b.getAppState().getSdk();
  }
  async init() {
    try {
      if (this.state.isFetching)
        return;
      await this.setState({
        isFetching: !0,
        dlcItem: null,
        baseApp: null,
        error: null
      });
      const [t, r] = await Promise.all([this.fetchDlcItem(), this.fetchAdditionalAppInfo()]);
      if (!t.appId)
        throw new P("");
      await this.initPurchaseLogic(t);
      const { baseAppId: a } = t;
      if (!a)
        throw new E("");
      const [s, c] = await Promise.all([this.fetchBaseApp(a), this.fetchBaseAppEntitlement(a)]);
      await this.setState({
        dlcItem: t,
        baseApp: s,
        isBaseAppOwned: c,
        additionalAppInfo: r,
        itemPublishingContent: null
      });
    } catch (t) {
      console.error(t), this.setState({ error: t });
    } finally {
      await this.setState({ isFetching: !1 });
    }
  }
  async fetchDlcItem() {
    return await this.sdk.Platform.Item().getItemsByItemIdLocale({ itemId: this.props.itemId }).then((t) => {
      if (t.error)
        throw t.error;
      return t.response.data;
    });
  }
  async fetchBaseGameId(t) {
    return await this.sdk.BuildInfo.DLC().getBaseGamesByDlcAppId(t).then((r) => {
      if (r.error)
        throw r.error;
      const a = r.response.data[0];
      if (!a || !a.appId)
        throw new E("");
      return a.appId;
    });
  }
  async fetchBaseApp(t) {
    return await this.sdk.Platform.Item().getItemByAppId({ appId: t }).then((r) => {
      if (r.error)
        throw r.error;
      return r.response.data;
    });
  }
  async initPurchaseLogic(t) {
    const { user: r } = this.props, a = this.purchaseLogic = new B(this.sdk, {
      item: t,
      userId: D.safeParse(r).success ? r == null ? void 0 : r.userId : ""
    });
    a.subscribe(() => this.setState({})), await a.init();
    const { fetchError: s } = a.state;
    if (s)
      throw s;
  }
}
class P extends Error {
  constructor(t) {
    super(t), Object.setPrototypeOf(this, P.prototype);
  }
}
class E extends Error {
  constructor(t) {
    super(t), Object.setPrototypeOf(this, E.prototype);
  }
}
const oe = () => /* @__PURE__ */ e.createElement(H, { className: W.tall }, /* @__PURE__ */ e.createElement(l, { i18nKey: "InitialUserFetchPage.notice" }));
class le extends e.Component {
  constructor(t) {
    super(t), this.openLightbox = (o) => {
      this.setState({ isLightboxOpen: !0, selectedImagePosition: o });
    }, this.closeLightbox = () => {
      this.setState({ isLightboxOpen: !1 });
    }, this.renderLauncherGoToLibraryButton = () => {
      const { dlcItem: o, baseApp: m, isBaseAppOwned: p } = this.logic.state;
      return o ? !m || p ? /* @__PURE__ */ e.createElement(C, { item: m || o }) : /* @__PURE__ */ e.createElement(se, { baseApp: m }) : "";
    };
    const { itemId: r, country: a, language: s, user: c } = t;
    this.logic = new ne({ itemId: r, country: a, language: s, user: c }), this.logic.subscribe(() => this.setState({})), this.state = {
      isLightboxOpen: !1,
      selectedImagePosition: 0
    };
  }
  componentDidMount() {
    this.props.isPreviewMode || this.logic.init();
  }
  render() {
    const t = b.getAppState().isSingleGame(), { user: r, isPreviewMode: a } = this.props, { isFetching: s, dlcItem: c, error: o, baseApp: m, additionalAppInfo: p, itemPublishingContent: A } = this.logic.state, n = a ? te.data[2] : c, h = a ? ae : m;
    o && f.extractServiceErrorCode(o) === _.ITEM_NOT_FOUND && $({ eventCategory: j.Error404.makePath(), eventLabel: f.errorToString(o) });
    const { purchaseLogic: d } = this.logic, w = d && d.state.currencyMap, { isLightboxOpen: y, selectedImagePosition: L } = this.state, u = V.getMedia(n, A);
    return /* @__PURE__ */ e.createElement("main", { className: "itemDetailPage" }, /* @__PURE__ */ e.createElement(
      k,
      {
        purchaseLogic: d,
        user: r,
        isFetching: s,
        isPreviewMode: !!a
      }
    ), /* @__PURE__ */ e.createElement(X, null, n && /* @__PURE__ */ e.createElement("title", null, z.makeTitleWithAppName(n.title))), s && /* @__PURE__ */ e.createElement(oe, null), o && /* @__PURE__ */ e.createElement(ee, { error: o }), n && h && /* @__PURE__ */ e.createElement(e.Fragment, null, /* @__PURE__ */ e.createElement(G, { item: n, itemPublishingContent: A }, /* @__PURE__ */ e.createElement(O, null, /* @__PURE__ */ e.createElement(I, { to: g.GameList.makePath() }, /* @__PURE__ */ e.createElement(l, { i18nKey: "Navigation.links.games" }, "Games")), /* @__PURE__ */ e.createElement(
      I,
      {
        to: g.GameItem.makePath({
          itemId: h.itemId
        })
      },
      h.title
    ), n.title)), u && /* @__PURE__ */ e.createElement(S, { itemMedias: u, openLightbox: this.openLightbox }), y && u && /* @__PURE__ */ e.createElement(
      N,
      {
        itemMedias: u,
        isOpen: y,
        onClose: this.closeLightbox,
        selectedPosition: L
      }
    ), /* @__PURE__ */ e.createElement("div", { className: "itemDetailContent" }, /* @__PURE__ */ e.createElement("div", { className: "description" }, /* @__PURE__ */ e.createElement(v, { item: n }), p && /* @__PURE__ */ e.createElement(Z, { className: "requirementsDesktop", additionalAppInfo: p })), /* @__PURE__ */ e.createElement("div", { className: "detailWidget" }, /* @__PURE__ */ e.createElement(T, { item: n }, w && /* @__PURE__ */ e.createElement(x, { item: n, currencyMap: w, position: "right", showDetail: !0 })), /* @__PURE__ */ e.createElement("div", { className: "protruded" }, /* @__PURE__ */ e.createElement(
      M,
      {
        item: n,
        purchaseLogic: d,
        additionalAppInfo: p,
        renderLauncherGoToLibraryButton: this.renderLauncherGoToLibraryButton,
        isPreviewMode: !!a,
        isCheckoutPreview: a == null ? void 0 : a.isCheckoutModalPreview
      }
    )), /* @__PURE__ */ e.createElement("div", { className: "requiredApp" }, /* @__PURE__ */ e.createElement(l, { i18nKey: "InGameItemDetailPage.requiredAppNotice" }, "Required Game"), /* @__PURE__ */ e.createElement(
      I,
      {
        to: Y.isDesktopApp() && t ? J.Game.link : g.GameItem.makePath({
          itemId: h.itemId
        })
      },
      h.title
    )))), /* @__PURE__ */ e.createElement("div", { className: "storeContainer" }, /* @__PURE__ */ e.createElement("h2", null, /* @__PURE__ */ e.createElement(l, { i18nKey: "InGameItemDetailPage.inAppStore.heading" }, "Related Items")), /* @__PURE__ */ e.createElement(
      F,
      {
        baseItem: n,
        language: this.props.language,
        noItemMessage: /* @__PURE__ */ e.createElement("div", { className: "noItem" }, /* @__PURE__ */ e.createElement(l, { i18nKey: "InGameItemDetailPage.inAppStore.noItem" }, "Looks like there are no related items yet.")),
        isPreviewMode: !!a
      }
    ))));
  }
}
const ve = Q(
  ({ itemId: i, language: t, isPreviewMode: r }) => {
    const a = b.getAppState().getUser() || void 0, s = a && a.country || void 0;
    return /* @__PURE__ */ e.createElement(le, { itemId: i, country: s, user: a, language: t, isPreviewMode: r });
  },
  (i, t) => i.itemId !== t.itemId || t.language !== i.language || i.isPreviewMode !== t.isPreviewMode
);
export {
  le as DlcDetailPage,
  oe as ItemDetailPageLoadingScreen,
  ve as default
};
