"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAreaAvailability = void 0;
const contexts_1 = require("../contexts/index.js");
const react_1 = require("react");
const index_1 = require("../index");
const useAreaAvailability = (room) => {
    var _a;
    const { hotel } = (0, index_1.useCurrentHotel)();
    const basketContext = (0, react_1.useContext)(contexts_1.BasketContext);
    const beContext = (0, react_1.useContext)(contexts_1.BookingEngineContext);
    // By default, we assume the room is available.
    const [isAvailable, setIsAvailable] = (0, react_1.useState)(true);
    const selectedBasketRow = (_a = basketContext.selectedBasketRow) !== null && _a !== void 0 ? _a : basketContext.currentBasketRows[0];
    (0, react_1.useEffect)(() => {
        var _a;
        let isCancelled = false;
        setIsAvailable(true);
        const roomCode = room === null || room === void 0 ? void 0 : room.code;
        if ((hotel === null || hotel === void 0 ? void 0 : hotel.checkAreaAvailabilityOnRoomStep) && roomCode) {
            // Find the room group that this room belongs to
            const roomGroup = (_a = hotel.roomGroups) === null || _a === void 0 ? void 0 : _a.find((group) => group.id === (room === null || room === void 0 ? void 0 : room.roomGroupId));
            // Get the filter IDs that belong to this room group
            const roomGroupFilterIds = (roomGroup === null || roomGroup === void 0 ? void 0 : roomGroup.filters) ? Object.keys(roomGroup.filters) : [];
            // Filter to only include filters that belong to this room's room group
            const applicableFilters = Object.fromEntries(Object.entries(beContext.currentGroupedRoomFilters).filter(([id]) => {
                return roomGroupFilterIds.includes(id);
            }));
            index_1.API.Availability.fetchAreaAvailability(selectedBasketRow, hotel.hotelID, applicableFilters).then((areaAvailability) => {
                if (!isCancelled && areaAvailability[roomCode] === 0) {
                    setIsAvailable(false);
                }
            });
        }
        return () => {
            isCancelled = true;
        };
    }, [selectedBasketRow, beContext.currentGroupedRoomFilters, hotel, room]);
    return isAvailable;
};
exports.useAreaAvailability = useAreaAvailability;
//# sourceMappingURL=useAreaAvailability.js.map