"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 = ReadMoreRoomRateRow;
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 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 RoomRateReadMoreVersionFeature_1 = __importDefault(require("../../../../../providers/feature/RoomRateReadMoreVersionFeature"));
const Color_1 = require("../../../../../util/Color");
const DataLayer_1 = __importStar(require("../../../../../util/DataLayer"));
const TextAlignment_1 = require("../../../../../util/TextAlignment");
const ReadMoreRoomRateRow_module_scss_1 = __importDefault(require("./ReadMoreRoomRateRow.module.scss"));
function ReadMoreRoomRateRow(props) {
    var _a, _b, _c;
    const ccx = (0, react_1.useContext)(contexts_1.CompanyContext);
    const basketContext = (0, react_1.useContext)(contexts_1.BasketContext);
    const [viewingModal, setViewingModal] = (0, react_1.useState)({
        open: false,
        currentTab: RoomRateDetailsAndPriceBreakdownModal_1.RoomRateDetailsAndPriceBreakdownModalTabs.RoomDetails,
    });
    const [requireReadMore, setRequireReadMore] = (0, react_1.useState)(false);
    const [readMoreOpened, setReadMoreOpened] = (0, react_1.useState)(false);
    const [targetOpenHeight, setTargetOpenHeight] = (0, react_1.useState)(0);
    const innerDescRef = (0, react_1.useRef)(null);
    const outerDescRef = (0, react_1.useRef)(null);
    const { t } = (0, react_i18next_1.useTranslation)();
    const { isAccent2ColorDark } = (0, hooks_1.useCurrentHotel)();
    const { rate } = 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)(() => {
        let isCancelled = false;
        const onResize = () => {
            if (!isCancelled && innerDescRef.current && outerDescRef.current) {
                const innerBoundiugRect = innerDescRef.current.getBoundingClientRect();
                const outerBoundingRect = outerDescRef.current.getBoundingClientRect();
                setRequireReadMore(innerBoundiugRect.height > outerBoundingRect.height);
                setTargetOpenHeight(innerBoundiugRect.height);
            }
        };
        onResize();
        window.addEventListener('resize', onResize);
        return () => {
            isCancelled = true;
            window.removeEventListener('resize', onResize);
        };
    }, [innerDescRef, outerDescRef]);
    (0, react_1.useEffect)(() => {
        if (!isLocked) {
            const basketRow = basketContext.selectedBasketRow;
            if (!basketRow) {
                return;
            }
            DataLayer_1.default.instance.addRoomImpression(rate.getRoom(), { to: basketRow.getStartDate(), from: basketRow.getEndDate() }, basketRow.getPromoCode());
        }
    }, [isThisSelectedRow]);
    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 {
            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 = () => {
        setReadMoreOpened((opened) => {
            return !opened;
        });
    };
    const updateModalOnClose = () => {
        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 outerDescriptionClassList = (0, classnames_1.default)({
        [ReadMoreRoomRateRow_module_scss_1.default.descriptionTextOuter]: true,
        'text-small': true,
        [ReadMoreRoomRateRow_module_scss_1.default.requireReadMore]: requireReadMore,
        [ReadMoreRoomRateRow_module_scss_1.default.revealReadMore]: readMoreOpened,
    });
    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,
                    'col-lg-8': !hasPricePreview,
                }) },
                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" }, isLocked ? (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)))) : (react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Body, color: textWhiteOr(), bold: true }, rate.name))),
                        react_1.default.createElement("div", { className: outerDescriptionClassList, style: {
                                color: textWhiteOr(),
                                ['--maxLines']: RoomRateReadMoreVersionFeature_1.default.getMaxLines(),
                                ['--lineHeight']: targetOpenHeight + 'px',
                            }, ref: outerDescRef },
                            react_1.default.createElement("div", { className: ReadMoreRoomRateRow_module_scss_1.default.descriptionTextInner, dangerouslySetInnerHTML: { __html: isLocked ? t(Translation_1.Translation.Step.Room.RoomInfo.DealForMemberOnly) : rate.description }, ref: innerDescRef })),
                        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)))))),
                    react_1.default.createElement("div", { className: "u-flex justify-content-start u-marg-bottom u-marg-top-bottom@l-" },
                        requireReadMore && (react_1.default.createElement("div", { className: "u-marg-right u-inline-flex" },
                            react_1.default.createElement(BEButton_1.default, { icon: Icon_1.IconType.ArrowRight2, isText: true, primary: true, size: "small", iconPosition: "right", onClick: rateDetailsOnClick, textColor: textWhiteOr(Color_1.Color.Accent) }, readMoreOpened ? t(Translation_1.Translation.Step.Room.ReadLess) : t(Translation_1.Translation.Step.Room.ReadMore)))),
                        react_1.default.createElement(BEButton_1.default, { icon: Icon_1.IconType.ArrowRight2, isText: true, textColor: 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(RoomRatePills_1.default, { noMargin: true, rate: props.rate }))),
            react_1.default.createElement("div", { className: (0, classnames_1.default)({
                    'col-lg-6': hasPricePreview,
                    'col-lg-4': !hasPricePreview,
                }, 'u-flex flex-column align-items-start align-items-md-start align-items-lg-end justify-content-start') },
                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: TextAlignment_1.TextAlign.Right },
                        react_1.default.createElement(TaxInclusionNotice_1.TaxInclusionNotice, { rate: rate }))),
                !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: "u-marg-top--light" }, 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 }))))),
        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=ReadMoreRoomRateRow.js.map