"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 = RoomRateRow;
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 = __importStar(require("../../../../generic/Icon/Icon"));
const Pill_1 = __importStar(require("../../../../generic/Pill"));
const PlanpayPrice_1 = require("../../../../generic/PlanpayPrice/PlanpayPrice");
const TaxInclusionNotice_1 = require("../../../../generic/TaxInclusionNotice/TaxInclusionNotice");
const Text_1 = __importStar(require("../../../../generic/Text"));
const RoomDetailsPriceBlock_1 = __importDefault(require("../RoomDetailsPriceBlock"));
const BookNowButton_1 = __importDefault(require("./BookNowButton"));
const RoomRateDetailsAndPriceBreakdownModal_1 = __importStar(require("./RoomRateDetailsAndPriceBreakdownModal"));
const RoomRatePills_1 = __importDefault(require("./RoomRatePills"));
const Currency_1 = __importDefault(require("../../../../../hooks/Currency"));
const Planpay_1 = __importDefault(require("../../../../../hooks/Planpay"));
const useRateDetails_1 = require("../../../../../hooks/useRateDetails");
const WindowSize_1 = require("../../../../../hooks/WindowSize");
const Color_1 = require("../../../../../util/Color");
const DataLayer_1 = __importStar(require("../../../../../util/DataLayer"));
const TextAlignment_1 = require("../../../../../util/TextAlignment");
const RoomRateRow_module_scss_1 = __importDefault(require("./RoomRateRow.module.scss"));
function RoomRateRow(props) {
    var _a, _b, _c;
    const ccx = (0, react_1.useContext)(contexts_1.CompanyContext);
    const basketContext = (0, react_1.useContext)(contexts_1.BasketContext);
    const { roomRateCalendarInteractivity, setRoomRateCalendarInteractivity } = (0, react_1.useContext)(contexts_1.BookingEngineContext);
    const [viewingModal, setViewingModal] = (0, react_1.useState)({
        open: false,
        currentTab: RoomRateDetailsAndPriceBreakdownModal_1.RoomRateDetailsAndPriceBreakdownModalTabs.RoomDetails,
    });
    const { t } = (0, react_i18next_1.useTranslation)();
    const { isAccent2ColorDark } = (0, hooks_1.useCurrentHotel)();
    const { isMobile } = (0, WindowSize_1.useWindowSize)();
    const { rate, isCardLayout } = props;
    const selectedRow = basketContext.selectedBasketRow;
    const isRoomSelectedAndEditing = selectedRow === null || selectedRow === void 0 ? void 0 : selectedRow.isValid();
    const isThisSelectedRow = selectedRow && rate.name === ((_a = selectedRow.getRate()) === null || _a === void 0 ? void 0 : _a.name) && ((_b = rate.getRoom()) === null || _b === void 0 ? void 0 : _b.name) === ((_c = selectedRow.getRoom()) === null || _c === void 0 ? void 0 : _c.name);
    const currentRow = (0, react_1.useRef)(null);
    const classes = (0, classnames_1.default)('room-info', {
        '--default': !rate.isMemberOnly,
        '--locked': rate.isMemberOnly,
        '--current': isThisSelectedRow && isRoomSelectedAndEditing,
        '--is-promo': props.isPromoRate,
    });
    let isLocked = false;
    if (rate.isMemberOnly && !ccx.signedInUser) {
        isLocked = true;
    }
    (0, react_1.useEffect)(() => {
        if (!isLocked) {
            const basketRow = basketContext.selectedBasketRow;
            if (!basketRow) {
                return;
            }
            DataLayer_1.default.instance.addRoomRateImpression(rate.getRoom(), rate, { to: basketRow.getStartDate(), from: basketRow.getEndDate() }, basketRow.getPromoCode());
        }
    }, [isThisSelectedRow]);
    const { rateName, shortDescription } = (0, useRateDetails_1.useRateDetails)(rate, isLocked);
    const openTab = (tab) => {
        setViewingModal({ open: true, currentTab: tab });
    };
    let bookNowLabel = '';
    if (props.getButtonLabel) {
        bookNowLabel = props.getButtonLabel(isRoomSelectedAndEditing, isThisSelectedRow);
    }
    else {
        if (isRoomSelectedAndEditing) {
            bookNowLabel = isThisSelectedRow ? t(Translation_1.Translation.Step.Room.RoomInfo.Selected) : (bookNowLabel = t(Translation_1.Translation.Step.Room.RoomInfo.Update));
        }
        else if (props.bookNowButtonDisabledReason) {
            bookNowLabel = props.bookNowButtonDisabledReason;
        }
        else {
            bookNowLabel = t(Translation_1.Translation.Misc.BookNow);
        }
    }
    const openMemberSignupModalOnClick = () => {
        DataLayer_1.default.instance.sendInteraction('Member Signup Modal', DataLayer_1.InteractionType.LINK, DataLayer_1.InteractionStep.ROOMS);
        ccx.openMemberSignupModal();
    };
    const rateDetailsOnClick = () => {
        DataLayer_1.default.instance.sendInteraction('More Rate Details', DataLayer_1.InteractionType.LINK, DataLayer_1.InteractionStep.ROOMS);
        openTab(RoomRateDetailsAndPriceBreakdownModal_1.RoomRateDetailsAndPriceBreakdownModalTabs.RoomDetails);
    };
    (0, react_1.useEffect)(() => {
        const roomCode = props.rate.getRoom().code;
        if ((roomRateCalendarInteractivity === null || roomRateCalendarInteractivity === void 0 ? void 0 : roomRateCalendarInteractivity.roomCode) === roomCode && props.rate.code === (roomRateCalendarInteractivity === null || roomRateCalendarInteractivity === void 0 ? void 0 : roomRateCalendarInteractivity.rateCode)) {
            //Provided the API response still includes that rate => still open the modal
            openTab(RoomRateDetailsAndPriceBreakdownModal_1.RoomRateDetailsAndPriceBreakdownModalTabs.RoomDetails);
        }
    }, [props.rate, roomRateCalendarInteractivity]);
    const updateModalOnClose = () => {
        // Clear the modal state
        setRoomRateCalendarInteractivity(null);
        setViewingModal((old) => {
            old.open = false;
            return Object.assign({}, old);
        });
    };
    const openTabOnClick = () => {
        DataLayer_1.default.instance.sendInteraction('Rate Breakdown', DataLayer_1.InteractionType.LINK, DataLayer_1.InteractionStep.ROOMS);
        openTab(RoomRateDetailsAndPriceBreakdownModal_1.RoomRateDetailsAndPriceBreakdownModalTabs.PriceBreakdown);
    };
    const shouldTextBeWhite = isAccent2ColorDark && ((isThisSelectedRow && isRoomSelectedAndEditing) || props.isPromoRate);
    const textWhiteOr = (color = Color_1.Color.Graphite) => {
        return shouldTextBeWhite ? Color_1.Color.White : color;
    };
    const { value: price, isLoading } = (0, Currency_1.default)({
        disableLoading: true,
        hideDecimals: true,
        children: rate.getAveragePrice(true),
    });
    const { hasPricePreview } = (0, Planpay_1.default)();
    const RoomDetailsPriceBlockSection = () => (react_1.default.createElement("div", { className: "u-flex flex-column align-items-start align-items-lg-end" },
        react_1.default.createElement(RoomDetailsPriceBlock_1.default, { isLocked: isLocked, averagePreDiscount: rate.getAveragePriceBeforeDiscount(true), averagePrice: rate.getAveragePrice(true), isMemberOnly: props.rate.isMemberOnly, useLightText: shouldTextBeWhite }),
        react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Label, color: textWhiteOr(Color_1.Color.DarkGrey), align: isMobile ? TextAlignment_1.TextAlign.Left : TextAlignment_1.TextAlign.Right },
            react_1.default.createElement(TaxInclusionNotice_1.TaxInclusionNotice, { rate: rate }))));
    const RateTitle = () => {
        if (isLocked) {
            return (react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Body, color: Color_1.Color.Success },
                react_1.default.createElement("strong", null, t(Translation_1.Translation.Step.Room.RoomInfo.MemberOnlyDeal))));
        }
        return (react_1.default.createElement("div", { className: (0, classnames_1.default)(RoomRateRow_module_scss_1.default['card-layout-rate-title']) },
            props.isPromoRate && isCardLayout ? react_1.default.createElement(Icon_1.default, { icon: Icon_1.IconType.Star }) : null,
            react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Body, color: textWhiteOr(), bold: true }, rateName)));
    };
    return (react_1.default.createElement("div", { className: classes, ref: currentRow },
        react_1.default.createElement("div", { className: "row" },
            react_1.default.createElement("div", { className: (0, classnames_1.default)({
                    'col-lg-6': hasPricePreview && !isCardLayout,
                    'col-lg-8': !hasPricePreview && !isCardLayout,
                    'col-lg-12': isCardLayout,
                }) },
                react_1.default.createElement("div", { className: "u-flex flex-column h-100" },
                    react_1.default.createElement("div", null,
                        react_1.default.createElement("div", { className: "u-marg-bottom-half" },
                            react_1.default.createElement(RateTitle, null)),
                        react_1.default.createElement("div", { className: "u-marg-bottom u-h-ml-5 text-small u-clear-font-weight rdcaf", style: { color: textWhiteOr() } },
                            react_1.default.createElement("div", { dangerouslySetInnerHTML: { __html: shortDescription !== null && shortDescription !== void 0 ? shortDescription : '' } })),
                        isLocked && (react_1.default.createElement("div", { className: "u-marg" },
                            react_1.default.createElement("span", { onClick: openMemberSignupModalOnClick },
                                react_1.default.createElement(Pill_1.default, { icon: Icon_1.IconType.Money, type: Pill_1.PillType.Success }, t(Translation_1.Translation.Step.Room.RoomInfo.SignUpToSee))))),
                        !isCardLayout ? react_1.default.createElement(RoomRatePills_1.default, { rate: props.rate, rateDescription: true }) : null),
                    isCardLayout ? (react_1.default.createElement("div", { className: (0, classnames_1.default)(RoomRateRow_module_scss_1.default['card-layout-price-wrapper']) },
                        props.policy ? (react_1.default.createElement("div", { className: (0, classnames_1.default)(RoomRateRow_module_scss_1.default['card-layout-price-policy']) },
                            react_1.default.createElement(Icon_1.default, { icon: Icon_1.IconType.Info }),
                            react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.DarkGrey }, props.policy))) : null,
                        react_1.default.createElement("div", { className: (0, classnames_1.default)(RoomRateRow_module_scss_1.default['card-layout-price-row']) },
                            props.priceDescription ? (react_1.default.createElement("div", { className: (0, classnames_1.default)(RoomRateRow_module_scss_1.default['card-layout-price-notes']) },
                                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.Accent }, props.priceDescription))) : null,
                            react_1.default.createElement("div", { className: (0, classnames_1.default)(RoomRateRow_module_scss_1.default['card-layout-price-info']) },
                                react_1.default.createElement(RoomDetailsPriceBlockSection, null))))) : null,
                    react_1.default.createElement("div", { className: (0, classnames_1.default)('u-flex justify-content-start u-marg-top-bottom@l- room-rate-row-navigation-button', {
                            [RoomRateRow_module_scss_1.default['room-rate-row-navigation-button']]: isCardLayout,
                        }) },
                        react_1.default.createElement("div", { className: "u-marg-right u-inline-flex" },
                            react_1.default.createElement(BEButton_1.default, { icon: isCardLayout ? undefined : Icon_1.IconType.ArrowRight2, isText: true, primary: true, size: "small", iconPosition: "right", onClick: rateDetailsOnClick, textColor: isCardLayout ? Color_1.Color.Accent : textWhiteOr(Color_1.Color.Accent) }, t(Translation_1.Translation.Step.Room.ReadMore))),
                        react_1.default.createElement(BEButton_1.default, { icon: isCardLayout ? undefined : Icon_1.IconType.ArrowRight2, isText: true, textColor: isCardLayout ? Color_1.Color.Accent : textWhiteOr(Color_1.Color.Navy), size: "small", iconPosition: "right", onClick: openTabOnClick, disabled: isLocked }, t(Translation_1.Translation.Step.Room.RoomInfo.ViewPriceBreakdown))))),
            react_1.default.createElement("div", { className: (0, classnames_1.default)({
                    'col-lg-6': hasPricePreview && !isCardLayout,
                    'col-lg-4': !hasPricePreview && !isCardLayout,
                    'col-lg-12': isCardLayout,
                }, 'u-flex flex-column align-items-start align-items-md-start align-items-lg-end justify-content-start') },
                !isCardLayout ? react_1.default.createElement(RoomDetailsPriceBlockSection, null) : null,
                !isLoading && hasPricePreview ? (react_1.default.createElement("div", { className: "u-marg-top--light" },
                    react_1.default.createElement(PlanpayPrice_1.PlanpayPrice, { price: price, checkin: ((selectedRow === null || selectedRow === void 0 ? void 0 : selectedRow.getStartDate()) || basketContext.endDate).format('YYYY-MM-DD'), currency: ccx.currentCurrency }))) : null,
                react_1.default.createElement("div", { className: (0, classnames_1.default)('u-marg-top--light', {
                        [RoomRateRow_module_scss_1.default['room-rate-row-booknow-button']]: isCardLayout,
                    }) }, isLocked ? (react_1.default.createElement(BEButton_1.default, { filled: true, success: true, wide: true, onClick: openMemberSignupModalOnClick }, t(Translation_1.Translation.Step.Room.RoomInfo.RevealPrice))) : (react_1.default.createElement(BookNowButton_1.default, { rate: rate, buttonLabel: bookNowLabel, onAddToBasketOverride: props.onAddToBasketOverride, isFilled: !isThisSelectedRow, disabled: !!props.bookNowButtonDisabledReason }))))),
        react_1.default.createElement(RoomRateDetailsAndPriceBreakdownModal_1.default, { isOpen: viewingModal.open, currentTab: viewingModal.currentTab, rate: props.rate, getButtonLabel: props.getButtonLabel, onAddToBasketOverride: props.onAddToBasketOverride, onClose: updateModalOnClose })));
}
//# sourceMappingURL=RoomRateRow.js.map