"use strict";
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 };
};
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
const BookingAPI_1 = __importDefault(require("../api/BookingAPI"));
class FilterProvider {
}
_a = FilterProvider;
// TODO refactor to load from a static list of filters
FilterProvider.getRoomList = (row, filters, hotelID, memberNumber) => __awaiter(void 0, void 0, void 0, function* () {
    const rooms = yield _a.getUnfilteredRoomList(row, hotelID, memberNumber);
    return _a.filterRooms(rooms, filters);
});
FilterProvider.getRoomListByHotelIds = (row, filters, hotelIds, memberNumber) => __awaiter(void 0, void 0, void 0, function* () {
    const data = yield _a.getUnfilteredRoomListByHotelIds(row, hotelIds, memberNumber);
    return _a.filterRoomsByHotelIds(data, filters);
});
FilterProvider.filterRoomsByHotelIds = (data, filters) => Object.fromEntries(Object.entries(data).map(([hotelId, hotelData]) => [
    hotelId,
    {
        hotel: hotelData.hotel,
        rooms: _a.filterRooms(hotelData.rooms, filters),
    },
]));
FilterProvider.filterRooms = (rooms, filters) => {
    const filteredRooms = [];
    rooms.forEach((room) => {
        let filteredRoom = room;
        filteredRoom.filteredRates = room.getOriginalRates();
        for (const filter of filters) {
            if (!filter)
                continue;
            filteredRoom = filter.check(room);
            if (!filteredRoom) {
                break;
            }
        }
        if (filteredRoom) {
            filteredRooms.push(filteredRoom);
        }
    });
    return filteredRooms;
};
FilterProvider.getUnfilteredRoomList = (row, hotelID, memberNumber) => __awaiter(void 0, void 0, void 0, function* () { return BookingAPI_1.default.Availability.fetchAvailabilityData(row, hotelID, memberNumber); });
FilterProvider.getUnfilteredRoomListByHotelIds = (row, hotelIds, memberNumber) => __awaiter(void 0, void 0, void 0, function* () { return BookingAPI_1.default.Availability.fetchAvailabilityDataByHotelIds(row, hotelIds, memberNumber); });
exports.default = FilterProvider;
//# sourceMappingURL=FilterProvider.js.map