"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.TabGroupedRoomList = void 0;
const contexts_1 = require("../../../../contexts/index.js");
const ui_1 = require("@roomstay/ui");
const classnames_1 = __importDefault(require("classnames"));
const react_1 = __importStar(require("react"));
const react_i18next_1 = require("react-i18next");
const Alert_1 = __importStar(require("../../../generic/Alert"));
const Headline_1 = __importDefault(require("../../../generic/Headline"));
const Select_1 = require("../../../generic/Select/Select");
const Text_1 = __importStar(require("../../../generic/Text"));
const hooks_1 = require("../../../../hooks");
const RoomSortProvider_1 = __importDefault(require("../../../../providers/RoomSortProvider"));
const Translation_1 = require("../../../../translations/Translation");
const RoomDetails_1 = __importDefault(require("../roomDetails/RoomDetails"));
const TabGroupedRoomList_module_scss_1 = __importDefault(require("./TabGroupedRoomList.module.scss"));
const TabGroupedRoomList = (props) => {
    const beContext = (0, react_1.useContext)(contexts_1.BookingEngineContext);
    const { orderUnavailableRoomsLast } = (0, react_1.useContext)(contexts_1.HotelOverridesContext);
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const { t } = (0, react_i18next_1.useTranslation)();
    const sortBy = beContext.currentRoomSort;
    const { rooms, group } = props;
    const [appliedFilters, setAppliedFilters] = (0, react_1.useState)({});
    const filterKeys = (0, react_1.useMemo)(() => {
        return Object.keys(group.filters || {});
    }, [group.filters]);
    const bookNowButtonDisabledReason = (0, react_1.useMemo)(() => {
        var _a;
        for (const filterKey of filterKeys) {
            const filter = (_a = group.filters) === null || _a === void 0 ? void 0 : _a[filterKey];
            if ((filter === null || filter === void 0 ? void 0 : filter.requireSelection) && !appliedFilters[filterKey]) {
                return t(Translation_1.Translation.Step.Room.SelectDwellingOptionsFirst);
            }
        }
    }, [appliedFilters, filterKeys]);
    const mandatoryFilters = (0, react_1.useMemo)(() => {
        return Object.entries(group.filters)
            .filter(([id, filter]) => !appliedFilters[id] && filter.requireSelection)
            .map(([, filter]) => filter.name)
            .join(', ');
    }, [appliedFilters, filterKeys]);
    const roomList = (0, react_1.useMemo)(() => {
        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;
                });
            }
        }
        return sortedRooms
            .filter((room) => {
            var _a;
            for (const filter of Object.keys(appliedFilters)) {
                if (((_a = room.auxiliaryValues) === null || _a === void 0 ? void 0 : _a[filter]) && appliedFilters[filter] && !room.auxiliaryValues[filter].includes(`${appliedFilters[filter]}`)) {
                    return false;
                }
            }
            return room;
        })
            .map((room, index) => {
            return react_1.default.createElement(RoomDetails_1.default, { key: room.code, room: room, disableMarginBottom: index === rooms.length - 1, bookNowButtonDisabledReason: bookNowButtonDisabledReason });
        });
    }, [rooms, appliedFilters, sortBy, orderUnavailableRoomsLast, bookNowButtonDisabledReason]);
    return (react_1.default.createElement("div", { className: (0, classnames_1.default)(TabGroupedRoomList_module_scss_1.default.groupedRoomList, 'room-list') },
        !!group.groupName && (react_1.default.createElement("div", { className: TabGroupedRoomList_module_scss_1.default.groupedRoomListHeader },
            react_1.default.createElement(Headline_1.default, { bold: true }, group.groupName))),
        mandatoryFilters && (react_1.default.createElement(Alert_1.default, { className: "rs-mandatory-filters-alert", type: Alert_1.AlertType.Accent1, icon: ui_1.IconType.Info },
            react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, t(Translation_1.Translation.Misc.Alerts.SelectRequiredForAvailability, { message: mandatoryFilters })))),
        !!(filterKeys === null || filterKeys === void 0 ? void 0 : filterKeys.length) && rooms.length > 0 && (react_1.default.createElement("div", { className: TabGroupedRoomList_module_scss_1.default.filterContainer }, filterKeys.map((filterKey) => {
            var _a, _b, _c;
            const filter = (_a = group.filters) === null || _a === void 0 ? void 0 : _a[filterKey];
            if (filter.type === 'select') {
                const options = [{ label: 'All', value: null }, ...filter.options];
                const currentValue = (_b = appliedFilters === null || appliedFilters === void 0 ? void 0 : appliedFilters[filterKey]) !== null && _b !== void 0 ? _b : (_c = options[0]) === null || _c === void 0 ? void 0 : _c.value;
                const localOnFilterChange = (value) => {
                    setAppliedFilters((prev) => {
                        const filter = Object.assign(Object.assign({}, prev), { [filterKey]: value });
                        beContext.setCurrentGroupedRoomFilters(filter);
                        return filter;
                    });
                };
                return (react_1.default.createElement("div", { key: filterKey, className: TabGroupedRoomList_module_scss_1.default.filterItem },
                    react_1.default.createElement("div", { className: TabGroupedRoomList_module_scss_1.default['filterItem-label'] },
                        filter.name,
                        ":"),
                    react_1.default.createElement(Select_1.Select, { keyName: "value", labelName: "label", options: options, value: currentValue, onChange: localOnFilterChange })));
            }
        }))),
        react_1.default.createElement("div", { className: "room-list__container" }, roomList)));
};
exports.TabGroupedRoomList = TabGroupedRoomList;
//# sourceMappingURL=TabGroupedRoomList.js.map