"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 = RoomRates;
const contexts_1 = require("../../../../../contexts/index.js");
const hooks_1 = require("../../../../../hooks/index.js");
const classnames_1 = __importDefault(require("classnames"));
const react_1 = __importStar(require("react"));
const react_i18next_1 = require("react-i18next");
const Translation_1 = require("translations/Translation");
const BEButton_1 = __importDefault(require("../../../../generic/BEButton"));
const Icon_1 = require("../../../../generic/Icon/Icon");
const MembersOnlyRow_1 = __importDefault(require("./MembersOnlyRow"));
const RoomRateRow_1 = __importDefault(require("./RoomRateRow"));
const RoomRateReadMoreVersionFeature_1 = __importDefault(require("../../../../../providers/feature/RoomRateReadMoreVersionFeature"));
const RoomSortProvider_1 = __importDefault(require("../../../../../providers/RoomSortProvider"));
const Color_1 = require("../../../../../util/Color");
const DataLayer_1 = __importDefault(require("../../../../../util/DataLayer"));
const ScreenSize_1 = __importDefault(require("../../../../../util/ScreenSize"));
const ReadMoreRoomRateRow_1 = __importDefault(require("./ReadMoreRoomRateRow"));
const RoomRateListPromotion_1 = require("./RoomRateListPromotion/RoomRateListPromotion");
function RoomRates(props) {
    const { t } = (0, react_i18next_1.useTranslation)();
    const [showingMoreOpen, setShowingMoreOpen] = (0, react_1.useState)(false);
    const basketContext = (0, react_1.useContext)(contexts_1.BasketContext);
    const beContext = (0, react_1.useContext)(contexts_1.BookingEngineContext);
    const ccx = (0, react_1.useContext)(contexts_1.CompanyContext);
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const { rates, isCardLayout } = props;
    const numOfRatesToShowFirst = beContext.screenSize <= ScreenSize_1.default.Medium ? 2 : 3;
    (0, react_1.useEffect)(() => {
        var _a;
        if (((_a = basketContext.selectedBasketRow) === null || _a === void 0 ? void 0 : _a.getRoom()) && basketContext.selectedBasketRow.getRoom().code === props.roomCode) {
            setShowingMoreOpen(true);
        }
    }, [basketContext.selectedBasketRow]);
    (0, react_1.useEffect)(() => {
        DataLayer_1.default.instance.sendRoomImpressions();
    }, [showingMoreOpen]);
    return (0, react_1.useMemo)(() => {
        var _a, _b;
        // Use currentRoomRateSort if defined, otherwise fallback to currentRoomSort
        const sortBy = (_a = beContext.currentRoomRateSort) !== null && _a !== void 0 ? _a : beContext.currentRoomSort;
        const featuredRates = (_b = props.featuredRates) === null || _b === void 0 ? void 0 : _b.filter((rate) => !!rate);
        const hasFeaturedRates = !!featuredRates && featuredRates.length > 0;
        let sortedRates;
        if (rates.length !== 0) {
            sortedRates = rates.sort(RoomSortProvider_1.default.getSortByFunction(sortBy));
        }
        // Need two sorts because of MOD
        sortedRates = rates.sort((a, b) => {
            if (a.isMemberOnly) {
                return -1;
            }
            else if (b.isMemberOnly) {
                return 1;
            }
            else {
                return 0;
            }
        });
        const showMoreOnClick = () => {
            setShowingMoreOpen(true);
        };
        const itemsToRender = [];
        const renderRoomRateRow = (rate, isPromo = false) => {
            if (RoomRateReadMoreVersionFeature_1.default.isActive()) {
                return (react_1.default.createElement(ReadMoreRoomRateRow_1.default, { key: rate.name, rate: rate, getButtonLabel: props.getButtonLabel, onAddToBasketOverride: props.onAddToBasketOverride, isPromoRate: isPromo }));
            }
            return (react_1.default.createElement(RoomRateRow_1.default, { key: rate.name, rate: rate, getButtonLabel: props.getButtonLabel, onAddToBasketOverride: props.onAddToBasketOverride, isPromoRate: isPromo, isCardLayout: isCardLayout, policy: props.policy, priceDescription: props.priceDescription, bookNowButtonDisabledReason: props.bookNowButtonDisabledReason }));
        };
        const maxRows = hasFeaturedRates && !showingMoreOpen ? featuredRates.length : numOfRatesToShowFirst;
        let rowsLeftToOpen = 0;
        if (featuredRates && featuredRates.length) {
            featuredRates.sort(RoomSortProvider_1.default.getSortByFunction(sortBy));
            // First we add Featured Rates to the top of the list - no limits there
            featuredRates.forEach((row) => {
                if (row) {
                    itemsToRender.push(renderRoomRateRow(row, true));
                }
            });
        }
        if (basketContext.loadedPromotion) {
            itemsToRender.push(react_1.default.createElement(RoomRateListPromotion_1.RoomRateListPromotion, { promotion: basketContext.loadedPromotion }));
        }
        // Then we add the remaining rates to the render list - stop adding if we hit `maxRows`
        sortedRates.forEach((rate) => {
            if (isCardLayout || itemsToRender.length < maxRows || showingMoreOpen) {
                itemsToRender.push(renderRoomRateRow(rate));
            }
            else {
                rowsLeftToOpen++;
            }
        });
        // Then we do a tricky check to see if MOD intent should be displayed
        if (!isCardLayout && (!hasFeaturedRates || showingMoreOpen)) {
            if (!props.hideMemberOnlyRow && !!(hotel === null || hotel === void 0 ? void 0 : hotel.memberOnlyPromoCode)) {
                if (hotel === null || hotel === void 0 ? void 0 : hotel.memberOnlyRenderRateLast) {
                    itemsToRender.push(react_1.default.createElement(MembersOnlyRow_1.default, null));
                }
                else {
                    itemsToRender.unshift(react_1.default.createElement(MembersOnlyRow_1.default, null));
                }
            }
        }
        return (react_1.default.createElement("div", { className: (0, classnames_1.default)(props.classNames) },
            itemsToRender.map((item, index) => {
                return react_1.default.createElement(react_1.default.Fragment, { key: index }, item);
            }),
            rowsLeftToOpen > 0 && (react_1.default.createElement("div", { className: "room-show-more" },
                react_1.default.createElement(BEButton_1.default, { icon: Icon_1.IconType.ArrowDown2, iconPosition: "right", stopIconAnimation: true, isText: true, textColor: Color_1.Color.Navy, onClick: showMoreOnClick },
                    t(Translation_1.Translation.Step.Room.RoomInfo.Show),
                    " ",
                    rowsLeftToOpen,
                    " ",
                    t(Translation_1.Translation.Step.Room.RoomInfo.MoreRates))))));
    }, [
        props.featuredRates,
        rates,
        beContext.currentRoomSort,
        beContext.currentRoomRateSort,
        showingMoreOpen,
        basketContext === null || basketContext === void 0 ? void 0 : basketContext.loadedPromotion,
        hotel === null || hotel === void 0 ? void 0 : hotel.memberOnlyRenderRateLast,
        props.bookNowButtonDisabledReason,
    ]);
}
//# sourceMappingURL=RoomRates.js.map