"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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const contexts_1 = require("../../../contexts/index.js");
const dayjs_1 = __importDefault(require("dayjs"));
const react_1 = __importStar(require("react"));
const react_i18next_1 = require("react-i18next");
const react_router_dom_1 = require("react-router-dom");
const Translation_1 = require("translations/Translation");
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 SmallSpinner_1 = __importDefault(require("../../generic/loader/SmallSpinner"));
const Placeholder_1 = __importDefault(require("../../generic/Placeholder"));
const TaxInclusionNotice_1 = require("../../generic/TaxInclusionNotice/TaxInclusionNotice");
const Text_1 = __importStar(require("../../generic/Text"));
const Tooltip_1 = __importDefault(require("../../generic/Tooltip/Tooltip"));
const HotelCardModal_1 = __importDefault(require("./HotelCardModal"));
const ImageGallerySlider_1 = __importDefault(require("../room/ImageGallerySlider"));
const hooks_1 = require("../../../hooks");
const BasketRow_1 = __importDefault(require("../../../models/BasketRow"));
const Color_1 = require("../../../util/Color");
const GetStepRoom_1 = __importDefault(require("../../../util/GetStepRoom"));
const HotelPerkHelper_1 = require("../../../util/HotelPerkHelper");
const ScreenSize_1 = __importDefault(require("../../../util/ScreenSize"));
const HotelCard = ({ hotel }) => {
    var _a, _b, _c, _d;
    const beContext = (0, react_1.useContext)(contexts_1.BookingEngineContext);
    const ccx = (0, react_1.useContext)(contexts_1.CompanyContext);
    const { hotel: currentHotel } = (0, hooks_1.useCurrentHotel)();
    const basketContext = (0, contexts_1.useBasket)();
    const navigate = (0, react_router_dom_1.useNavigate)();
    const [images, setImages] = (0, react_1.useState)([]);
    const [modalOpen, setModalOpen] = (0, react_1.useState)(false);
    const hotelCardRef = (0, react_1.useRef)(null);
    const currentPrice = (_a = hotel.lowestPrice) === null || _a === void 0 ? void 0 : _a.value;
    const currentPriceLoading = (_b = hotel.lowestPrice) === null || _b === void 0 ? void 0 : _b.isLoading;
    const showPrice = currentPriceLoading || (!currentPriceLoading && currentPrice > 0);
    const { t } = (0, react_i18next_1.useTranslation)();
    (0, react_1.useEffect)(() => {
        const newImages = [];
        if (hotel.heroImage) {
            newImages.push(hotel.heroImage);
        }
        if (hotel.rooms) {
            for (const room of Object.values(hotel.rooms)) {
                room.images.forEach((image) => {
                    newImages.push(image);
                });
            }
        }
        setImages(newImages);
    }, [hotel.rooms, hotel.heroImage, setImages]);
    const [openPerk, setOpenPerk] = (0, react_1.useState)((_d = (_c = hotel.perks) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.name);
    const colorProfile = hotel.colors;
    const onViewRoomsClicked = () => __awaiter(void 0, void 0, void 0, function* () {
        var _a;
        if ((currentHotel === null || currentHotel === void 0 ? void 0 : currentHotel.hotelID) !== hotel.hotelID) {
            const appliedBasketRow = basketContext.selectedBasketRow || basketContext.currentBasketRows[basketContext.currentBasketRows.length - 1];
            const row = new BasketRow_1.default();
            row.setStartDate((appliedBasketRow === null || appliedBasketRow === void 0 ? void 0 : appliedBasketRow.getStartDate()) || (0, dayjs_1.default)().add(1, 'day'));
            row.setEndDate((appliedBasketRow === null || appliedBasketRow === void 0 ? void 0 : appliedBasketRow.getEndDate()) || (0, dayjs_1.default)().add(2, 'day'));
            row.setAdults((appliedBasketRow === null || appliedBasketRow === void 0 ? void 0 : appliedBasketRow.getAdults()) || 1);
            row.setChildren((appliedBasketRow === null || appliedBasketRow === void 0 ? void 0 : appliedBasketRow.getChildren()) || 0);
            row.setInfants((appliedBasketRow === null || appliedBasketRow === void 0 ? void 0 : appliedBasketRow.getInfants()) || 0);
            row.setPromoCode((_a = appliedBasketRow.getPromoCode()) !== null && _a !== void 0 ? _a : '');
            basketContext.initBasketRows([row]);
        }
        ccx.changeHotel(hotel);
        navigate((0, GetStepRoom_1.default)().getStepUrl());
    });
    const onMoreDetailsClicked = () => __awaiter(void 0, void 0, void 0, function* () {
        setModalOpen(true);
    });
    const onModalClose = () => {
        setModalOpen(false);
    };
    const isLoading = !hotel.hasLoadedDetails;
    return (0, react_1.useMemo)(() => {
        var _a, _b, _c, _d, _e, _f, _g;
        const content = isLoading ? (react_1.default.createElement("div", null,
            react_1.default.createElement("div", { className: "d-flex flex-column" },
                react_1.default.createElement(Placeholder_1.default, { type: "block", blockSize: "sm", rounded: "lg", blink: true }),
                react_1.default.createElement("div", { className: "u-pad" },
                    react_1.default.createElement(Placeholder_1.default, { type: "text", w: "md", h: "md", blink: true }),
                    react_1.default.createElement(Placeholder_1.default, { type: "text", w: "md", h: "md", blink: true }),
                    react_1.default.createElement("div", { className: "u-marg-top" },
                        react_1.default.createElement("div", { className: "u-flex justify-space-between" },
                            react_1.default.createElement(Placeholder_1.default, { type: "block", blockSize: "xs", rounded: "lg", blink: true }),
                            react_1.default.createElement(Placeholder_1.default, { type: "block", blockSize: "xs", rounded: "lg", blink: true }),
                            react_1.default.createElement(Placeholder_1.default, { type: "block", blockSize: "xs", rounded: "lg", blink: true }))))))) : (react_1.default.createElement("div", { className: "large-hotel-card u-rounded h-100 justify-items-between" },
            react_1.default.createElement("div", { className: "large-hotel-card--image" },
                react_1.default.createElement(ImageGallerySlider_1.default, { images: images })),
            react_1.default.createElement("div", { className: "large-hotel-card--body u-pad--heavy u-pad-bottom--none container" },
                react_1.default.createElement(Headline_1.default, { bold: true }, hotel === null || hotel === void 0 ? void 0 : hotel.name),
                react_1.default.createElement(Text_1.default, { color: Color_1.Color.DarkGrey, bold: true, className: "u-marg-bottom--light" }, (_a = hotel === null || hotel === void 0 ? void 0 : hotel.address) === null || _a === void 0 ? void 0 : _a.city),
                react_1.default.createElement("div", { className: "u-flex u-marg-bottom" },
                    react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, [(_b = hotel === null || hotel === void 0 ? void 0 : hotel.address) === null || _b === void 0 ? void 0 : _b.line1, (_c = hotel === null || hotel === void 0 ? void 0 : hotel.address) === null || _c === void 0 ? void 0 : _c.line2, (_d = hotel === null || hotel === void 0 ? void 0 : hotel.address) === null || _d === void 0 ? void 0 : _d.state].filter((line) => !!line).join(', '))),
                react_1.default.createElement("div", { className: "u-marg-bottom--medium large-hotel-card--description" },
                    react_1.default.createElement(Text_1.default, { className: "text", type: Text_1.TextType.Small },
                        react_1.default.createElement("span", { dangerouslySetInnerHTML: { __html: hotel.description || '' } }))),
                !!((_e = hotel.perks) === null || _e === void 0 ? void 0 : _e.length) && (react_1.default.createElement(Text_1.default, { className: "u-marg-bottom", bold: true, type: Text_1.TextType.Small, color: Color_1.Color.Accent }, "And these come free...")),
                react_1.default.createElement("div", { className: "u-flex u-flex-align-center u-marg-bottom--medium" },
                    !!((_f = hotel.perks) === null || _f === void 0 ? void 0 : _f.length) && (react_1.default.createElement("div", { className: "u-flex" }, hotel.perks.slice(0, 5).map((perk) => {
                        var _a;
                        const openPerkOnClick = () => {
                            setOpenPerk(perk.name);
                        };
                        const perkIconElement = (0, HotelPerkHelper_1.getPerkIconElement)(perk);
                        return (react_1.default.createElement("div", { className: "u-flex u-flex-align-center", key: perk.name },
                            react_1.default.createElement(Tooltip_1.default, { title: (_a = perk.tooltip) !== null && _a !== void 0 ? _a : perk.name, overrideContainer: hotelCardRef === null || hotelCardRef === void 0 ? void 0 : hotelCardRef.current, dark: (0, Color_1.lightOrDark)(hotel.colors.accent2) === 'dark' },
                                react_1.default.createElement("div", { onClick: openPerkOnClick, style: { backgroundColor: Color_1.Color.Snow }, className: "u-rounded u-pad--light u-marg-right--light d-flex justify-center" },
                                    react_1.default.createElement("div", { className: "u-mw-16 d-flex justify-content-center" }, perkIconElement))),
                            react_1.default.createElement("div", { className: 'u-marg-right u-none@l- u-block@m- ' +
                                    (beContext.screenSize > ScreenSize_1.default.Medium ||
                                        beContext.screenSize <= ScreenSize_1.default.Mobile ||
                                        (beContext.screenSize > ScreenSize_1.default.Mobile && beContext.screenSize <= ScreenSize_1.default.Medium && openPerk !== perk.name)
                                        ? 'u-none-2'
                                        : '') },
                                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, perk.name))));
                    }))),
                    react_1.default.createElement(BEButton_1.default, { icon: Icon_1.IconType.ArrowRight2, isText: true, primary: true, size: "tiny", iconPosition: "right", onClick: onMoreDetailsClicked }, t(Translation_1.Translation.Step.Room.RoomInfo.MoreDetails)))),
            react_1.default.createElement("div", { className: "u-pad--heavy u-pad-top--none h-100 justify-content-end u-flex flex-column u-flex-align-flex-start align-items-md-start no-gutters" },
                showPrice ? (react_1.default.createElement(react_1.default.Fragment, null,
                    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: "u-flex u-flex-flex-start u-flex-align-center flex-row" },
                        react_1.default.createElement(Headline_1.default, { bold: true, className: "u-flex" }, ((_g = hotel.lowestPrice) === null || _g === void 0 ? void 0 : _g.isLoading) ? (react_1.default.createElement(SmallSpinner_1.default, null)) : (react_1.default.createElement(Currency_1.default, { hideDecimals: true, fromCurrencyCode: hotel.defaultCurrency }, currentPrice))),
                        react_1.default.createElement(Text_1.default, { color: Color_1.Color.DarkGrey, inline: true },
                            "/",
                            t(Translation_1.Translation.Misc.Night))),
                    react_1.default.createElement("div", { className: "u-marg-bottom" },
                        react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.Grey, inline: true, className: "u-nowrap" },
                            react_1.default.createElement(TaxInclusionNotice_1.TaxInclusionNotice, { hasTax: false }))))) : (react_1.default.createElement("div", { className: "u-h-fill d-flex align-items-center" },
                    react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.Alert }, t(Translation_1.Translation.Step.Hotel.HotelCard.DatesUnavailable)))),
                react_1.default.createElement("div", { className: "u-marg-top--light u-flex justify-content-end u-w-100@m-" },
                    react_1.default.createElement(BEButton_1.default, { name: "ViewRatesNowButton", primary: true, primaryActive: true, filled: true, icon: Icon_1.IconType.ArrowRight, iconPosition: "right", wide: beContext.screenSize <= ScreenSize_1.default.Medium, onClick: onViewRoomsClicked }, t(Translation_1.Translation.Step.Hotel.ViewRooms)))),
            react_1.default.createElement(HotelCardModal_1.default, { open: modalOpen, onClose: onModalClose, hotel: hotel, container: hotelCardRef === null || hotelCardRef === void 0 ? void 0 : hotelCardRef.current })));
        return (react_1.default.createElement("span", { ref: hotelCardRef, id: `hotel-card-${hotel.hotelID}` },
            react_1.default.createElement("style", null, `#hotel-card-modal-${hotel.hotelID},
                #hotel-card-${hotel.hotelID} {
                    ${(0, Color_1.getAccentOverrideStyleContent)(colorProfile)}
                }`),
            content));
    }, [
        isLoading,
        images,
        hotel === null || hotel === void 0 ? void 0 : hotel.hotelID,
        hotel === null || hotel === void 0 ? void 0 : hotel.address,
        hotel === null || hotel === void 0 ? void 0 : hotel.perks,
        hotel === null || hotel === void 0 ? void 0 : hotel.lowestPrice,
        hotel === null || hotel === void 0 ? void 0 : hotel.colors,
        currentPrice,
        currentPriceLoading,
        modalOpen,
        hotelCardRef,
        openPerk,
        showPrice,
    ]);
};
exports.default = HotelCard;
//# sourceMappingURL=HotelCard.js.map