"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 = RoomList;
const contexts_1 = require("../../../contexts/index.js");
const hooks_1 = require("../../../hooks/index.js");
const react_1 = __importStar(require("react"));
const LargeLoader_1 = __importDefault(require("../../generic/loader/LargeLoader"));
const RoomDetails_1 = __importDefault(require("./roomDetails/RoomDetails"));
const RoomListCrossSellBlock_1 = __importDefault(require("./RoomListCrossSellBlock"));
const RoomRateAvailabilityListFromApi_1 = require("../../../hooks/RoomRateAvailabilityListFromApi");
const RoomSortProvider_1 = __importDefault(require("../../../providers/RoomSortProvider"));
const DataLayer_1 = __importDefault(require("../../../util/DataLayer"));
const ScreenSize_1 = __importDefault(require("../../../util/ScreenSize"));
const NoRoomsFoundBlock_1 = __importDefault(require("./NoRoomsFoundBlock"));
const RoomDetailsRetargeting_1 = require("./roomDetails/RoomDetailsRetargeting");
const RoomListAlert_1 = require("./RoomListAlert");
const StepRoomErrorForm_1 = require("./StepRoomErrorForm");
function RoomList() {
    const basketContext = (0, react_1.useContext)(contexts_1.BasketContext);
    const beContext = (0, react_1.useContext)(contexts_1.BookingEngineContext);
    const { roomRateCalendarInteractivity } = beContext;
    const { orderUnavailableRoomsLast } = (0, react_1.useContext)(contexts_1.HotelOverridesContext);
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const { rooms, loadingAttempted, isLoading, openRoom, apiHasError, retargetingRoom } = (0, RoomRateAvailabilityListFromApi_1.useRoomRateAvailabilityListFromApi)();
    const roomListRef = (0, react_1.useRef)(null);
    const timeoutRef = (0, react_1.useRef)(null);
    const sortBy = beContext.currentRoomSort;
    const listWhenLastScrolledRef = (0, react_1.useRef)(null);
    const scrollIntoView = () => {
        if (roomListRef.current && listWhenLastScrolledRef.current !== rooms) {
            listWhenLastScrolledRef.current = rooms;
            const offset = beContext.screenSize <= ScreenSize_1.default.Medium ? 185 : 350;
            window.scrollTo({
                top: window.scrollY + (roomListRef.current.getBoundingClientRect().top - offset),
                behavior: 'smooth',
            });
        }
    };
    (0, react_1.useEffect)(() => {
        return () => {
            // Clear timeout on unmount or before next effect
            if (timeoutRef.current) {
                clearTimeout(timeoutRef.current);
            }
        };
    }, []);
    return (0, react_1.useMemo)(() => {
        var _a;
        const sortedRooms = [...rooms];
        if (sortedRooms.length !== 0) {
            sortedRooms.sort(RoomSortProvider_1.default.getSortByFunction(sortBy));
        }
        if (hotel === null || hotel === void 0 ? void 0 : hotel.showUnavailableRooms) {
            if (orderUnavailableRoomsLast) {
                sortedRooms.sort((a, b) => {
                    if (a.isAvailable() && !b.isAvailable())
                        return -1;
                    if (!a.isAvailable() && b.isAvailable())
                        return 1;
                    return 0;
                });
            }
        }
        const hasRoomRateModalOpening = !!((roomRateCalendarInteractivity === null || roomRateCalendarInteractivity === void 0 ? void 0 : roomRateCalendarInteractivity.rateCode) && (roomRateCalendarInteractivity === null || roomRateCalendarInteractivity === void 0 ? void 0 : roomRateCalendarInteractivity.roomCode));
        const hasLoading = isLoading || !loadingAttempted;
        if (hasRoomRateModalOpening) {
            listWhenLastScrolledRef.current = rooms;
        }
        if (!basketContext.isDatePickerOpen && !isLoading && sortedRooms.length > 0) {
            if (timeoutRef.current) {
                clearTimeout(timeoutRef.current);
            }
            timeoutRef.current = setTimeout(() => {
                if (!hasRoomRateModalOpening) {
                    scrollIntoView();
                }
            }, 100);
        }
        const elements = (react_1.default.createElement("div", { ref: roomListRef, className: "room-list" }, hasLoading && !hasRoomRateModalOpening ? (react_1.default.createElement(LargeLoader_1.default, null)) : sortedRooms.length ? (react_1.default.createElement("div", { className: "room-list__container" },
            hasLoading && hasRoomRateModalOpening ? react_1.default.createElement(LargeLoader_1.default, null) : react_1.default.createElement(react_1.default.Fragment, null),
            sortedRooms.map((room, index) => (react_1.default.createElement(RoomDetails_1.default, { key: room.code, room: room, disableMarginBottom: index === sortedRooms.length - 1 }))))) : (react_1.default.createElement(react_1.default.Fragment, null,
            apiHasError ? react_1.default.createElement(StepRoomErrorForm_1.StepRoomErrorForm, null) : react_1.default.createElement(NoRoomsFoundBlock_1.default, null), (_a = hotel === null || hotel === void 0 ? void 0 : hotel.crossSellHotelIds) === null || _a === void 0 ? void 0 :
            _a.map((hotelId) => (react_1.default.createElement(RoomListCrossSellBlock_1.default, { key: hotelId, hotelId: hotelId })))))));
        DataLayer_1.default.instance.sendRoomImpressions();
        return (react_1.default.createElement(react_1.default.Fragment, null,
            react_1.default.createElement(RoomListAlert_1.RoomListAlert, null, !isLoading && retargetingRoom ? react_1.default.createElement(RoomDetailsRetargeting_1.RoomDetailsRetargeting, { key: retargetingRoom.code, room: retargetingRoom }) : null),
            elements));
    }, [
        rooms,
        isLoading,
        loadingAttempted,
        openRoom,
        sortBy,
        beContext.currentFilters,
        basketContext.loadedPromotion,
        basketContext.isDatePickerOpen,
        orderUnavailableRoomsLast,
        roomRateCalendarInteractivity,
    ]);
}
//# sourceMappingURL=RoomList.js.map