"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useSearchScopeHook = void 0;
const CurrentHotelHook_1 = require("./CurrentHotelHook");
const useSearchScopeHook = () => {
    var _a, _b;
    const { hotel } = (0, CurrentHotelHook_1.useCurrentHotel)();
    const limits = hotel === null || hotel === void 0 ? void 0 : hotel.occupancyLimits;
    const countInfants = (_a = hotel === null || hotel === void 0 ? void 0 : hotel.countInfantsInOccupancy) !== null && _a !== void 0 ? _a : false;
    const getOccupancyConfig = (adults) => {
        if (!limits)
            return null;
        if (adults > limits.highestAdultConfig)
            return null;
        if (limits.maxSelectableChildren[adults] === undefined)
            return null;
        return {
            children: limits.maxSelectableChildren[adults],
            infants: limits.maxSelectableInfants[adults],
        };
    };
    const getMaxSelectableAdults = ({ adults, children = 0, infants = 0 }) => {
        var _a, _b;
        const maxOccupancy = Math.min((_a = hotel === null || hotel === void 0 ? void 0 : hotel.maxAdults) !== null && _a !== void 0 ? _a : Number.MAX_VALUE, (_b = hotel === null || hotel === void 0 ? void 0 : hotel.maxOccupancy) !== null && _b !== void 0 ? _b : 0);
        if (limits) {
            let maxAdults = 0;
            for (let adultCount = 1; adultCount <= limits.highestAdultConfig; adultCount++) {
                const config = getOccupancyConfig(adultCount);
                if (!config) {
                    // No config for this adult count - use standard occupancy logic
                    const totalOccupancy = adultCount + children + (countInfants ? infants : 0);
                    if (totalOccupancy <= maxOccupancy) {
                        maxAdults = adultCount;
                    }
                }
                else if (config.children >= children) {
                    // Config exists and supports the children count
                    if (countInfants && config.infants !== undefined) {
                        if (config.infants >= infants) {
                            maxAdults = adultCount;
                        }
                    }
                    else {
                        maxAdults = adultCount;
                    }
                }
            }
            return Math.max(maxAdults, 1);
        }
        // Standard logic
        const maxAdults = Math.max(maxOccupancy - children, adults !== null && adults !== void 0 ? adults : 1);
        return Math.max(maxAdults, 1) || 1;
    };
    const getMaxSelectableChildren = ({ adults }) => {
        var _a, _b, _c;
        const standardMax = Math.max(Math.min((_a = hotel === null || hotel === void 0 ? void 0 : hotel.maxChildren) !== null && _a !== void 0 ? _a : Number.MAX_VALUE, (_b = hotel === null || hotel === void 0 ? void 0 : hotel.maxOccupancy) !== null && _b !== void 0 ? _b : 0) - adults, 0);
        const config = getOccupancyConfig(adults);
        return (_c = config === null || config === void 0 ? void 0 : config.children) !== null && _c !== void 0 ? _c : standardMax;
    };
    const getMaxSelectableInfants = ({ adults }) => {
        var _a, _b;
        const standardMax = (_a = hotel === null || hotel === void 0 ? void 0 : hotel.maxInfants) !== null && _a !== void 0 ? _a : 10;
        if (!countInfants) {
            return standardMax;
        }
        const config = getOccupancyConfig(adults);
        return (_b = config === null || config === void 0 ? void 0 : config.infants) !== null && _b !== void 0 ? _b : standardMax;
    };
    const showChildren = ((_b = hotel === null || hotel === void 0 ? void 0 : hotel.childConfiguration) === null || _b === void 0 ? void 0 : _b.supportChildren) !== false;
    return {
        getMaxSelectableAdults,
        getMaxSelectableChildren,
        getMaxSelectableInfants,
        showChildren,
    };
};
exports.useSearchScopeHook = useSearchScopeHook;
//# sourceMappingURL=useSearchScopeHook.js.map