import { O as L, I as B, C as P, B as C, A as v, L as N, a as D, b as A, D as S, c as w, d as k } from "./ItemDetailPurchaseAlert-3cdb1ea7.mjs";
import { w as x } from "./WithPropsGuard-9dd19ecc.mjs";
import { a as O, I as T } from "./ItemHelper-37a12c2c.mjs";
import { C as _, a as d, w as F, T as o, an as G, ab as p, af as H, y, G as M, ag as U, K, F as R, H as q } from "./index-162fd1aa.mjs";
import e from "react";
import { a as W } from "./Helmet-ed6d5573.mjs";
import { Link as m } from "react-router-dom";
import { ItemDetailPageLoadingScreen as Q, ItemDetailPageError as $ } from "./InGameItemDetailPage-2ff05429.mjs";
import { d as f } from "./default-item-image-9aab0e29.mjs";
import { c as z } from "./reactSlick-cb4a2d39.mjs";
import "./reactPlayer-2f69f369.mjs";
import "./dateFns-2367ee5b.mjs";
import "./SuccessFlatIcon-54f5f907.mjs";
import "./WarningIcon-c5874dd2.mjs";
import "./PendingOrdersAppManager-4782fef4.mjs";
import "./PaymentStatusDialog-3afb3b20.mjs";
import "./Radio-b897ba55.mjs";
import "./MockVars-1cf2eb0a.mjs";
import "./validator-61879ab7.mjs";
import "./CommonError-0ae83068.mjs";
import "./starBgGradient-589e5185.mjs";
import "./CommonError-4ed993c7.mjs";
class j extends _ {
  constructor(t) {
    super(), this.props = t, this.state = {
      bundleItem: null,
      itemsInBundle: [],
      baseApp: null,
      isFetching: !1,
      error: null,
      itemPublishingContent: null,
      itemPublishingContentError: null
    }, this.sdk = d.getAppState().getSdk();
  }
  async init() {
    try {
      if (this.state.isFetching)
        return;
      this.setState({
        isFetching: !0,
        bundleItem: null,
        itemsInBundle: [],
        error: null
      });
      const t = await this.fetchBundleItem();
      await this.initPurchaseLogic(t);
      const s = await this.fetchItemsInBundle(t.itemIds || []);
      let i = null;
      const { baseAppId: a } = t;
      a && (i = s.find((r) => a === r.appId) || await this.fetchBaseApp(a)), this.setState({
        bundleItem: t,
        itemsInBundle: s,
        baseApp: i,
        itemPublishingContent: null
      });
    } catch (t) {
      console.error(t), this.setState({ error: t });
    } finally {
      this.setState({ isFetching: !1 });
    }
  }
  async fetchBundleItem() {
    const { itemId: t, country: s, language: i } = this.props;
    return await this.sdk.Platform.Item().getItemsByItemIdLocale({ itemId: t, queryParams: { language: i, region: s } }).then((a) => {
      if (a.error)
        throw a.error;
      return a.response.data;
    });
  }
  async fetchItemsInBundle(t) {
    if (t.length === 0)
      return [];
    const { language: s, country: i } = this.props;
    return await this.sdk.Platform.Item().getItemsByItemIds({
      queryParams: { language: s, region: i, itemIds: t.toString() }
    }).then((a) => {
      if (a.error)
        throw a.error;
      return a.response.data;
    });
  }
  async fetchBaseApp(t) {
    return await this.sdk.Platform.Item().getItemByAppId({
      appId: t,
      region: this.props.country,
      language: this.props.language
    }).then((s) => {
      if (s.error)
        throw s.error;
      return s.response.data;
    });
  }
  async initPurchaseLogic(t) {
    const { user: s } = this.props, i = this.purchaseLogic = new L(this.sdk, {
      item: t,
      userId: F.safeParse(s).success && (s == null ? void 0 : s.userId) || null
    });
    i.subscribe(() => this.setState({})), await i.init();
    const { fetchError: a } = i.state;
    if (a)
      throw a;
  }
}
const J = "catalogBundleItemList _catalogBundleItemList_f0de3", V = {
  catalogBundleItemList: J
}, X = ({ link: n, thumbnail: t, name: s, type: i, quantity: a }) => /* @__PURE__ */ e.createElement(m, { to: n, className: "catalogItem" }, /* @__PURE__ */ e.createElement("div", { className: "catalogItemImage" }, /* @__PURE__ */ e.createElement("img", { src: t || f, loading: "lazy" })), /* @__PURE__ */ e.createElement("div", { className: "catalogItemDetail" }, /* @__PURE__ */ e.createElement("div", { className: "catalogItemHeading" }, s), /* @__PURE__ */ e.createElement("div", { className: "catalogItemType" }, i), /* @__PURE__ */ e.createElement("div", { className: "catalogItemQty" }, a > 1 && `x${a}`)));
class Y extends e.Component {
  constructor(t) {
    super(t), this.state = {
      showAll: !1
    };
  }
  render() {
    const { items: t, bundleItem: s } = this.props;
    return /* @__PURE__ */ e.createElement("div", { className: z(V.catalogBundleItemList, "catalogBundleItemList") }, /* @__PURE__ */ e.createElement("h2", { className: "catalogHeading" }, /* @__PURE__ */ e.createElement(o, { i18nKey: "CatalogBundleItemList.heading" }, "Inside the Bundle")), t.length > 0 && /* @__PURE__ */ e.createElement("div", { className: "catalogList" }, t.map((i) => {
      const a = i.appType || i.itemType, r = G.getFilteredImageUrlByTag(i.images, O.productCover) || f;
      return /* @__PURE__ */ e.createElement(
        X,
        {
          key: i.itemId,
          link: p.getItemDetailRoute(i),
          thumbnail: r,
          name: i.title || i.name || "",
          type: a,
          quantity: s.itemQty ? s.itemQty[i.itemId] : 1
        }
      );
    })), t.length === 0 && /* @__PURE__ */ e.createElement("div", { className: "catalogNoItem" }, /* @__PURE__ */ e.createElement(o, { i18nKey: "CatalogBundleItemList.noItem" }, "Looks like there aren't any bundles for this game yet.")));
  }
}
class Z extends e.Component {
  constructor(t) {
    super(t), this.openLightbox = (l) => {
      this.setState({ isLightboxOpen: !0, selectedImagePosition: l });
    }, this.closeLightbox = () => {
      this.setState({ isLightboxOpen: !1 });
    };
    const { itemId: s, country: i, language: a, user: r } = this.props;
    this.logic = new j({ itemId: s, country: i, language: a, user: r }), this.logic.subscribe(() => this.setState({})), this.state = {
      isLightboxOpen: !1,
      selectedImagePosition: 0
    };
  }
  componentDidMount() {
    this.logic.init();
  }
  hasPendingOrder() {
    var t, s, i;
    return !!((i = (s = (t = this.logic.purchaseLogic) == null ? void 0 : t.state) == null ? void 0 : s.pendingOrders) != null && i[0]);
  }
  isItemUnavailable() {
    const { purchaseLogic: t } = this.logic;
    return !!(t && t.state.purchaseConstraint && H.purchaseUnavailable(t.state.purchaseConstraint));
  }
  render() {
    const t = d.getAppState().isSingleGame(), { user: s } = this.props, { isFetching: i, bundleItem: a, error: r, baseApp: l, itemsInBundle: u, itemPublishingContent: h } = this.logic.state;
    r && y.extractServiceErrorCode(r) === M.ITEM_NOT_FOUND && U({ eventCategory: K.Error404.makePath(), eventLabel: y.errorToString(r) });
    const { purchaseLogic: c } = this.logic, I = c && c.state.currencyMap, { isLightboxOpen: E, selectedImagePosition: b } = this.state, g = T.getMedia(a, h);
    return /* @__PURE__ */ e.createElement("main", { className: "itemDetailPage" }, /* @__PURE__ */ e.createElement(
      B,
      {
        purchaseLogic: c,
        user: s,
        isFetching: i
      }
    ), /* @__PURE__ */ e.createElement(W, null, a && /* @__PURE__ */ e.createElement("title", null, R.makeTitleWithAppName(a.title))), i && /* @__PURE__ */ e.createElement(Q, null), r && /* @__PURE__ */ e.createElement($, { error: r }), a && /* @__PURE__ */ e.createElement(e.Fragment, null, /* @__PURE__ */ e.createElement(P, { item: a, itemPublishingContent: h }, /* @__PURE__ */ e.createElement(C, null, /* @__PURE__ */ e.createElement(m, { to: p.GameList.makePath() }, /* @__PURE__ */ e.createElement(o, { i18nKey: "Navigation.links.games" }, "Games")), l && /* @__PURE__ */ e.createElement(m, { to: p.getItemDetailRoute(l) }, l.title), a.title)), g && /* @__PURE__ */ e.createElement(v, { itemMedias: g, openLightbox: this.openLightbox }), E && g && /* @__PURE__ */ e.createElement(
      N,
      {
        itemMedias: g,
        isOpen: E,
        onClose: this.closeLightbox,
        selectedPosition: b
      }
    ), /* @__PURE__ */ e.createElement("div", { className: "itemDetailContent" }, /* @__PURE__ */ e.createElement("div", { className: "description" }, /* @__PURE__ */ e.createElement(Y, { items: u, bundleItem: a }), /* @__PURE__ */ e.createElement(D, { item: a })), /* @__PURE__ */ e.createElement("div", { className: "detailWidget" }, /* @__PURE__ */ e.createElement(A, { item: a }, I && /* @__PURE__ */ e.createElement(S, { item: a, currencyMap: I, position: "right", showDetail: !0 })), /* @__PURE__ */ e.createElement("div", { className: "protruded" }, /* @__PURE__ */ e.createElement(w, { item: a, purchaseLogic: c, itemsInBundle: u })), l && /* @__PURE__ */ e.createElement("div", { className: "requiredApp" }, /* @__PURE__ */ e.createElement(o, { i18nKey: "BundleItemDetailPage.requiredAppNotice" }, "Required Game"), q.isDesktopApp() && t ? /* @__PURE__ */ e.createElement(m, { to: "/game" }, l.title) : /* @__PURE__ */ e.createElement(m, { to: p.getItemDetailRoute(l) }, l.title)))), /* @__PURE__ */ e.createElement("div", { className: "storeContainer" }, /* @__PURE__ */ e.createElement("h2", null, /* @__PURE__ */ e.createElement(o, { i18nKey: "BundleItemDetailPage.inAppStore.heading" }, "Related Items")), /* @__PURE__ */ e.createElement(
      k,
      {
        baseItem: a,
        language: this.props.language,
        noItemMessage: /* @__PURE__ */ e.createElement("div", { className: "noItem" }, /* @__PURE__ */ e.createElement(o, { i18nKey: "BundleItemDetailPage.inAppStore.noItem" }, "Looks like there are no related items yet."))
      }
    ))));
  }
}
const Be = x(
  ({ itemId: n, language: t }) => {
    const s = d.getAppState().getUser() || void 0, i = s && s.country || void 0;
    return /* @__PURE__ */ e.createElement(Z, { itemId: n, country: i, user: s, language: t });
  },
  (n, t) => n.itemId !== t.itemId || t.language !== n.language
);
export {
  Z as BundleItemDetailPage,
  Be as default
};
