import { O as OrderAppManager, f as OpenLibraryLauncherButton, 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 { T as Trans, a3 as Dialog, a4 as DialogBody, a5 as DialogFooter, a2 as EcomPath, C as Container, ac as EligibleUser, E as ErrorHelper, a as Ctx, L as Loading, w as LoadingSize, q as EcommerceErrorCode, ad as gaSendErrorEvent, r as ErrorPath, F as FeUtils, 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 { P as PlatformRequirements } from "./PlatformRequirements.js";
import { ItemDetailPageError } from "./InGameItemDetailPage.js";
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 "./Radio.js";
import "./validator.js";
import "./Tabs.js";
import "./CommonError.js";
import "./starBgGradient.js";
import "./CommonError2.js";
var PurchaseBaseAppButton$1 = "";
const PurchaseBaseAppDialog = ({ baseApp, onClose }) => {
  return /* @__PURE__ */ React__default.createElement(Dialog, {
    isOpen: true,
    className: "purchaseBaseAppDialog"
  }, /* @__PURE__ */ React__default.createElement(DialogBody, null, /* @__PURE__ */ React__default.createElement(Trans, {
    i18nKey: "PurchaseBaseApp.lockedDownloadableContent.content"
  }, "This expansion requires the base game ", { appTitle: baseApp.title }, " to play")), /* @__PURE__ */ React__default.createElement(DialogFooter, {
    className: "dialogFooter"
  }, /* @__PURE__ */ React__default.createElement("button", {
    onClick: onClose,
    className: "btn small btn-ghost btnCancel"
  }, /* @__PURE__ */ React__default.createElement(Trans, {
    i18nKey: "PurchaseBaseApp.cancel"
  }, "Cancel")), /* @__PURE__ */ React__default.createElement(Link, {
    to: EcomPath.GameItem.makePath({
      itemId: baseApp.itemId
    }),
    className: "btn small btn-primary btnGoToGamePage"
  }, /* @__PURE__ */ React__default.createElement(Trans, {
    i18nKey: "PurchaseBaseApp.goToGamePage"
  }, "Go to the game page"))));
};
class PurchaseBaseAppButton extends React__default.Component {
  constructor(props) {
    super(props);
    this.state = {
      isDownloadBaseAppDialogOpen: false
    };
  }
  render() {
    const { baseApp } = this.props;
    const { isDownloadBaseAppDialogOpen } = this.state;
    return /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, /* @__PURE__ */ React__default.createElement("button", {
      className: "btn big btn-primary btnGoToLibrary",
      onClick: () => this.setState({ isDownloadBaseAppDialogOpen: true })
    }, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "PurchaseBaseApp.goToLibrary"
    }, "GO TO LIBRARY")), isDownloadBaseAppDialogOpen && /* @__PURE__ */ React__default.createElement(PurchaseBaseAppDialog, {
      baseApp,
      onClose: () => this.setState({ isDownloadBaseAppDialogOpen: false })
    }));
  }
}
var DlcDetailPage$2 = "";
const ENTITLEMENT_NOT_FOUND = 31142;
class DlcDetailPageLogic extends Container {
  constructor(props) {
    super();
    this.fetchBaseAppEntitlement = async (baseAppId) => {
      const { user } = this.props;
      if (!EligibleUser.safeParse(user).success || !(user == null ? void 0 : user.userId))
        return null;
      const { error } = await this.sdk.Platform.Entitlement().getEntitlementByAppId({ userId: user.userId, appId: baseAppId });
      if (!error) {
        return true;
      }
      if (ErrorHelper.extractServiceErrorCode(error) === ENTITLEMENT_NOT_FOUND) {
        return false;
      }
      throw error;
    };
    this.fetchAdditionalAppInfo = async () => {
      return await this.sdk.Platform.Item().getAppInfoByItemId({ itemId: this.props.itemId }).then((result) => {
        if (result.error)
          throw result.error;
        return result.response.data;
      });
    };
    this.props = props;
    this.state = {
      dlcItem: null,
      additionalAppInfo: null,
      baseApp: null,
      isBaseAppOwned: 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,
        dlcItem: null,
        baseApp: null,
        error: null
      });
      const [dlcItem, additionalAppInfo] = await Promise.all([this.fetchDlcItem(), this.fetchAdditionalAppInfo()]);
      if (!dlcItem.appId) {
        throw new NoAppIdError("");
      }
      await this.initPurchaseLogic(dlcItem);
      const { baseAppId } = dlcItem;
      if (!baseAppId) {
        throw new NoBaseAppIdError("");
      }
      const [baseApp, isBaseAppOwned] = await Promise.all([this.fetchBaseApp(baseAppId), this.fetchBaseAppEntitlement(baseAppId)]);
      await this.setState({
        dlcItem,
        baseApp,
        isBaseAppOwned,
        additionalAppInfo,
        itemPublishingContent: null
      });
    } catch (error) {
      console.error(error);
      this.setState({ error });
    } finally {
      await this.setState({ isFetching: false });
    }
  }
  async fetchDlcItem() {
    return await this.sdk.Platform.Item().getItemsByItemIdLocale({ itemId: this.props.itemId }).then((result) => {
      if (result.error)
        throw result.error;
      return result.response.data;
    });
  }
  async fetchBaseGameId(dlcAppId) {
    return await this.sdk.BuildInfo.DLC().getBaseGamesByDlcAppId(dlcAppId).then((result) => {
      if (result.error)
        throw result.error;
      const baseGame = result.response.data[0];
      if (!baseGame || !baseGame.appId) {
        throw new NoBaseAppIdError("");
      }
      return baseGame.appId;
    });
  }
  async fetchBaseApp(baseAppId) {
    return await this.sdk.Platform.Item().getItemByAppId({ appId: baseAppId }).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 NoAppIdError extends Error {
  constructor(message) {
    super(message);
    Object.setPrototypeOf(this, NoAppIdError.prototype);
  }
}
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"
}));
class DlcDetailPage extends React__default.Component {
  constructor(props) {
    super(props);
    this.openLightbox = (selectedImagePosition) => {
      this.setState({ isLightboxOpen: true, selectedImagePosition });
    };
    this.closeLightbox = () => {
      this.setState({ isLightboxOpen: false });
    };
    this.renderLauncherGoToLibraryButton = () => {
      const { dlcItem, baseApp, isBaseAppOwned } = this.logic.state;
      if (!dlcItem) {
        return "";
      }
      if (!baseApp || isBaseAppOwned) {
        return /* @__PURE__ */ React__default.createElement(OpenLibraryLauncherButton, {
          item: baseApp || dlcItem
        });
      }
      return /* @__PURE__ */ React__default.createElement(PurchaseBaseAppButton, {
        baseApp
      });
    };
    const { itemId, country, language, user } = props;
    this.logic = new DlcDetailPageLogic({ 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, dlcItem, error, baseApp, additionalAppInfo, 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(dlcItem, itemPublishingContent);
    return /* @__PURE__ */ React__default.createElement("main", {
      className: "itemDetailPage"
    }, /* @__PURE__ */ React__default.createElement(ItemDetailPurchaseAlert, {
      ...{
        purchaseLogic,
        user,
        isFetching
      }
    }), /* @__PURE__ */ React__default.createElement(HelmetExport, null, dlcItem && /* @__PURE__ */ React__default.createElement("title", null, FeUtils.makeTitleWithAppName(dlcItem.title))), isFetching && /* @__PURE__ */ React__default.createElement(ItemDetailPageLoadingScreen, null), error && /* @__PURE__ */ React__default.createElement(ItemDetailPageError, {
      error
    }), dlcItem && baseApp && /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, /* @__PURE__ */ React__default.createElement(CatalogItemBanner, {
      item: dlcItem,
      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), dlcItem.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(CatalogOverview, {
      item: dlcItem
    }), additionalAppInfo && /* @__PURE__ */ React__default.createElement(PlatformRequirements, {
      className: "requirementsDesktop",
      additionalAppInfo
    })), /* @__PURE__ */ React__default.createElement("div", {
      className: "detailWidget"
    }, /* @__PURE__ */ React__default.createElement(CatalogTitleWidget, {
      item: dlcItem
    }, currencyMap && /* @__PURE__ */ React__default.createElement(DiscountCountdownWidget, {
      item: dlcItem,
      currencyMap,
      position: "right",
      showDetail: true
    })), /* @__PURE__ */ React__default.createElement("div", {
      className: "protruded"
    }, /* @__PURE__ */ React__default.createElement(CatalogPurchaseWidget, {
      item: dlcItem,
      purchaseLogic,
      additionalAppInfo,
      renderLauncherGoToLibraryButton: this.renderLauncherGoToLibraryButton
    })), /* @__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: dlcItem,
      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 DlcDetailPage$1 = withPropsGuard(({ itemId, language }) => {
  const user = Ctx.getAppState().getUser() || void 0;
  const country = user && user.country || void 0;
  return /* @__PURE__ */ React__default.createElement(DlcDetailPage, {
    itemId,
    country,
    user,
    language
  });
}, (prevProps, nextProps) => prevProps.itemId !== nextProps.itemId || nextProps.language !== prevProps.language);
export { DlcDetailPage, ItemDetailPageLoadingScreen, DlcDetailPage$1 as default };
