import { S as w, O as S, I as L, C as A, B as D, A as N, L as T, a as M, b as v, D as k, c as F } from "./ItemDetailPurchaseAlert-3cdb1ea7.mjs";
import { C as f, a as u, L as K, R as x, i as R, T as p, w as B, y as E, G as _, ag as O, K as H, F as W, ab as g, t as q, ad as G } from "./index-162fd1aa.mjs";
import { I as U } from "./ItemHelper-37a12c2c.mjs";
import t from "react";
import { a as z } from "./Helmet-ed6d5573.mjs";
import { Link as I } from "react-router-dom";
import "./CodeStore-4ed993c7.mjs";
import { w as $ } from "./WithPropsGuard-9dd19ecc.mjs";
import { ItemDetailPageLoadingScreen as j, ItemDetailPageError as J } from "./InGameItemDetailPage-2ff05429.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 "./MockVars-1cf2eb0a.mjs";
import "./validator-61879ab7.mjs";
import "./CommonError-0ae83068.mjs";
import "./starBgGradient-589e5185.mjs";
import "./CommonError-4ed993c7.mjs";
const y = class extends f {
  constructor(s) {
    super(), this.props = s, this.state = {
      isFetching: !1,
      items: null,
      error: null,
      currencyMap: null
    }, this.sdk = u.getAppState().getSdk();
  }
  async init() {
    try {
      if (this.state.isFetching)
        return;
      await this.setState({
        isFetching: !0,
        error: null,
        items: null,
        currencyMap: null
      });
      const s = await this.fetchItems(), e = await this.fetchCurrencyMap();
      this.setState({ items: s, currencyMap: e });
    } catch (s) {
      console.error(s), this.setState({ error: s });
    } finally {
      await this.setState({ isFetching: !1 });
    }
  }
  async fetchItems() {
    const { baseItem: s, itemType: e, appType: a, country: i, language: r } = this.props, n = s.baseAppId || s.appId;
    let o = !1, l = 0, m = [];
    do {
      const d = await this.sdk.Platform.Item().fetchItemsByCriteria({
        queryParams: {
          baseAppId: n,
          itemType: e,
          appType: a,
          region: i,
          language: r,
          offset: l,
          limit: y.ITEMS_PER_FETCH
        }
      }).then((h) => {
        var c;
        if (h.error)
          throw h.error;
        return o = !!((c = h.response.data.paging) != null && c.next), l += y.ITEMS_PER_FETCH, h.response.data.data.filter((b) => b.itemId !== s.itemId);
      });
      m = m.concat(d);
    } while (o);
    return m;
  }
  async fetchCurrencyMap() {
    return this.sdk.Platform.Currency().getCurrencyMap().then((s) => {
      if (s.error)
        throw s.error;
      return s.value;
    });
  }
};
let C = y;
C.ITEMS_PER_FETCH = 100;
const Q = 8;
class P extends t.Component {
  constructor(e) {
    super(e), this.initialShownItem = Q, e.initialShownItem && (this.initialShownItem = e.initialShownItem), this.logic = new C({
      baseItem: e.baseItem,
      itemType: e.itemType,
      appType: e.appType,
      country: e.country,
      language: e.language
    }), this.logic.subscribe(() => this.setState({})), this.state = {
      open: !1
    };
  }
  componentDidMount() {
    this.logic.init();
  }
  renderContent() {
    const { noItemMessage: e } = this.props, { items: a, isFetching: i, error: r, currencyMap: n } = this.logic.state, { open: o } = this.state;
    return /* @__PURE__ */ t.createElement("div", { className: "relatedProductKeys list" }, /* @__PURE__ */ t.createElement("div", { className: "relatedProductKeysData" }, i && /* @__PURE__ */ t.createElement(K, { className: x.medium }), r && /* @__PURE__ */ t.createElement("div", { className: "errorContainer" }, /* @__PURE__ */ t.createElement(R, { error: r })), a && a.length === 0 && /* @__PURE__ */ t.createElement(t.Fragment, null, e, !e && /* @__PURE__ */ t.createElement(p, { i18nKey: "RelatedProductKey.grid.empty" }, "No Items Available")), /* @__PURE__ */ t.createElement("div", { className: "relatedProductKeysGrid" }, a && n && (o ? a : a.slice(0, this.initialShownItem)).map((l) => /* @__PURE__ */ t.createElement(w, { key: l.itemId, item: l, currencyMap: n })))), a && a.length > this.initialShownItem && !o && /* @__PURE__ */ t.createElement("div", { className: "showMoreButton" }, /* @__PURE__ */ t.createElement("button", { className: "btn btn-primary", type: "button", onClick: () => this.setState({ open: !0 }) }, /* @__PURE__ */ t.createElement(p, { i18nKey: "RelatedProductKey.showAllButton" }, "Show All"))));
  }
  render() {
    const { withWrapper: e } = this.props, { items: a, error: i } = this.logic.state;
    return e ? /* @__PURE__ */ t.createElement(t.Fragment, null, (a && a.length > 0 || i) && e(this.renderContent())) : /* @__PURE__ */ t.createElement(t.Fragment, null, this.renderContent());
  }
}
$(
  (s) => {
    const e = u.getAppState();
    return /* @__PURE__ */ t.createElement(P, { country: (e.getUser() || { country: void 0 }).country, ...s });
  },
  (s, e) => s.baseItem !== e.baseItem || s.country !== e.country
);
class V extends f {
  constructor(e) {
    super(), this.props = e, this.state = {
      codeItem: null,
      itemsInCode: [],
      baseApp: null,
      isFetching: !1,
      error: null,
      itemPublishingContent: null,
      itemPublishingContentError: null
    }, this.sdk = u.getAppState().getSdk();
  }
  setProperties(e) {
    for (const a in e)
      this.props[a] = e[a];
  }
  async init() {
    try {
      if (this.state.isFetching)
        return;
      this.setState({
        isFetching: !0,
        codeItem: null,
        itemsInCode: [],
        error: null
      });
      const e = await this.fetchBundleItem();
      await this.initPurchaseLogic(e);
      const a = await this.fetchItemsInCode(e.itemIds || []);
      let i = null;
      const { baseAppId: r } = e;
      r && (i = a.find((n) => r === n.appId) || await this.fetchBaseApp(r)), this.setState({
        codeItem: e,
        itemsInCode: a,
        baseApp: i,
        itemPublishingContent: null
      });
    } catch (e) {
      console.error(e), this.setState({ error: e });
    } finally {
      this.setState({ isFetching: !1 });
    }
  }
  async fetchBundleItem() {
    const { itemId: e, country: a, language: i } = this.props;
    return await this.sdk.Platform.Item().getItemsByItemIdLocale({ itemId: e, queryParams: { language: i, region: a } }).then((r) => {
      if (r.error)
        throw r.error;
      return r.response.data;
    });
  }
  async fetchItemsInCode(e) {
    if (e.length === 0)
      return [];
    const { language: a, country: i } = this.props;
    return await this.sdk.Platform.Item().getItemsByItemIds({
      queryParams: { language: a, itemIds: e.toString(), region: i }
    }).then((r) => {
      if (r.error)
        throw r.error;
      return r.response.data;
    });
  }
  async fetchBaseApp(e) {
    return await this.sdk.Platform.Item().getItemByAppId({
      appId: e,
      region: this.props.country,
      language: this.props.language
    }).then((a) => {
      if (a.error)
        throw a.error;
      return a.response.data;
    });
  }
  async initPurchaseLogic(e) {
    const { user: a } = this.props, i = this.purchaseLogic = new S(this.sdk, {
      item: e,
      userId: B.safeParse(a).success && (a == null ? void 0 : a.userId) || null
    });
    i.subscribe(() => this.setState({})), await i.init();
    const { fetchError: r } = i.state;
    if (r)
      throw r;
  }
}
class X extends t.Component {
  constructor(e) {
    super(e), this.openLightbox = (o) => {
      this.setState({ isLightboxOpen: !0, selectedImagePosition: o });
    }, this.closeLightbox = () => {
      this.setState({ isLightboxOpen: !1 });
    };
    const { itemId: a, country: i, language: r, user: n } = this.props;
    this.logic = new V({ itemId: a, country: i, language: r, user: n }), this.logic.subscribe(() => this.setState({})), this.state = {
      isLightboxOpen: !1,
      selectedImagePosition: 0
    };
  }
  async componentDidMount() {
    await this.initialize();
  }
  async componentDidUpdate(e) {
    (e.itemId !== this.props.itemId || e.language !== this.props.language) && await this.initialize();
  }
  async initialize() {
    this.logic.setProperties(this.props), await this.logic.init();
  }
  render() {
    const { user: e } = this.props, { isFetching: a, codeItem: i, error: r, baseApp: n, itemPublishingContent: o } = this.logic.state;
    r && E.extractServiceErrorCode(r) === _.ITEM_NOT_FOUND && O({ eventCategory: H.Error404.makePath(), eventLabel: E.errorToString(r) });
    const { purchaseLogic: l } = this.logic, m = l && l.state.currencyMap, { isLightboxOpen: d, selectedImagePosition: h } = this.state, c = U.getMedia(i, o);
    return /* @__PURE__ */ t.createElement("main", { className: "itemDetailPage" }, /* @__PURE__ */ t.createElement(
      L,
      {
        purchaseLogic: l,
        user: e,
        isFetching: a
      }
    ), /* @__PURE__ */ t.createElement(z, null, i && /* @__PURE__ */ t.createElement("title", null, W.makeTitleWithAppName(i.title))), a && /* @__PURE__ */ t.createElement(j, null), r && /* @__PURE__ */ t.createElement(J, { error: r }), i && /* @__PURE__ */ t.createElement(t.Fragment, null, /* @__PURE__ */ t.createElement(A, { item: i, itemPublishingContent: o }, /* @__PURE__ */ t.createElement(D, null, /* @__PURE__ */ t.createElement(I, { to: g.CodeList.makePath() }, /* @__PURE__ */ t.createElement(p, { i18nKey: "Navigation.links.productKeys" }, "Product Keys")), n && /* @__PURE__ */ t.createElement(I, { to: g.getItemDetailRoute(n) }, n.title), i.title)), c && /* @__PURE__ */ t.createElement(N, { itemMedias: c, openLightbox: this.openLightbox }), d && c && /* @__PURE__ */ t.createElement(
      T,
      {
        itemMedias: c,
        isOpen: d,
        onClose: this.closeLightbox,
        selectedPosition: h
      }
    ), /* @__PURE__ */ t.createElement("div", { className: "itemDetailContent" }, /* @__PURE__ */ t.createElement("div", { className: "description" }, /* @__PURE__ */ t.createElement(M, { item: i })), /* @__PURE__ */ t.createElement("div", { className: "detailWidget" }, /* @__PURE__ */ t.createElement(v, { item: i, tag: q("CodeItemDetailPage.tag") }, m && /* @__PURE__ */ t.createElement(k, { item: i, currencyMap: m, position: "right", showDetail: !0 })), /* @__PURE__ */ t.createElement("div", { className: "protruded" }, /* @__PURE__ */ t.createElement(F, { item: i, purchaseLogic: l })), n && /* @__PURE__ */ t.createElement("div", { className: "requiredApp" }, /* @__PURE__ */ t.createElement(p, { i18nKey: "CodeItemDetailPage.requiredAppNotice" }, "Required Game"), /* @__PURE__ */ t.createElement(I, { to: g.getItemDetailRoute(n) }, n.title)))), /* @__PURE__ */ t.createElement("div", { className: "storeContainer" }, /* @__PURE__ */ t.createElement("h2", null, /* @__PURE__ */ t.createElement(p, { i18nKey: "CodeItemDetailPage.inAppStore.heading" }, "Related Items")), /* @__PURE__ */ t.createElement(
      P,
      {
        baseItem: i,
        language: this.props.language,
        itemType: G.enum.CODE,
        noItemMessage: /* @__PURE__ */ t.createElement("div", { className: "noItem" }, /* @__PURE__ */ t.createElement(p, { i18nKey: "CodeItemDetailPage.inAppStore.noItem" }, "Looks like there are no related items yet."))
      }
    ))));
  }
}
const Pe = t.memo(
  (s) => {
    const { itemId: e, language: a } = s, i = u.getAppState().getUser() || void 0, r = i && i.country || void 0;
    return /* @__PURE__ */ t.createElement(X, { itemId: e, country: r, user: i, language: a });
  },
  (s, e) => s.itemId === e.itemId && e.language === s.language
);
export {
  Pe as default
};
