"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 };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useCalendarNightStay = void 0;
const hooks_1 = require("./index.js");
const BookingAPI_1 = __importDefault(require("../api/BookingAPI"));
const useSignedInMember_1 = require("./useSignedInMember");
const useCalendarNightStay = ({ basketContext, }) => {
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const { memberNumber } = (0, useSignedInMember_1.useSignedInMember)();
    const nightStayValidation = {
        init: false,
        arrivalMinNightStay: null,
        arrivalMaxNightStay: null,
        validationMinNightStay: null,
        validationMaxNightStay: null,
    };
    const getLastValidDay = (fromDate, toDate, roomCode, rateCode) => new Promise((resolve) => __awaiter(void 0, void 0, void 0, function* () {
        var _a, _b, _c, _d;
        const selectedBasketRow = basketContext.selectedBasketRow;
        const adults = selectedBasketRow ? selectedBasketRow.getAdults() : 0;
        const children = selectedBasketRow ? selectedBasketRow.getChildren() : 0;
        const infants = selectedBasketRow ? selectedBasketRow.getInfants() : 0;
        const promoCode = selectedBasketRow ? selectedBasketRow.getPromoCode() : '';
        if (!roomCode) {
            roomCode = selectedBasketRow ? selectedBasketRow.getRoomCode() : '';
        }
        const toDateDiff = (_a = toDate === null || toDate === void 0 ? void 0 : toDate.diff) === null || _a === void 0 ? void 0 : _a.call(toDate, fromDate, 'day');
        for (let i = 0; i <= toDateDiff; i++) {
            const dayToCheck = fromDate.add(i, 'day');
            const price = yield BookingAPI_1.default.Availability.getCheapestPriceForDay(dayToCheck, adults, children, infants, promoCode, roomCode, rateCode || '', hotel === null || hotel === void 0 ? void 0 : hotel.hotelID, memberNumber, false);
            const nextMustBreak = !(price === null || price === void 0 ? void 0 : price.price) && hotel;
            const departureDateDiff = (_b = dayToCheck === null || dayToCheck === void 0 ? void 0 : dayToCheck.diff) === null || _b === void 0 ? void 0 : _b.call(dayToCheck, fromDate, 'day');
            if (departureDateDiff == 0) {
                //Arrival date
                Object.assign(nightStayValidation, {
                    init: true,
                    arrivalMinNightStay: (_c = price === null || price === void 0 ? void 0 : price.arrivalMinNightStay) !== null && _c !== void 0 ? _c : null,
                    arrivalMaxNightStay: (_d = price === null || price === void 0 ? void 0 : price.arrivalMaxNightStay) !== null && _d !== void 0 ? _d : null,
                    validationMinNightStay: null,
                    validationMaxNightStay: null,
                });
            }
            if (nightStayValidation.arrivalMinNightStay) {
                //Min. Departure date
                if (departureDateDiff >= nightStayValidation.arrivalMinNightStay) {
                    nightStayValidation.validationMinNightStay = null;
                }
                else {
                    nightStayValidation.validationMinNightStay = nightStayValidation.arrivalMinNightStay;
                }
            }
            if (nightStayValidation.arrivalMaxNightStay) {
                //Max. Departure date
                if (departureDateDiff <= nightStayValidation.arrivalMaxNightStay) {
                    nightStayValidation.validationMaxNightStay = null;
                }
                else {
                    nightStayValidation.validationMaxNightStay = nightStayValidation.arrivalMaxNightStay;
                }
            }
            if (nextMustBreak) {
                //if true, this date only available for departure (no price for checking)
                return resolve({
                    lastValidDay: dayToCheck,
                    nightStayValidation,
                    isValidNightStay: !nightStayValidation.validationMinNightStay && !nightStayValidation.validationMaxNightStay,
                });
            }
        }
        return resolve({
            lastValidDay: toDate,
            nightStayValidation,
            isValidNightStay: !nightStayValidation.validationMinNightStay && !nightStayValidation.validationMaxNightStay,
        });
    }));
    const getArrivalNightStay = (fromDate, roomCode, rateCode) => new Promise((resolve) => __awaiter(void 0, void 0, void 0, function* () {
        var _a, _b;
        const selectedBasketRow = basketContext.selectedBasketRow;
        const adults = selectedBasketRow ? selectedBasketRow.getAdults() : 0;
        const children = selectedBasketRow ? selectedBasketRow.getChildren() : 0;
        const infants = selectedBasketRow ? selectedBasketRow.getInfants() : 0;
        const promoCode = selectedBasketRow ? selectedBasketRow.getPromoCode() : '';
        const price = yield BookingAPI_1.default.Availability.getCheapestPriceForDay(fromDate, adults, children, infants, promoCode, roomCode, rateCode, hotel === null || hotel === void 0 ? void 0 : hotel.hotelID, memberNumber, false);
        return resolve({
            arrivalMinNightStay: (_a = price === null || price === void 0 ? void 0 : price.arrivalMinNightStay) !== null && _a !== void 0 ? _a : null,
            arrivalMaxNightStay: (_b = price === null || price === void 0 ? void 0 : price.arrivalMaxNightStay) !== null && _b !== void 0 ? _b : null,
        });
    }));
    const checkAvailableNightStay = ({ arrivalNightStay, startDate, dateToCheck, }) => {
        var _a, _b;
        const arrivalMinNightStay = (_a = arrivalNightStay === null || arrivalNightStay === void 0 ? void 0 : arrivalNightStay.arrivalMinNightStay) !== null && _a !== void 0 ? _a : null;
        const arrivalMaxNightStay = (_b = arrivalNightStay === null || arrivalNightStay === void 0 ? void 0 : arrivalNightStay.arrivalMaxNightStay) !== null && _b !== void 0 ? _b : null;
        let minDate = null;
        let maxDate = null;
        if (dateToCheck && startDate && (arrivalMinNightStay || arrivalMaxNightStay)) {
            const validation = [];
            if (arrivalMinNightStay) {
                minDate = startDate.clone().add(arrivalMinNightStay, 'day');
                validation.push(dateToCheck >= minDate);
            }
            if (arrivalMaxNightStay) {
                maxDate = startDate.clone().add(arrivalMaxNightStay, 'day');
                validation.push(dateToCheck <= maxDate);
            }
            return {
                minDate,
                maxDate,
                available: validation.every((v) => v === true),
            };
        }
        return {
            minDate,
            maxDate,
            available: true,
        };
    };
    return {
        getLastValidDay,
        getArrivalNightStay,
        checkAvailableNightStay,
    };
};
exports.useCalendarNightStay = useCalendarNightStay;
//# sourceMappingURL=useCalendarNightStay.js.map