"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 = RoomRatePills;
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 Icon_1 = require("../../../../generic/Icon/Icon");
const Pill_1 = __importStar(require("../../../../generic/Pill"));
const Text_1 = __importStar(require("../../../../generic/Text"));
const Tooltip_1 = __importDefault(require("../../../../generic/Tooltip/Tooltip"));
const HotelOverridesContext_1 = require("../../../../../contexts/HotelOverridesContext");
const FeaturedPromoFeature_1 = __importDefault(require("../../../../../providers/feature/FeaturedPromoFeature"));
const NumberOfPeopleBookedPillFeature_1 = __importDefault(require("../../../../../providers/feature/NumberOfPeopleBookedPillFeature"));
const RatePillProvider_1 = __importDefault(require("../../../../../providers/RatePillProvider"));
function RoomRatePills(props) {
    var _a, _b;
    const [pills, setPills] = (0, react_1.useState)([]);
    const currentHotel = (0, hooks_1.useCurrentHotel)();
    const hotelOverridesContext = react_1.default.useContext(HotelOverridesContext_1.HotelOverridesContext);
    const ccx = (0, react_1.useContext)(contexts_1.CompanyContext);
    const basketContext = (0, react_1.useContext)(contexts_1.BasketContext);
    const { t } = (0, react_i18next_1.useTranslation)();
    (0, react_1.useEffect)(() => {
        let pillsToDisplay = [];
        RatePillProvider_1.default.registerRatePill({ icon: Icon_1.IconType.Breakfast, title: t(Translation_1.Translation.Step.Room.BreakfastIncluded) }, ({ rate, hotel }) => {
            var _a;
            return !!hotel.showMealPill && !!((_a = rate === null || rate === void 0 ? void 0 : rate.mealsIncluded) === null || _a === void 0 ? void 0 : _a.breakfast);
        }, true);
        RatePillProvider_1.default.registerRatePill({
            translation: Translation_1.Translation.Step.Room.RoomInfo.PromoCode.MemberDeal,
            translationProps: {
                companyShort: ccx.name,
            },
            icon: Icon_1.IconType.Check,
        }, ({ rate, hotel }) => {
            var _a;
            return !!(rate === null || rate === void 0 ? void 0 : rate.promoCode) && ((_a = rate === null || rate === void 0 ? void 0 : rate.promoCode) === null || _a === void 0 ? void 0 : _a.toUpperCase()) === (hotel === null || hotel === void 0 ? void 0 : hotel.memberOnlyPromoCode);
        }, true);
        RatePillProvider_1.default.registerRatePill({
            translation: Translation_1.Translation.Step.Room.RoomInfo.PromoCode.Standard,
            icon: Icon_1.IconType.Check,
            setDescription: getAppliedPromoCodeDescription,
        }, ({ rate, hotel }) => {
            var _a;
            return !!(rate === null || rate === void 0 ? void 0 : rate.promoCode) && !(((_a = rate === null || rate === void 0 ? void 0 : rate.promoCode) === null || _a === void 0 ? void 0 : _a.toUpperCase()) === (hotel === null || hotel === void 0 ? void 0 : hotel.memberOnlyPromoCode));
        }, true);
        /** Registering Room Pills */
        RatePillProvider_1.default.registerRoomPill({
            translation: Translation_1.Translation.Misc.Pills.PeopleBookedShort,
            translationProps: {
                people: Math.floor(Math.random() * 8 + 7),
            },
            icon: Icon_1.IconType.People,
        }, () => {
            return NumberOfPeopleBookedPillFeature_1.default.isActive();
        });
        RatePillProvider_1.default.registerRoomPill({
            icon: Icon_1.IconType.Check,
            title: t(Translation_1.Translation.Step.Room.PromoCodeIsApplied),
        }, (match) => {
            var _a, _b, _c, _d;
            if (FeaturedPromoFeature_1.default.isActive() && ((_a = match.room) === null || _a === void 0 ? void 0 : _a.getRates())) {
                for (const rate of match.room.getRates()) {
                    if (((_b = rate === null || rate === void 0 ? void 0 : rate.promoCode) === null || _b === void 0 ? void 0 : _b.toLowerCase()) === ((_d = (_c = basketContext === null || basketContext === void 0 ? void 0 : basketContext.selectedBasketRow) === null || _c === void 0 ? void 0 : _c.getPromoCode()) === null || _d === void 0 ? void 0 : _d.toLowerCase())) {
                        return true;
                    }
                }
            }
            return false;
        });
        RatePillProvider_1.default.registerRatePill({
            icon: Icon_1.IconType.Money,
            title: t(Translation_1.Translation.Step.Room.RoomInfo.RateVariesByDay),
            renderOverride: () => {
                return react_1.default.createElement(react_1.default.Fragment, null, t(Translation_1.Translation.Step.Room.RoomInfo.RateVariesByDay));
            },
        }, (match) => {
            const selectedRow = basketContext.selectedBasketRow;
            if (!selectedRow)
                return false;
            const rate = match.rate;
            if (!rate)
                return false;
            // If room is present, we must be at the room level, we only want to show the pill
            // on a rate.
            if (match.room)
                return false;
            const hasRateVariesByDay = rate.hasRateVariesByDay({
                startDate: selectedRow.getStartDate(),
                endDate: selectedRow.getEndDate(),
            });
            return hasRateVariesByDay;
        });
        if (props.room) {
            pillsToDisplay = RatePillProvider_1.default.getPillsForRoom({ room: props.room, hotel: currentHotel.hotel });
        }
        else if (props.rate) {
            pillsToDisplay = RatePillProvider_1.default.getPillsForRate({ rate: props.rate, hotel: currentHotel.hotel });
        }
        if (props.limit) {
            setPills(pillsToDisplay.splice(0, props.limit));
        }
        else {
            setPills(pillsToDisplay);
        }
    }, [props.room, props.rate, props.limit]);
    const getAppliedPromoCodeDescription = (rate) => {
        return rate.promoCodeName;
    };
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement("div", { className: 'c-pill-list ' + (props.noMargin !== true && 'u-marg-bottom--light') }, pills.map((pill, index) => {
            var _a;
            let pillContent = pill.translation ? t(pill.translation, pill.translationProps) : pill.title;
            if (pill.renderOverride && typeof pill.renderOverride === 'function') {
                const overrideContent = pill.renderOverride({ hotel: currentHotel.hotel, rate: props.rate, room: props.room });
                if (overrideContent) {
                    pillContent = overrideContent;
                }
            }
            const pillELement = (react_1.default.createElement(Pill_1.default, { type: (_a = pill.type) !== null && _a !== void 0 ? _a : Pill_1.PillType.Primary, icon: pill.icon }, pillContent));
            return (react_1.default.createElement("div", { className: "u-inline u-marg-left--light u-marg-bottom--light", key: index }, pill.tooltipText ? react_1.default.createElement(Tooltip_1.default, { title: pill.tooltipText }, pillELement) : pillELement));
        })),
        hotelOverridesContext.showPromoCodeDescription && props.rateDescription && ((_a = pills === null || pills === void 0 ? void 0 : pills[0]) === null || _a === void 0 ? void 0 : _a.description) && (react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, className: "u-marg-bottom" }, (_b = pills === null || pills === void 0 ? void 0 : pills[0]) === null || _b === void 0 ? void 0 : _b.description))));
}
//# sourceMappingURL=RoomRatePills.js.map