"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = RoomDetails;
const contexts_1 = require("../../../../contexts/index.js");
const hooks_1 = require("../../../../hooks/index.js");
const classnames_1 = __importDefault(require("classnames"));
const react_1 = __importStar(require("react"));
const react_i18next_1 = require("react-i18next");
const AutoAutoHeight_1 = __importDefault(require("../../../../animations/AutoAutoHeight"));
const BEButton_1 = __importDefault(require("../../../generic/BEButton"));
const Currency_1 = __importDefault(require("../../../generic/Currency"));
const Headline_1 = __importDefault(require("../../../generic/Headline"));
const Icon_1 = require("../../../generic/Icon/Icon");
const LineBreak_1 = __importDefault(require("../../../generic/LineBreak"));
const PlanpayPrice_1 = require("../../../generic/PlanpayPrice/PlanpayPrice");
const TaxInclusionNotice_1 = require("../../../generic/TaxInclusionNotice/TaxInclusionNotice");
const Text_1 = __importStar(require("../../../generic/Text"));
const ImageGallerySlider_1 = __importDefault(require("../ImageGallerySlider"));
const RoomDetailsBedsBlock_1 = require("./RoomDetailsBedsBlock");
const RoomDetailsPerkBlock_1 = require("./RoomDetailsPerkBlock");
const RoomRatePills_1 = __importDefault(require("./roomRates/RoomRatePills"));
const RoomRates_1 = __importDefault(require("./roomRates/RoomRates"));
const UnavailableRoom_1 = __importDefault(require("./unavailableRoom/UnavailableRoom"));
const RoomModal_1 = __importDefault(require("../RoomModal"));
const Currency_2 = __importDefault(require("../../../../hooks/Currency"));
const Planpay_1 = __importDefault(require("../../../../hooks/Planpay"));
const useAreaAvailability_1 = require("../../../../hooks/useAreaAvailability");
const WindowSize_1 = require("../../../../hooks/WindowSize");
const FeaturedPromoFeature_1 = __importDefault(require("../../../../providers/feature/FeaturedPromoFeature"));
const ImageProvider_1 = require("../../../../providers/ImageProvider");
const Translation_1 = require("../../../../translations/Translation");
const Color_1 = require("../../../../util/Color");
const currencyHelper = __importStar(require("../../../../util/CurrencyHelper"));
const DataLayer_1 = __importStar(require("../../../../util/DataLayer"));
const ScreenSize_1 = __importDefault(require("../../../../util/ScreenSize"));
const TextAlignment_1 = require("../../../../util/TextAlignment");
function RoomDetails(props) {
    const { room } = props;
    const [featuredRates, setFeaturedRates] = (0, react_1.useState)([]);
    const [standardRates, setStandardRates] = (0, react_1.useState)([]);
    const [viewingRates, setViewingRates] = (0, react_1.useState)(!room.isAvailable());
    const [viewingDescription, setViewingDescription] = (0, react_1.useState)(false);
    const { t } = (0, react_i18next_1.useTranslation)();
    const context = (0, react_1.useContext)(contexts_1.BookingEngineContext);
    const basketContext = (0, react_1.useContext)(contexts_1.BasketContext);
    const ccx = (0, react_1.useContext)(contexts_1.CompanyContext);
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const hotelOverridesContext = (0, react_1.useContext)(contexts_1.HotelOverridesContext);
    const areaAvailability = (0, useAreaAvailability_1.useAreaAvailability)(room);
    const classes = (0, classnames_1.default)('room-details', {
        ['+is-open']: viewingRates,
    });
    (0, react_1.useEffect)(() => {
        var _a;
        if (((_a = basketContext.selectedBasketRow) === null || _a === void 0 ? void 0 : _a.getRoom()) && basketContext.selectedBasketRow.getRoom().code === props.room.code) {
            setViewingRates(true);
        }
        const basketRow = basketContext.selectedBasketRow;
        if (!basketRow) {
            return;
        }
        DataLayer_1.default.instance.addRoomImpression(room, { to: basketRow.getStartDate(), from: basketRow.getEndDate() }, basketRow.getPromoCode());
    }, [basketContext.selectedBasketRow]);
    (0, react_1.useEffect)(() => {
        var _a, _b, _c;
        let nonPromoRates = [];
        const toSetFeaturedRate = [];
        if (FeaturedPromoFeature_1.default.isActive()) {
            for (const currentRate of room.getRates()) {
                if (((_a = currentRate.promoCode) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === ((_c = (_b = basketContext.selectedBasketRow) === null || _b === void 0 ? void 0 : _b.getPromoCode()) === null || _c === void 0 ? void 0 : _c.toLowerCase())) {
                    toSetFeaturedRate.unshift(currentRate);
                }
                else {
                    nonPromoRates.push(currentRate);
                }
            }
            if (toSetFeaturedRate.length > 0) {
                setViewingRates(true);
                setFeaturedRates(toSetFeaturedRate);
            }
        }
        else {
            nonPromoRates = room.getRates();
        }
        setStandardRates(nonPromoRates);
    }, [room.getRates()]);
    const trimmedDescription = room.getShortDescription();
    const { isMobile } = (0, WindowSize_1.useWindowSize)();
    const ratesOpen = viewingRates || (context.screenSize <= ScreenSize_1.default.Medium && viewingRates === null && !(hotel === null || hotel === void 0 ? void 0 : hotel.mobileDefaultRatesClosed));
    const moreDetailsOnClick = () => {
        DataLayer_1.default.instance.sendInteraction('More Room Details', DataLayer_1.InteractionType.LINK, DataLayer_1.InteractionStep.ROOMS);
        setViewingDescription(true);
    };
    const sendRoomClickOnClick = () => {
        if (!ratesOpen) {
            const basketRow = basketContext.selectedBasketRow;
            if (basketRow) {
                DataLayer_1.default.instance.sendRoomClick(room, basketRow.getStartDate(), basketRow.getEndDate(), basketRow.getPromoCode());
            }
        }
        setViewingRates(!ratesOpen);
    };
    const closeDescriptionOnClose = () => {
        setViewingDescription(false);
    };
    const lowestRawPrice = room.getLowestPrice(false, true);
    const lowestDisplayPrice = hotelOverridesContext.alwaysShowDecimals ? lowestRawPrice : Math.round(lowestRawPrice);
    const currencySize = context.screenSize > ScreenSize_1.default.Medium && lowestDisplayPrice.toLocaleString().length < 7 ? 'large' : 'normal';
    const convertCurrencyPrice = (value) => Math.round(currencyHelper.convertPrice(value, true, ccx.currentCurrency, hotel === null || hotel === void 0 ? void 0 : hotel.defaultCurrency));
    const { value: price, isLoading } = (0, Currency_2.default)({
        hideDecimals: true,
        children: lowestDisplayPrice,
    });
    const selectedRow = basketContext.selectedBasketRow;
    const { hasPricePreview } = (0, Planpay_1.default)();
    const { roomPerks } = room.getRoomPerks({
        hotel: hotel,
        room: room,
        perksLimitNumber: 5,
    });
    return (react_1.default.createElement("div", { className: `room-details-container ${props.disableMarginBottom ? '' : 'u-marg-bottom'}` },
        react_1.default.createElement("div", { className: classes },
            react_1.default.createElement("div", { className: "room-details--image" },
                react_1.default.createElement(ImageGallerySlider_1.default, { images: props.room.getImages(), imageSize: ImageProvider_1.ImageSize.Medium })),
            react_1.default.createElement("div", { className: "room-details--content u-pad--heavy u-pad@l-" },
                react_1.default.createElement("div", { className: "u-marg-bottom" },
                    context.screenSize > ScreenSize_1.default.Large && (react_1.default.createElement(react_1.default.Fragment, null,
                        react_1.default.createElement("div", { className: "u-marg-bottom" },
                            react_1.default.createElement(RoomRatePills_1.default, { room: room, limit: 2 })))),
                    react_1.default.createElement("div", { className: "room-details--content-title u-marg-bottom--light" },
                        react_1.default.createElement(Headline_1.default, { bold: true }, room.name)),
                    react_1.default.createElement(RoomDetailsBedsBlock_1.RoomDetailsBedsBlock, { room: room }),
                    (trimmedDescription === null || trimmedDescription === void 0 ? void 0 : trimmedDescription.replace(/<br(\/|)>/g, '')) && (react_1.default.createElement("div", { className: "room-details--content-description u-marg-bottom--light" },
                        react_1.default.createElement(Text_1.default, { color: Color_1.Color.DarkGrey, type: Text_1.TextType.Small },
                            react_1.default.createElement("span", { dangerouslySetInnerHTML: { __html: (trimmedDescription === null || trimmedDescription === void 0 ? void 0 : trimmedDescription.replace(/<br(\/|)>/g, '')) || '' } }))))),
                react_1.default.createElement("div", { className: "row" },
                    !(hotel === null || hotel === void 0 ? void 0 : hotel.perksHideOnRoomRow) && (react_1.default.createElement("div", { className: "col-12 u-flex u-marg-bottom@m- flex-column align-items-start" },
                        react_1.default.createElement("div", { className: "room-details--content-perks u-marg-bottom@m-" },
                            isMobile && (react_1.default.createElement("div", { className: "u-marg-bottom" },
                                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, bold: true, color: Color_1.Color.Accent }, (hotel === null || hotel === void 0 ? void 0 : hotel.perkTitle) || t(Translation_1.Translation.Misc.Perks.AndYouGetAll)))),
                            react_1.default.createElement(RoomDetailsPerkBlock_1.RoomDetailsPerkBlock, { perks: roomPerks, room: room })))),
                    (hotel === null || hotel === void 0 ? void 0 : hotel.showDescriptionOnRoomDetails) && (react_1.default.createElement("div", { className: "col-12" },
                        react_1.default.createElement(LineBreak_1.default, { noMargins: true }),
                        react_1.default.createElement("div", { className: "u-marg-top u-text-overflow-ellipsis-3 u-marg-bottom" },
                            react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.Graphite },
                                react_1.default.createElement("span", { dangerouslySetInnerHTML: { __html: props.room.getLongDescription() || '' } }))))),
                    react_1.default.createElement("div", { className: "col-md-6 u-flex u-marg-bottom@m- flex-column align-items-start" },
                        react_1.default.createElement(BEButton_1.default, { icon: Icon_1.IconType.ArrowRight2, isText: true, primary: true, size: "tiny", iconPosition: "right", onClick: moreDetailsOnClick }, t(Translation_1.Translation.Step.Room.RoomInfo.MoreDetails))),
                    room.isAvailable() && (react_1.default.createElement("div", { className: "col-md-6 u-flex justify-content-end flex-column align-items-start align-items-md-end" },
                        react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.Navy, inline: true }, t(Translation_1.Translation.Misc.From)),
                        react_1.default.createElement("div", { className: "room-details--content-price flex-row align-items-end" },
                            react_1.default.createElement(Headline_1.default, { size: currencySize, bold: true },
                                react_1.default.createElement(Currency_1.default, { hideDecimals: true }, lowestDisplayPrice)),
                            react_1.default.createElement(Text_1.default, { color: Color_1.Color.DarkGrey, inline: true },
                                "/",
                                t(Translation_1.Translation.Misc.Night))),
                        react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.Grey, className: "u-nowrap", align: isMobile ? TextAlignment_1.TextAlign.Left : TextAlignment_1.TextAlign.Right },
                            react_1.default.createElement(TaxInclusionNotice_1.TaxInclusionNotice, { room: room }))))),
                react_1.default.createElement("div", { className: "u-flex-direction-column u-flex u-flex-align-flex-end u-flex-align-flex-start@m- u-w-100@m-" },
                    !isLoading && hasPricePreview ? (react_1.default.createElement("div", { className: "u-marg-top--light" },
                        react_1.default.createElement(PlanpayPrice_1.PlanpayPrice, { price: price, checkin: ((selectedRow === null || selectedRow === void 0 ? void 0 : selectedRow.getStartDate()) || basketContext.endDate).format('YYYY-MM-DD'), currency: ccx.currentCurrency }))) : null,
                    react_1.default.createElement(BEButton_1.default, { className: "u-marg-top--light", name: "ViewRatesNowButton", primary: true, primaryActive: !!ratesOpen, filled: true, stopIconAnimation: true, icon: ratesOpen ? Icon_1.IconType.ArrowUp2 : Icon_1.IconType.ArrowDown2, iconPosition: "right", onClick: sendRoomClickOnClick, wide: context.screenSize <= ScreenSize_1.default.Large, textColor: (0, Color_1.getContrastTextColor)(ratesOpen ? hotel === null || hotel === void 0 ? void 0 : hotel.colors.accent3 : hotel === null || hotel === void 0 ? void 0 : hotel.colors.accent) }, ratesOpen ? t(Translation_1.Translation.Step.Room.RoomInfo.CloseRates) : t(Translation_1.Translation.Step.Room.RoomInfo.ViewRates))))),
        react_1.default.createElement(AutoAutoHeight_1.default, { open: !!ratesOpen }, room.isAvailable() && areaAvailability ? (react_1.default.createElement(RoomRates_1.default, { roomCode: room.code, rates: standardRates, featuredRates: featuredRates, bookNowButtonDisabledReason: props.bookNowButtonDisabledReason })) : (react_1.default.createElement(UnavailableRoom_1.default, { room: room, hideFindAvailableRooms: room.isAvailable() && !areaAvailability }))),
        react_1.default.createElement(RoomModal_1.default, { open: viewingDescription, room: props.room, rates: standardRates, featuredRates: featuredRates, onClose: closeDescriptionOnClose }),
        room.getRates().map((rate) => {
            return (react_1.default.createElement("script", { key: room.code + '-' + rate.code, type: "application/ld+json" }, JSON.stringify({
                '@context': 'http://schema.org/',
                '@type': 'Offer',
                itemOffered: {
                    '@type': 'HotelRoom',
                    name: room.name,
                },
                name: rate.name,
                identifier: rate.code,
                priceSpecification: {
                    '@type': 'UnitPriceSpecification',
                    price: convertCurrencyPrice(rate.getAveragePrice(true)),
                    basePrice: convertCurrencyPrice(rate.getTotalPrice(true)),
                    totalPrice: convertCurrencyPrice(rate.getAveragePrice()),
                    priceCurrency: ccx.currentCurrency,
                    unitText: 'Nightly',
                },
            })));
        })));
}
//# sourceMappingURL=RoomDetails.js.map