"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.TabGroupedRooms = void 0;
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 Tab_1 = require("../../../generic/Tabs/Tab");
const Tabs_1 = __importDefault(require("../../../generic/Tabs/Tabs"));
const NoRoomsFoundBlock_1 = __importDefault(require("../NoRoomsFoundBlock"));
const RoomListCrossSellBlock_1 = __importDefault(require("../RoomListCrossSellBlock"));
const StepRoomErrorForm_1 = require("../StepRoomErrorForm");
const TabGroupedRoomList_1 = require("./TabGroupedRoomList");
const Query_1 = require("../../../../hooks/Query");
const RoomRateAvailabilityListFromApi_1 = require("../../../../hooks/RoomRateAvailabilityListFromApi");
const DataLayer_1 = __importDefault(require("../../../../util/DataLayer"));
const TabGroupedRooms = () => {
    var _a;
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const { roomRateCalendarInteractivity } = (0, react_1.useContext)(contexts_1.BookingEngineContext);
    const { disableGroupRoomsAllTab } = (0, react_1.useContext)(contexts_1.HotelOverridesContext);
    const { rooms, isLoading, loadingAttempted, apiHasError } = (0, RoomRateAvailabilityListFromApi_1.useRoomRateAvailabilityListFromApi)();
    const params = (0, Query_1.useHashQuery)();
    const groupedRooms = (0, react_1.useMemo)(() => {
        var _a, _b, _c, _d;
        if (!hotel) {
            return [];
        }
        const sortingGroup = {
            'list-all': {
                id: 'list-all',
                groupName: 'All Accommodations',
                filters: {},
                rooms: [],
            },
        };
        for (const roomGroup of (_a = hotel === null || hotel === void 0 ? void 0 : hotel.roomGroups) !== null && _a !== void 0 ? _a : []) {
            sortingGroup[roomGroup.id] = Object.assign(Object.assign({}, roomGroup), { rooms: [] });
        }
        for (const room of rooms) {
            let groupId = !!room.roomGroupId ? room.roomGroupId : 'list-all';
            if (!sortingGroup[groupId]) {
                const foundGroup = (_b = hotel === null || hotel === void 0 ? void 0 : hotel.roomGroups) === null || _b === void 0 ? void 0 : _b.find((group) => group.id === groupId);
                if (foundGroup) {
                    sortingGroup[groupId] = Object.assign(Object.assign({}, foundGroup), { rooms: [] });
                }
                else {
                    // Can't find the specific room, defaulting to "unsorted" rooms
                    groupId = 'list-all';
                }
            }
            // Actual bit that puts the right room in the right location
            sortingGroup[groupId].rooms.push(room);
        }
        for (const sortedGroupKey of Object.keys(sortingGroup)) {
            if (((_d = (_c = sortingGroup[sortedGroupKey]) === null || _c === void 0 ? void 0 : _c.rooms) === null || _d === void 0 ? void 0 : _d.length) === 0 && sortedGroupKey !== 'list-all') {
                delete sortingGroup[sortedGroupKey];
            }
        }
        // Sort the rooms by the display order
        const valuedSortedGroups = Object.values(sortingGroup).filter((item) => !!item);
        if (valuedSortedGroups.length === 1 && valuedSortedGroups[0].rooms.length === 0) {
            valuedSortedGroups.splice(0, 1);
        }
        DataLayer_1.default.instance.sendRoomImpressions();
        return valuedSortedGroups;
    }, [rooms, hotel === null || hotel === void 0 ? void 0 : hotel.roomGroups]);
    const defaultTab = (0, react_1.useMemo)(() => {
        var _a;
        return (_a = params.get('defaultTab')) !== null && _a !== void 0 ? _a : undefined;
    }, []);
    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 && hasLoading) {
        return react_1.default.createElement(LargeLoader_1.default, null);
    }
    if (groupedRooms.length > 0 && groupedRooms.length <= 2) {
        let groupObject = { groupName: '', filters: {}, id: '' };
        let rooms = [];
        if (groupedRooms.length === 2) {
            groupObject = groupedRooms[1];
            rooms = groupedRooms[1].rooms;
        }
        else {
            rooms = groupedRooms[0].rooms;
        }
        return react_1.default.createElement(TabGroupedRoomList_1.TabGroupedRoomList, { group: groupObject, rooms: rooms });
    }
    else if (groupedRooms.length > 2) {
        const onlyGroups = [...groupedRooms];
        // Update onlyGroups to be the same as groupedRooms, but without the first element
        onlyGroups.splice(0, 1);
        return (react_1.default.createElement("div", { style: { maxWidth: '100%', overflow: 'hidden' } },
            react_1.default.createElement(Tabs_1.default, { fullBleedOnLineBreak: false, activeKey: defaultTab },
                react_1.default.createElement(Tab_1.Tab, { title: "All Accommodation", navPath: "list-all", key: "list-all", preventScrollTop: true }, disableGroupRoomsAllTab ? (react_1.default.createElement(TabGroupedRoomList_1.TabGroupedRoomList, { group: groupedRooms[0], rooms: rooms })) : (
                // We're not starting at 1 here as we want to display these rooms, even if they're not ordered
                groupedRooms === null || groupedRooms === void 0 ? void 0 : groupedRooms.filter((item) => item.rooms.length).map((group) => {
                    return react_1.default.createElement(TabGroupedRoomList_1.TabGroupedRoomList, { group: group, rooms: group.rooms, key: group.id });
                }))),
                onlyGroups.map((group) => (react_1.default.createElement(Tab_1.Tab, { title: group.groupName, navPath: group.groupName, key: group.id, preventScrollTop: true },
                    react_1.default.createElement(TabGroupedRoomList_1.TabGroupedRoomList, { group: group, rooms: group.rooms })))))));
    }
    // No rooms found, so let's display the crossell block, or no rooms found
    return (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 })))));
};
exports.TabGroupedRooms = TabGroupedRooms;
//# sourceMappingURL=TabGroupedRooms.js.map