import { O as y, I as L, C as A, B as G, A as C, L as w, a as S, b as D, D as k, c as v, d as N } from "./ItemDetailPurchaseAlert-3cdb1ea7.mjs";
import { C as x } from "./CommonError-0ae83068.mjs";
import { C as O, a as u, w as F, L as T, R as K, T as m, y as h, G as b, ag as M, K as B, F as U, ab as p, H as _, N as H } from "./index-162fd1aa.mjs";
import { w as R } from "./WithPropsGuard-9dd19ecc.mjs";
import { I as W } from "./ItemHelper-37a12c2c.mjs";
import e from "react";
import { H as q } from "./Helmet-ed6d5573.mjs";
import { Link as g } from "react-router-dom";
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 "./MockVars-1cf2eb0a.mjs";
import "./starBgGradient-589e5185.mjs";
import "./CommonError-4ed993c7.mjs";
import "./validator-61879ab7.mjs";
class V extends O {
  constructor(t) {
    super(), this.props = t, this.state = {
      inGameItem: null,
      baseApp: null,
      isFetching: !1,
      error: null,
      itemPublishingContent: null,
      itemPublishingContentError: null
    }, this.sdk = u.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 d("");
      await this.initPurchaseLogic(t);
      const a = await this.fetchBaseApp(t.baseAppId);
      await this.setState({
        inGameItem: t,
        baseApp: a,
        itemPublishingContent: null
      });
    } catch (t) {
      console.error(t), this.setState({ error: t });
    } finally {
      await this.setState({ isFetching: !1 });
    }
  }
  async fetchInGameItem() {
    const { itemId: t, country: a, language: r } = this.props;
    return await this.sdk.Platform.Item().getItemsByItemIdLocale({ itemId: t, queryParams: { language: r, region: a } }).then((i) => {
      if (i.error)
        throw i.error;
      return i.response.data;
    });
  }
  async fetchBaseApp(t) {
    return await this.sdk.Platform.Item().getItemByAppId({
      appId: t,
      region: this.props.country,
      language: this.props.language
    }).then((a) => {
      if (a.error)
        throw a.error;
      return a.response.data;
    });
  }
  async initPurchaseLogic(t) {
    const { user: a } = this.props, r = this.purchaseLogic = new y(this.sdk, {
      item: t,
      userId: F.safeParse(a).success ? a == null ? void 0 : a.userId : ""
    });
    r.subscribe(() => this.setState({})), await r.init();
    const { fetchError: i } = r.state;
    if (i)
      throw i;
  }
}
class d extends Error {
  constructor(t) {
    super(t), Object.setPrototypeOf(this, d.prototype);
  }
}
const $ = () => /* @__PURE__ */ e.createElement(T, { className: K.tall }, /* @__PURE__ */ e.createElement(m, { i18nKey: "InitialUserFetchPage.notice" })), j = ({ error: s }) => {
  const t = () => s && h.extractServiceErrorCode(s) === b.PRODUCT_KEY_NOT_AVAILABLE ? /* @__PURE__ */ e.createElement(m, { i18nKey: "GameDetailViewPage.item.productKeyNotAvailable" }, "This product key is no longer available") : /* @__PURE__ */ e.createElement(x, { error: s });
  return /* @__PURE__ */ e.createElement("div", { className: "error" }, t());
};
class z extends e.Component {
  constructor(t) {
    super(t), this.openLightbox = (o) => {
      this.setState({ isLightboxOpen: !0, selectedImagePosition: o });
    }, this.closeLightbox = () => {
      this.setState({ isLightboxOpen: !1 });
    };
    const { itemId: a, country: r, language: i, user: n } = t;
    this.logic = new V({ itemId: a, country: r, language: i, user: n }), this.logic.subscribe(() => this.setState({})), this.state = {
      isLightboxOpen: !1,
      selectedImagePosition: 0
    };
  }
  componentDidMount() {
    this.logic.init();
  }
  render() {
    const t = u.getAppState().isSingleGame(), { user: a } = this.props, { isFetching: r, inGameItem: i, error: n, baseApp: o, itemPublishingContent: I } = this.logic.state;
    n && h.extractServiceErrorCode(n) === b.ITEM_NOT_FOUND && M({ eventCategory: B.Error404.makePath(), eventLabel: h.errorToString(n) });
    const { purchaseLogic: l } = this.logic, E = l && l.state.currencyMap, { isLightboxOpen: P, selectedImagePosition: f } = this.state, c = W.getMedia(i, I);
    return /* @__PURE__ */ e.createElement("main", { className: "itemDetailPage" }, /* @__PURE__ */ e.createElement(
      L,
      {
        purchaseLogic: l,
        user: a,
        isFetching: r
      }
    ), /* @__PURE__ */ e.createElement(q, null, i && /* @__PURE__ */ e.createElement("title", null, U.makeTitleWithAppName(i.title))), r && /* @__PURE__ */ e.createElement($, null), n && /* @__PURE__ */ e.createElement(j, { error: n }), i && o && /* @__PURE__ */ e.createElement(e.Fragment, null, /* @__PURE__ */ e.createElement(A, { item: i, itemPublishingContent: I }, /* @__PURE__ */ e.createElement(G, null, /* @__PURE__ */ e.createElement(g, { to: p.GameList.makePath() }, /* @__PURE__ */ e.createElement(m, { i18nKey: "Navigation.links.games" }, "Games")), /* @__PURE__ */ e.createElement(
      g,
      {
        to: p.GameItem.makePath({
          itemId: o.itemId
        })
      },
      o.title
    ), i.title)), c && /* @__PURE__ */ e.createElement(C, { itemMedias: c, openLightbox: this.openLightbox }), P && c && /* @__PURE__ */ e.createElement(
      w,
      {
        itemMedias: c,
        isOpen: P,
        onClose: this.closeLightbox,
        selectedPosition: f
      }
    ), /* @__PURE__ */ e.createElement("div", { className: "itemDetailContent container" }, /* @__PURE__ */ e.createElement("div", { className: "description" }, /* @__PURE__ */ e.createElement(S, { item: i })), /* @__PURE__ */ e.createElement("div", { className: "detailWidget" }, /* @__PURE__ */ e.createElement(D, { item: i }, E && /* @__PURE__ */ e.createElement(k, { item: i, currencyMap: E, position: "right", showDetail: !0 })), /* @__PURE__ */ e.createElement("div", { className: "protruded" }, /* @__PURE__ */ e.createElement(v, { item: i, purchaseLogic: l })), /* @__PURE__ */ e.createElement("div", { className: "requiredApp" }, /* @__PURE__ */ e.createElement(m, { i18nKey: "InGameItemDetailPage.requiredAppNotice" }, "Required Game"), /* @__PURE__ */ e.createElement(
      g,
      {
        to: _.isDesktopApp() && t ? H.Game.link : p.GameItem.makePath({
          itemId: o.itemId
        })
      },
      o.title
    )))), /* @__PURE__ */ e.createElement("div", { className: "storeContainer" }, /* @__PURE__ */ e.createElement("h2", null, /* @__PURE__ */ e.createElement(m, { i18nKey: "InGameItemDetailPage.inAppStore.heading" }, "Related Items")), /* @__PURE__ */ e.createElement(
      N,
      {
        baseItem: i,
        language: this.props.language,
        noItemMessage: /* @__PURE__ */ e.createElement("div", { className: "noItem" }, /* @__PURE__ */ e.createElement(m, { i18nKey: "InGameItemDetailPage.inAppStore.noItem" }, "Looks like there are no related items yet."))
      }
    ))));
  }
}
const Ie = R(
  ({ itemId: s, language: t }) => {
    const a = u.getAppState().getUser() || void 0, r = a && a.country || void 0;
    return /* @__PURE__ */ e.createElement(z, { itemId: s, country: r, user: a, language: t });
  },
  (s, t) => s.itemId !== t.itemId || t.language !== s.language
);
export {
  z as InGameItemDetailPage,
  j as ItemDetailPageError,
  $ as ItemDetailPageLoadingScreen,
  Ie as default
};
