import { O as OrderAppManager, I as ItemDetailPurchaseAlert, C as CatalogItemBanner, B as Breadcrumbs, c as CatalogPurchaseWidget, A as AppDetailGallery, L as Lightbox, e as InAppStore, a as CatalogOverview, b as CatalogTitleWidget, D as DiscountCountdownWidget } from "./ItemDetailPurchaseAlert.js";
import { C as CommonError } from "./CommonError.js";
import { T as Trans, C as Container, a as Ctx, ac as EligibleUser, as as LoadingPage, k as UIEvent, l as bgCommon, E as ErrorHelper, q as EcommerceErrorCode, ad as gaSendErrorEvent, r as ErrorPath, F as FeUtils, a2 as EcomPath, a7 as ItemType, ar as AppType, at as DecodeError, au as FeatureDecodeError } 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 { P as PlatformRequirements } from "./PlatformRequirements.js";
var AdditionalDetail$1 = "";
const AdditionalDetail = (props) => {
  const { additionalAppInfo } = props;
  const { platforms, releaseDate, primaryGenre, genres, developer, publisher, websiteUrl, forumUrl } = additionalAppInfo;
  if (!platforms && !releaseDate && !genres && !developer && !publisher && !websiteUrl && !forumUrl) {
    return null;
  }
  return /* @__PURE__ */ React__default.createElement("div", {
    className: "gameDetailInfo"
  }, /* @__PURE__ */ React__default.createElement("ul", null, platforms && platforms.length > 0 && /* @__PURE__ */ React__default.createElement("li", {
    className: "infoWrapper"
  }, /* @__PURE__ */ React__default.createElement("h2", {
    className: "infoTitle"
  }, /* @__PURE__ */ React__default.createElement(Trans, {
    i18nKey: "GameDetailViewPage.detailInfo.platform"
  }, "Platform")), /* @__PURE__ */ React__default.createElement("p", {
    className: "infoContent"
  }, platforms.join(", "))), releaseDate && /* @__PURE__ */ React__default.createElement("li", {
    className: "infoWrapper"
  }, /* @__PURE__ */ React__default.createElement("h2", {
    className: "infoTitle"
  }, /* @__PURE__ */ React__default.createElement(Trans, {
    i18nKey: "GameDetailViewPage.detailInfo.releaseDate"
  }, "Release Date")), /* @__PURE__ */ React__default.createElement("p", {
    className: "infoContent"
  }, /* @__PURE__ */ React__default.createElement(Trans, {
    i18nKey: "GameDetailViewPage.detailInfo.releaseDateValue"
  }, { releaseDate: new Date(releaseDate) }))), primaryGenre && /* @__PURE__ */ React__default.createElement("li", {
    className: "infoWrapper"
  }, /* @__PURE__ */ React__default.createElement("h2", {
    className: "infoTitle"
  }, /* @__PURE__ */ React__default.createElement(Trans, {
    i18nKey: "GameDetailViewPage.detailInfo.primaryGenre"
  }, "Primary Genre")), /* @__PURE__ */ React__default.createElement("p", {
    className: "infoContent"
  }, primaryGenre)), genres && genres.length > 0 && /* @__PURE__ */ React__default.createElement("li", {
    className: "infoWrapper"
  }, /* @__PURE__ */ React__default.createElement("h2", {
    className: "infoTitle"
  }, /* @__PURE__ */ React__default.createElement(Trans, {
    i18nKey: "GameDetailViewPage.detailInfo.genre"
  }, "Genre")), /* @__PURE__ */ React__default.createElement("p", {
    className: "infoContent"
  }, genres.join(", "))), developer && /* @__PURE__ */ React__default.createElement("li", {
    className: "infoWrapper"
  }, /* @__PURE__ */ React__default.createElement("h2", {
    className: "infoTitle"
  }, /* @__PURE__ */ React__default.createElement(Trans, {
    i18nKey: "GameDetailViewPage.detailInfo.developer"
  }, "Developer")), /* @__PURE__ */ React__default.createElement("p", {
    className: "infoContent"
  }, developer)), publisher && /* @__PURE__ */ React__default.createElement("li", {
    className: "infoWrapper"
  }, /* @__PURE__ */ React__default.createElement("h2", {
    className: "infoTitle"
  }, /* @__PURE__ */ React__default.createElement(Trans, {
    i18nKey: "GameDetailViewPage.detailInfo.publisher"
  }, "Publisher")), /* @__PURE__ */ React__default.createElement("p", {
    className: "infoContent"
  }, publisher)), websiteUrl && /* @__PURE__ */ React__default.createElement("li", {
    className: "infoWrapper"
  }, /* @__PURE__ */ React__default.createElement("h2", {
    className: "infoTitle"
  }, /* @__PURE__ */ React__default.createElement(Trans, {
    i18nKey: "GameDetailViewPage.detailInfo.websiteUrl"
  }, "Website URL")), /* @__PURE__ */ React__default.createElement("p", {
    className: "infoContent"
  }, websiteUrl)), forumUrl && /* @__PURE__ */ React__default.createElement("li", {
    className: "infoWrapper"
  }, /* @__PURE__ */ React__default.createElement("h2", {
    className: "infoTitle"
  }, /* @__PURE__ */ React__default.createElement(Trans, {
    i18nKey: "GameDetailViewPage.detailInfo.forumUrl"
  }, "Forum URL")), /* @__PURE__ */ React__default.createElement("p", {
    className: "infoContent"
  }, forumUrl))));
};
class AppDetailPageLogic extends Container {
  constructor(props) {
    super();
    this.props = props;
    this.state = {
      isFetching: false,
      appItem: null,
      additionalAppInfo: null,
      itemPublishingContent: null,
      error: null,
      itemPublishingContentError: null
    };
    this.sdk = Ctx.getAppState().getSdk();
  }
  async init() {
    try {
      if (this.state.isFetching)
        return;
      await this.setState({
        isFetching: true,
        error: null,
        appItem: null,
        additionalAppInfo: null
      });
      const { itemId, country, language, user } = this.props;
      const [
        { response: appItemResponse, error: appItemError },
        { response: additionalInfoResponse, error: additionalInfoError },
        { response: itemPublishingContentResponse, error: itemPublishingContentError }
      ] = await Promise.all([
        this.sdk.Platform.Item().getItemsByItemIdLocale({
          itemId,
          queryParams: {
            region: country,
            language
          }
        }),
        this.sdk.Platform.Item().getAppInfoByItemId({ itemId }),
        { response: null, error: null }
      ]);
      if (appItemError)
        throw appItemError;
      if (additionalInfoError)
        throw additionalInfoError;
      if (additionalInfoResponse && appItemResponse) {
        const appItem = appItemResponse.data;
        const purchaseLogic = this.purchaseLogic = new OrderAppManager(this.sdk, {
          item: appItem,
          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;
        await this.setState({
          appItem,
          additionalAppInfo: additionalInfoResponse.data
        });
      }
      if (itemPublishingContentError) {
        await this.setState({
          itemPublishingContentError
        });
      } else if (itemPublishingContentResponse) {
        await this.setState({
          itemPublishingContent: itemPublishingContentResponse.data
        });
      }
    } catch (error) {
      console.error(error);
      this.setState({ error });
    } finally {
      await this.setState({ isFetching: false });
    }
  }
}
const AppDetailPageLoadingScreen = () => /* @__PURE__ */ React__default.createElement(LoadingPage, {
  className: "mt-3 md:mt-7"
});
const AppDetailPageError = ({ error }) => {
  const errorMessage = () => {
    switch (true) {
      case error instanceof FeatureDecodeError:
      case error instanceof DecodeError:
        return /* @__PURE__ */ React__default.createElement(Trans, {
          i18nKey: "AppDetailPage.item.decodeError"
        }, "The game data from the server is invalid. Please refresh the page.");
      default:
        return /* @__PURE__ */ React__default.createElement(CommonError, {
          error
        });
    }
  };
  return /* @__PURE__ */ React__default.createElement("div", {
    className: "error"
  }, errorMessage());
};
class AppDetailPage extends React__default.Component {
  constructor(props) {
    super(props);
    this.openLightbox = (selectedImagePosition) => {
      this.setState({ isLightboxOpen: true, selectedImagePosition });
    };
    this.closeLightbox = () => {
      this.setState({ isLightboxOpen: false });
    };
    const user = Ctx.getUserManager().state.user || void 0;
    const country = user == null ? void 0 : user.country;
    const { itemId, language } = props;
    this.logic = new AppDetailPageLogic({ itemId, country, user, language });
    this.logic.subscribe(() => this.setState({}));
    this.state = {
      isLightboxOpen: false,
      selectedImagePosition: 0
    };
  }
  componentDidMount() {
    UIEvent.updateBackground(`url(${bgCommon})`);
    this.logic.init();
  }
  componentWillUnmount() {
    UIEvent.updateBackground(null);
  }
  render() {
    const user = Ctx.getUserManager().state.user || void 0;
    const { language } = this.props;
    const { isLightboxOpen, selectedImagePosition } = this.state;
    const { appItem, additionalAppInfo, itemPublishingContent, isFetching, error } = this.logic.state;
    const { purchaseLogic } = this.logic;
    const itemMedias = ItemHelper.getMedia(appItem, itemPublishingContent);
    const isSingleGameTemplate = Ctx.getAppState().isSingleGame();
    if (error && ErrorHelper.extractServiceErrorCode(error) === EcommerceErrorCode.ITEM_NOT_FOUND) {
      gaSendErrorEvent({ eventCategory: ErrorPath.Error404.makePath(), eventLabel: ErrorHelper.errorToString(error) });
    }
    return /* @__PURE__ */ React__default.createElement("div", {
      className: "itemDetailPage"
    }, /* @__PURE__ */ React__default.createElement(ItemDetailPurchaseAlert, {
      ...{
        purchaseLogic,
        user,
        isFetching
      }
    }), /* @__PURE__ */ React__default.createElement(HelmetExport, null, appItem && /* @__PURE__ */ React__default.createElement("title", null, FeUtils.makeTitleWithAppName(appItem.title))), isFetching && /* @__PURE__ */ React__default.createElement(AppDetailPageLoadingScreen, null), error && /* @__PURE__ */ React__default.createElement(AppDetailPageError, {
      error
    }), appItem && /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, /* @__PURE__ */ React__default.createElement(CatalogItemBanner, {
      item: appItem,
      itemPublishingContent
    }, !isSingleGameTemplate && /* @__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")), appItem.title), isSingleGameTemplate && /* @__PURE__ */ React__default.createElement(CatalogPurchaseWidget, {
      item: appItem,
      itemPublishingContent,
      purchaseLogic,
      additionalAppInfo
    })), itemMedias && !isSingleGameTemplate && /* @__PURE__ */ React__default.createElement(AppDetailGallery, {
      itemMedias,
      openLightbox: this.openLightbox
    }), isLightboxOpen && itemMedias && !isSingleGameTemplate && /* @__PURE__ */ React__default.createElement(Lightbox, {
      itemMedias,
      isOpen: isLightboxOpen,
      onClose: this.closeLightbox,
      selectedPosition: selectedImagePosition
    }), !isSingleGameTemplate && /* @__PURE__ */ React__default.createElement("div", {
      className: "availableBundleContainer"
    }, /* @__PURE__ */ React__default.createElement("h2", null, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "AppDetailPage.availableBundles.heading"
    }, "Available Bundles")), /* @__PURE__ */ React__default.createElement(InAppStore, {
      baseItem: appItem,
      itemType: ItemType.enum.BUNDLE,
      language,
      initialShownItem: 4,
      noItemMessage: /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "AppDetailPage.availableBundles.noItem"
      }, "Looks like there are no items for this game yet.")
    })), !isSingleGameTemplate && /* @__PURE__ */ React__default.createElement("div", {
      className: "itemDetailContent"
    }, /* @__PURE__ */ React__default.createElement("div", {
      className: "description"
    }, /* @__PURE__ */ React__default.createElement(CatalogOverview, {
      item: appItem
    }), additionalAppInfo && /* @__PURE__ */ React__default.createElement(PlatformRequirements, {
      className: "requirementsDesktop",
      additionalAppInfo
    })), /* @__PURE__ */ React__default.createElement("div", {
      className: "detailWidget"
    }, purchaseLogic && purchaseLogic.state.currencyMap && /* @__PURE__ */ React__default.createElement(CatalogTitleWidget, {
      item: appItem
    }, /* @__PURE__ */ React__default.createElement(DiscountCountdownWidget, {
      item: appItem,
      currencyMap: purchaseLogic.state.currencyMap,
      position: "right",
      showDetail: true
    })), /* @__PURE__ */ React__default.createElement("div", {
      className: "protruded"
    }, /* @__PURE__ */ React__default.createElement(CatalogPurchaseWidget, {
      item: appItem,
      itemPublishingContent,
      purchaseLogic,
      additionalAppInfo
    })), additionalAppInfo && /* @__PURE__ */ React__default.createElement(AdditionalDetail, {
      additionalAppInfo
    })), additionalAppInfo && /* @__PURE__ */ React__default.createElement(PlatformRequirements, {
      className: "requirementsMobile",
      additionalAppInfo
    })), appItem.appType === AppType.enum.GAME && !isSingleGameTemplate && /* @__PURE__ */ React__default.createElement(InAppStore, {
      baseItem: appItem,
      itemType: ItemType.enum.APP,
      appType: AppType.enum.DLC,
      language,
      withWrapper: (content) => /* @__PURE__ */ React__default.createElement("div", {
        className: "storeContainer"
      }, /* @__PURE__ */ React__default.createElement("h2", null, /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "AppDetailPage.dlcStore.heading"
      }, "Expansions")), content),
      noItemMessage: /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "AppDetailPage.inAppStore.noItem"
      }, "Looks like there are not any items for this game yet.")
    }), !isSingleGameTemplate && /* @__PURE__ */ React__default.createElement("div", {
      className: "storeContainer"
    }, /* @__PURE__ */ React__default.createElement("h2", null, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "AppDetailPage.inAppStore.heading"
    }, "Game Store")), /* @__PURE__ */ React__default.createElement(InAppStore, {
      baseItem: appItem,
      itemType: ItemType.enum.INGAMEITEM,
      language,
      noItemMessage: /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "AppDetailPage.inAppStore.noItem"
      }, "Looks like there are not any items for this game yet.")
    }))));
  }
}
var AppDetailPage$1 = withPropsGuard(({ itemId, language }) => {
  var _a;
  return /* @__PURE__ */ React__default.createElement(AppDetailPage, {
    itemId,
    language,
    key: (_a = Ctx.getUserManager().state.user) == null ? void 0 : _a.userId
  });
}, (prevProps, nextProps) => prevProps.itemId !== nextProps.itemId || nextProps.language !== prevProps.language);
var AppDetailPage$2 = /* @__PURE__ */ Object.freeze({
  __proto__: null,
  [Symbol.toStringTag]: "Module",
  AppDetailPageLoadingScreen,
  AppDetailPageError,
  "default": AppDetailPage$1
});
export { AppDetailPageLogic as A, AppDetailPageLoadingScreen as a, AppDetailPageError as b, AppDetailPage$2 as c };
