import { O as OrderAppManager, I as ItemDetailPurchaseAlert, C as CatalogItemBanner, B as Breadcrumbs, A as AppDetailGallery, L as Lightbox, a as CatalogOverview, b as CatalogTitleWidget, D as DiscountCountdownWidget, c as CatalogPurchaseWidget, d as InAppStore } from "./ItemDetailPurchaseAlert.js";
import { w as withPropsGuard } from "./WithPropsGuard.js";
import { a as ItemImageTag, I as ItemHelper$1 } from "./ItemHelper.js";
import { C as Container, a as Ctx, ac as EligibleUser, T as Trans, ak as ItemHelper, a2 as EcomPath, ab as PurchaseHelper, E as ErrorHelper, q as EcommerceErrorCode, ad as gaSendErrorEvent, r as ErrorPath, F as FeUtils, D as DesktopChecker } from "./index.js";
import React__default from "react";
import { a as Helmet } from "./Helmet.js";
import { Link } from "react-router-dom";
import { ItemDetailPageLoadingScreen, ItemDetailPageError } from "./InGameItemDetailPage.js";
import { d as defaultItemImage } from "./default-item-image.js";
import { c as classNames } from "./reactSlick.js";
import "./reactPlayer.js";
import "./dateFns.js";
import "./SuccessFlatIcon.js";
import "./WarningIcon.js";
import "./PendingOrdersAppManager.js";
import "./PaymentStatusDialog.js";
import "./Radio.js";
import "./validator.js";
import "./CommonError.js";
import "./starBgGradient.js";
import "./CommonError2.js";
class BundleItemDetailPageLogic extends Container {
  constructor(props) {
    super();
    this.props = props;
    this.state = {
      bundleItem: null,
      itemsInBundle: [],
      baseApp: null,
      isFetching: false,
      error: null,
      itemPublishingContent: null,
      itemPublishingContentError: null
    };
    this.sdk = Ctx.getAppState().getSdk();
  }
  async init() {
    try {
      if (this.state.isFetching)
        return;
      this.setState({
        isFetching: true,
        bundleItem: null,
        itemsInBundle: [],
        error: null
      });
      const bundleItem = await this.fetchBundleItem();
      await this.initPurchaseLogic(bundleItem);
      const itemsInBundle = await this.fetchItemsInBundle(bundleItem.itemIds || []);
      let baseApp = null;
      const { baseAppId } = bundleItem;
      if (baseAppId) {
        baseApp = itemsInBundle.find((item) => baseAppId === item.appId) || await this.fetchBaseApp(baseAppId);
      }
      this.setState({
        bundleItem,
        itemsInBundle,
        baseApp,
        itemPublishingContent: null
      });
    } catch (error) {
      console.error(error);
      this.setState({ error });
    } finally {
      this.setState({ isFetching: false });
    }
  }
  async fetchBundleItem() {
    const { itemId, country, language } = this.props;
    return await this.sdk.Platform.Item().getItemsByItemIdLocale({ itemId, queryParams: { language, region: country } }).then((result) => {
      if (result.error)
        throw result.error;
      return result.response.data;
    });
  }
  async fetchItemsInBundle(itemIds) {
    if (itemIds.length === 0)
      return [];
    const { language, country } = this.props;
    return await this.sdk.Platform.Item().getItemsByItemIds({
      queryParams: { language, region: country, itemIds: itemIds.toString() }
    }).then((result) => {
      if (result.error)
        throw result.error;
      return result.response.data;
    });
  }
  async fetchBaseApp(baseAppId) {
    return await this.sdk.Platform.Item().getItemByAppId({
      appId: baseAppId,
      region: this.props.country,
      language: this.props.language
    }).then((result) => {
      if (result.error)
        throw result.error;
      return result.response.data;
    });
  }
  async initPurchaseLogic(item) {
    const { user } = this.props;
    const purchaseLogic = this.purchaseLogic = new OrderAppManager(this.sdk, {
      item,
      userId: EligibleUser.safeParse(user).success && (user == null ? void 0 : user.userId) || null
    });
    purchaseLogic.subscribe(() => this.setState({}));
    await purchaseLogic.init();
    const { fetchError } = purchaseLogic.state;
    if (fetchError)
      throw fetchError;
  }
}
const catalogBundleItemList = "catalogBundleItemList _catalogBundleItemList_bd059";
var styles = {
  catalogBundleItemList
};
const CatalogBundleItem = ({ link, thumbnail, name, type, quantity }) => /* @__PURE__ */ React__default.createElement(Link, {
  to: link,
  className: "catalogItem"
}, /* @__PURE__ */ React__default.createElement("div", {
  className: "catalogItemImage"
}, /* @__PURE__ */ React__default.createElement("img", {
  src: thumbnail || defaultItemImage,
  loading: "lazy"
})), /* @__PURE__ */ React__default.createElement("div", {
  className: "catalogItemDetail"
}, /* @__PURE__ */ React__default.createElement("div", {
  className: "catalogItemHeading"
}, name), /* @__PURE__ */ React__default.createElement("div", {
  className: "catalogItemType"
}, type), /* @__PURE__ */ React__default.createElement("div", {
  className: "catalogItemQty"
}, quantity > 1 && `x${quantity}`)));
class CatalogBundleItemList extends React__default.Component {
  constructor(props) {
    super(props);
    this.state = {
      showAll: false
    };
  }
  render() {
    const { items, bundleItem } = this.props;
    return /* @__PURE__ */ React__default.createElement("div", {
      className: classNames(styles.catalogBundleItemList, "catalogBundleItemList")
    }, /* @__PURE__ */ React__default.createElement("h2", {
      className: "catalogHeading"
    }, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "CatalogBundleItemList.heading"
    }, "Inside the Bundle")), items.length > 0 && /* @__PURE__ */ React__default.createElement("div", {
      className: "catalogList"
    }, items.map((item) => {
      const type = item.appType || item.itemType;
      const itemCoverImageUrl = ItemHelper.getFilteredImageUrlByTag(item.images, ItemImageTag.productCover) || defaultItemImage;
      return /* @__PURE__ */ React__default.createElement(CatalogBundleItem, {
        key: item.itemId,
        link: EcomPath.getItemDetailRoute(item),
        thumbnail: itemCoverImageUrl,
        name: item.title || item.name || "",
        type,
        quantity: bundleItem.itemQty ? bundleItem.itemQty[item.itemId] : 1
      });
    })), items.length === 0 && /* @__PURE__ */ React__default.createElement("div", {
      className: "catalogNoItem"
    }, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "CatalogBundleItemList.noItem"
    }, "Looks like there aren't any bundles for this game yet.")));
  }
}
class BundleItemDetailPage extends React__default.Component {
  constructor(props) {
    super(props);
    this.openLightbox = (selectedImagePosition) => {
      this.setState({ isLightboxOpen: true, selectedImagePosition });
    };
    this.closeLightbox = () => {
      this.setState({ isLightboxOpen: false });
    };
    const { itemId, country, language, user } = this.props;
    this.logic = new BundleItemDetailPageLogic({ itemId, country, language, user });
    this.logic.subscribe(() => this.setState({}));
    this.state = {
      isLightboxOpen: false,
      selectedImagePosition: 0
    };
  }
  componentDidMount() {
    this.logic.init();
  }
  hasPendingOrder() {
    var _a, _b, _c;
    return !!((_c = (_b = (_a = this.logic.purchaseLogic) == null ? void 0 : _a.state) == null ? void 0 : _b.pendingOrders) == null ? void 0 : _c[0]);
  }
  isItemUnavailable() {
    const { purchaseLogic } = this.logic;
    return !!(purchaseLogic && purchaseLogic.state.purchaseConstraint && PurchaseHelper.purchaseUnavailable(purchaseLogic.state.purchaseConstraint));
  }
  render() {
    const isSingleGame = Ctx.getAppState().isSingleGame();
    const { user } = this.props;
    const { isFetching, bundleItem, error, baseApp, itemsInBundle, itemPublishingContent } = this.logic.state;
    if (error && ErrorHelper.extractServiceErrorCode(error) === EcommerceErrorCode.ITEM_NOT_FOUND) {
      gaSendErrorEvent({ eventCategory: ErrorPath.Error404.makePath(), eventLabel: ErrorHelper.errorToString(error) });
    }
    const { purchaseLogic } = this.logic;
    const currencyMap = purchaseLogic && purchaseLogic.state.currencyMap;
    const { isLightboxOpen, selectedImagePosition } = this.state;
    const itemMedias = ItemHelper$1.getMedia(bundleItem, itemPublishingContent);
    return /* @__PURE__ */ React__default.createElement("main", {
      className: "itemDetailPage"
    }, /* @__PURE__ */ React__default.createElement(ItemDetailPurchaseAlert, {
      ...{
        purchaseLogic,
        user,
        isFetching
      }
    }), /* @__PURE__ */ React__default.createElement(Helmet, null, bundleItem && /* @__PURE__ */ React__default.createElement("title", null, FeUtils.makeTitleWithAppName(bundleItem.title))), isFetching && /* @__PURE__ */ React__default.createElement(ItemDetailPageLoadingScreen, null), error && /* @__PURE__ */ React__default.createElement(ItemDetailPageError, {
      error
    }), bundleItem && /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, /* @__PURE__ */ React__default.createElement(CatalogItemBanner, {
      item: bundleItem,
      itemPublishingContent
    }, /* @__PURE__ */ React__default.createElement(Breadcrumbs, null, /* @__PURE__ */ React__default.createElement(Link, {
      to: EcomPath.GameList.makePath()
    }, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "Navigation.links.games"
    }, "Games")), baseApp && /* @__PURE__ */ React__default.createElement(Link, {
      to: EcomPath.getItemDetailRoute(baseApp)
    }, baseApp.title), bundleItem.title)), itemMedias && /* @__PURE__ */ React__default.createElement(AppDetailGallery, {
      itemMedias,
      openLightbox: this.openLightbox
    }), isLightboxOpen && itemMedias && /* @__PURE__ */ React__default.createElement(Lightbox, {
      itemMedias,
      isOpen: isLightboxOpen,
      onClose: this.closeLightbox,
      selectedPosition: selectedImagePosition
    }), /* @__PURE__ */ React__default.createElement("div", {
      className: "itemDetailContent"
    }, /* @__PURE__ */ React__default.createElement("div", {
      className: "description"
    }, /* @__PURE__ */ React__default.createElement(CatalogBundleItemList, {
      items: itemsInBundle,
      bundleItem
    }), /* @__PURE__ */ React__default.createElement(CatalogOverview, {
      item: bundleItem
    })), /* @__PURE__ */ React__default.createElement("div", {
      className: "detailWidget"
    }, /* @__PURE__ */ React__default.createElement(CatalogTitleWidget, {
      item: bundleItem
    }, currencyMap && /* @__PURE__ */ React__default.createElement(DiscountCountdownWidget, {
      item: bundleItem,
      currencyMap,
      position: "right",
      showDetail: true
    })), /* @__PURE__ */ React__default.createElement("div", {
      className: "protruded"
    }, /* @__PURE__ */ React__default.createElement(CatalogPurchaseWidget, {
      item: bundleItem,
      purchaseLogic,
      itemsInBundle
    })), baseApp && /* @__PURE__ */ React__default.createElement("div", {
      className: "requiredApp"
    }, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "BundleItemDetailPage.requiredAppNotice"
    }, "Required Game"), DesktopChecker.isDesktopApp() && isSingleGame ? /* @__PURE__ */ React__default.createElement(Link, {
      to: `/game`
    }, baseApp.title) : /* @__PURE__ */ React__default.createElement(Link, {
      to: EcomPath.getItemDetailRoute(baseApp)
    }, baseApp.title)))), /* @__PURE__ */ React__default.createElement("div", {
      className: "storeContainer"
    }, /* @__PURE__ */ React__default.createElement("h2", null, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "BundleItemDetailPage.inAppStore.heading"
    }, "Related Items")), /* @__PURE__ */ React__default.createElement(InAppStore, {
      baseItem: bundleItem,
      language: this.props.language,
      noItemMessage: /* @__PURE__ */ React__default.createElement("div", {
        className: "noItem"
      }, /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "BundleItemDetailPage.inAppStore.noItem"
      }, "Looks like there are no related items yet."))
    }))));
  }
}
var BundleItemDetailPage$1 = withPropsGuard(({ itemId, language }) => {
  const user = Ctx.getAppState().getUser() || void 0;
  const country = user && user.country || void 0;
  return /* @__PURE__ */ React__default.createElement(BundleItemDetailPage, {
    itemId,
    country,
    user,
    language
  });
}, (prevProps, nextProps) => prevProps.itemId !== nextProps.itemId || nextProps.language !== prevProps.language);
export { BundleItemDetailPage, BundleItemDetailPage$1 as default };
