"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.default = StepDatePricePerRoom;
const contexts_1 = require("../../../contexts/index.js");
const hooks_1 = require("../../../hooks/index.js");
const react_1 = __importStar(require("react"));
const react_i18next_1 = require("react-i18next");
const Translation_1 = require("translations/Translation");
const BookingAPI_1 = __importDefault(require("../../../api/BookingAPI"));
const Currency_1 = __importDefault(require("../../generic/Currency"));
const Headline_1 = __importDefault(require("../../generic/Headline"));
const Icon_1 = __importStar(require("../../generic/Icon/Icon"));
const Text_1 = __importStar(require("../../generic/Text"));
const useSignedInMember_1 = require("../../../hooks/useSignedInMember");
const Color_1 = require("../../../util/Color");
const TextAlignment_1 = require("../../../util/TextAlignment");
// StepDateComponent
function StepDatePricePerRoom(props) {
    const { t } = (0, react_i18next_1.useTranslation)();
    const basketContext = (0, react_1.useContext)(contexts_1.BasketContext);
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const [pricePerRoom, setPricePerRoom] = (0, react_1.useState)(0);
    const [isLoading, setIsLoading] = (0, react_1.useState)(false);
    const { memberNumber } = (0, useSignedInMember_1.useSignedInMember)();
    (0, react_1.useEffect)(() => {
        var _a;
        let cancelled = false;
        const selectedBasketRow = (_a = basketContext.selectedBasketRow) !== null && _a !== void 0 ? _a : basketContext.currentBasketRows[basketContext.currentBasketRows.length - 1];
        if (basketContext.endDate) {
            const adults = selectedBasketRow ? selectedBasketRow.getAdults() : 0;
            const children = selectedBasketRow ? selectedBasketRow.getChildren() : 0;
            const infants = selectedBasketRow ? selectedBasketRow.getInfants() : 0;
            const promoCode = selectedBasketRow ? selectedBasketRow.getPromoCode() : '';
            const roomCode = selectedBasketRow ? selectedBasketRow.getRoomCode() : '';
            const rateCode = ''; // No rate code filter required in StepDateComponent
            setIsLoading(true);
            setTimeout(() => {
                BookingAPI_1.default.Availability.getCheapestPricePerDay(basketContext.startDate, basketContext.endDate.subtract(1, 'day'), adults, children, infants, promoCode, roomCode, rateCode, hotel === null || hotel === void 0 ? void 0 : hotel.hotelID, memberNumber).then((dayPrices) => {
                    const prices = Object.values(dayPrices);
                    if (!cancelled) {
                        let cheapestFromAPI = prices.length > 0 ? prices.reduce((prev, curr) => (!curr ? prev : Math.min(prev, curr))) : 0;
                        if (isNaN(cheapestFromAPI)) {
                            cheapestFromAPI = 0;
                        }
                        setPricePerRoom(cheapestFromAPI);
                        setIsLoading(false);
                    }
                });
            }, 250);
        }
        return () => {
            setIsLoading(false);
            cancelled = true;
        };
    }, [basketContext.endDate, basketContext.selectedBasketRow]);
    const isMobile = props.isMobile;
    return (0, react_1.useMemo)(() => {
        return (react_1.default.createElement("div", { className: "u-flex flex-column justify-content-end align-items-end h-100" },
            !isMobile ? (react_1.default.createElement(Text_1.default, { type: isMobile ? Text_1.TextType.Label : Text_1.TextType.Small, align: TextAlignment_1.TextAlign.Right, className: "u-nowrap" },
                react_1.default.createElement("span", { style: { color: Color_1.Color.DarkGrey } },
                    t(Translation_1.Translation.Misc.From),
                    " ",
                    react_1.default.createElement("span", { style: { color: Color_1.Color.Grey } },
                        "(",
                        t(Translation_1.Translation.Misc.PerRoom),
                        ")")))) : null,
            isMobile ? (react_1.default.createElement("span", { className: "u-ellip-w-100-deep" },
                react_1.default.createElement(Text_1.default, { bold: true }, isLoading ? (react_1.default.createElement("div", { className: "u-inline-flex a-spin" },
                    react_1.default.createElement(Icon_1.default, { icon: Icon_1.IconType.Spinner }))) : (react_1.default.createElement(Currency_1.default, { removeCurrencyAfterChar: 6 }, pricePerRoom))))) : (react_1.default.createElement(Headline_1.default, { bold: true, size: "large", align: TextAlignment_1.TextAlign.Right }, isLoading ? (react_1.default.createElement("div", { className: "u-inline-flex a-spin" },
                react_1.default.createElement(Icon_1.default, { icon: Icon_1.IconType.Spinner }))) : (react_1.default.createElement(Currency_1.default, null, pricePerRoom))))));
    }, [isLoading, pricePerRoom]);
}
//# sourceMappingURL=StepDatePricePerRoom.js.map