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 { C as CommonError } from "./CommonError.js";
import { C as Container, a as Ctx, aj as EligibleUser, L as Loading, w as LoadingSize, T as Trans, E as ErrorHelper, q as EcommerceErrorCode, ak as gaSendErrorEvent, r as ErrorPath, F as FeUtils, a0 as EcomPath, D as DesktopChecker, G as GlobalPath } from "./index.js";
import { w as withPropsGuard } from "./WithPropsGuard.js";
import { I as ItemHelper } from "./ItemHelper.js";
import React__default from "react";
import { H as HelmetExport } from "./Helmet.js";
import { Link } from "react-router-dom";
import "./reactSlick.js";
import "./dateFns.js";
import "./default-item-image.js";
import "./reactPlayer.js";
import "./SuccessFlatIcon.js";
import "./WarningIcon.js";
import "./PendingOrdersAppManager.js";
import "./PaymentStatusDialog.js";
import "./starBgGradient.js";
import "./CommonError2.js";
import "./validator.js";
var InGameItemDetailPage$2 = "";
class InGameItemDetailPageLogic extends Container {
  constructor(props) {
    super();
    this.props = props;
    this.state = {
      inGameItem: null,
      baseApp: null,
      isFetching: false,
      error: null,
      itemPublishingContent: null,
      itemPublishingContentError: null
    };
    this.sdk = Ctx.getAppState().getSdk();
  }
  async init() {
    try {
      if (this.state.isFetching)
        return;
      await this.setState({
        isFetching: true,
        inGameItem: null,
        baseApp: null,
        error: null
      });
      const inGameItem = await this.fetchInGameItem();
      if (!inGameItem.baseAppId)
        throw new NoBaseAppIdError("");
      await this.initPurchaseLogic(inGameItem);
      const baseApp = await this.fetchBaseApp(inGameItem.baseAppId);
      await this.setState({
        inGameItem,
        baseApp,
        itemPublishingContent: null
      });
    } catch (error) {
      console.error(error);
      this.setState({ error });
    } finally {
      await this.setState({ isFetching: false });
    }
  }
  async fetchInGameItem() {
    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 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 : ""
    });
    purchaseLogic.subscribe(() => this.setState({}));
    await purchaseLogic.init();
    const { fetchError } = purchaseLogic.state;
    if (fetchError)
      throw fetchError;
  }
}
class NoBaseAppIdError extends Error {
  constructor(message) {
    super(message);
    Object.setPrototypeOf(this, NoBaseAppIdError.prototype);
  }
}
const ItemDetailPageLoadingScreen = () => /* @__PURE__ */ React__default.createElement(Loading, {
  className: LoadingSize.tall
}, /* @__PURE__ */ React__default.createElement(Trans, {
  i18nKey: "InitialUserFetchPage.notice"
}));
const ItemDetailPageError = ({ error }) => {
  const errorMessage = () => {
    if (error && ErrorHelper.extractServiceErrorCode(error) === EcommerceErrorCode.PRODUCT_KEY_NOT_AVAILABLE) {
      return /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "GameDetailViewPage.item.productKeyNotAvailable"
      }, "This product key is no longer available");
    }
    return /* @__PURE__ */ React__default.createElement(CommonError, {
      error
    });
  };
  return /* @__PURE__ */ React__default.createElement("div", {
    className: "error"
  }, errorMessage());
};
class InGameItemDetailPage 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 } = props;
    this.logic = new InGameItemDetailPageLogic({ itemId, country, language, user });
    this.logic.subscribe(() => this.setState({}));
    this.state = {
      isLightboxOpen: false,
      selectedImagePosition: 0
    };
  }
  componentDidMount() {
    this.logic.init();
  }
  render() {
    const isSingleGame = Ctx.getAppState().isSingleGame();
    const { user } = this.props;
    const { isFetching, inGameItem, error, baseApp, 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.getMedia(inGameItem, itemPublishingContent);
    return /* @__PURE__ */ React__default.createElement("main", {
      className: "itemDetailPage"
    }, /* @__PURE__ */ React__default.createElement(ItemDetailPurchaseAlert, {
      ...{
        purchaseLogic,
        user,
        isFetching
      }
    }), /* @__PURE__ */ React__default.createElement(HelmetExport, null, inGameItem && /* @__PURE__ */ React__default.createElement("title", null, FeUtils.makeTitleWithAppName(inGameItem.title))), isFetching && /* @__PURE__ */ React__default.createElement(ItemDetailPageLoadingScreen, null), error && /* @__PURE__ */ React__default.createElement(ItemDetailPageError, {
      error
    }), inGameItem && baseApp && /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, /* @__PURE__ */ React__default.createElement(CatalogItemBanner, {
      item: inGameItem,
      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")), /* @__PURE__ */ React__default.createElement(Link, {
      to: EcomPath.GameItem.makePath({
        itemId: baseApp.itemId
      })
    }, baseApp.title), inGameItem.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 container"
    }, /* @__PURE__ */ React__default.createElement("div", {
      className: "description"
    }, /* @__PURE__ */ React__default.createElement(CatalogOverview, {
      item: inGameItem
    })), /* @__PURE__ */ React__default.createElement("div", {
      className: "detailWidget"
    }, /* @__PURE__ */ React__default.createElement(CatalogTitleWidget, {
      item: inGameItem
    }, currencyMap && /* @__PURE__ */ React__default.createElement(DiscountCountdownWidget, {
      item: inGameItem,
      currencyMap,
      position: "right",
      showDetail: true
    })), /* @__PURE__ */ React__default.createElement("div", {
      className: "protruded"
    }, /* @__PURE__ */ React__default.createElement(CatalogPurchaseWidget, {
      item: inGameItem,
      purchaseLogic
    })), /* @__PURE__ */ React__default.createElement("div", {
      className: "requiredApp"
    }, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "InGameItemDetailPage.requiredAppNotice"
    }, "Required Game"), /* @__PURE__ */ React__default.createElement(Link, {
      to: DesktopChecker.isDesktopApp() && isSingleGame ? GlobalPath.Game.link : EcomPath.GameItem.makePath({
        itemId: baseApp.itemId
      })
    }, baseApp.title)))), /* @__PURE__ */ React__default.createElement("div", {
      className: "storeContainer"
    }, /* @__PURE__ */ React__default.createElement("h2", null, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "InGameItemDetailPage.inAppStore.heading"
    }, "Related Items")), /* @__PURE__ */ React__default.createElement(InAppStore, {
      baseItem: inGameItem,
      language: this.props.language,
      noItemMessage: /* @__PURE__ */ React__default.createElement("div", {
        className: "noItem"
      }, /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "InGameItemDetailPage.inAppStore.noItem"
      }, "Looks like there are no related items yet."))
    }))));
  }
}
var InGameItemDetailPage$1 = withPropsGuard(({ itemId, language }) => {
  const user = Ctx.getAppState().getUser() || void 0;
  const country = user && user.country || void 0;
  return /* @__PURE__ */ React__default.createElement(InGameItemDetailPage, {
    itemId,
    country,
    user,
    language
  });
}, (prevProps, nextProps) => prevProps.itemId !== nextProps.itemId || nextProps.language !== prevProps.language);
export { InGameItemDetailPage, ItemDetailPageError, ItemDetailPageLoadingScreen, InGameItemDetailPage$1 as default };
