"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.PriceBreakdownFooter = void 0;
exports.default = PriceBreakdownBlock;
const contexts_1 = require("../../../../../contexts/index.js");
const hooks_1 = require("../../../../../hooks/index.js");
const ui_1 = require("@roomstay/ui");
const react_1 = __importStar(require("react"));
const react_i18next_1 = require("react-i18next");
const Translation_1 = require("translations/Translation");
const Currency_1 = __importDefault(require("../../../../generic/Currency"));
const Headline_1 = __importDefault(require("../../../../generic/Headline"));
const LineBreak_1 = __importDefault(require("../../../../generic/LineBreak"));
const TaxInclusionNotice_1 = require("../../../../generic/TaxInclusionNotice/TaxInclusionNotice");
const Text_1 = __importStar(require("../../../../generic/Text"));
const Tooltip_1 = __importDefault(require("../../../../generic/Tooltip/Tooltip"));
const RoomDetailsPriceBlock_1 = __importDefault(require("../RoomDetailsPriceBlock"));
const BookNowButton_1 = __importDefault(require("./BookNowButton"));
const useSearchScopeHook_1 = require("../../../../../hooks/useSearchScopeHook");
const WindowSize_1 = require("../../../../../hooks/WindowSize");
const Color_1 = require("../../../../../util/Color");
const StringHelper_1 = __importDefault(require("../../../../../util/StringHelper"));
const TextAlignment_1 = require("../../../../../util/TextAlignment");
const PriceBreakdownBlock_module_scss_1 = __importDefault(require("./PriceBreakdownBlock.module.scss"));
const PriceBreakdownFooter = (props) => {
    var _a, _b, _c, _d;
    const { t } = (0, react_i18next_1.useTranslation)();
    const basketContext = (0, react_1.useContext)(contexts_1.BasketContext);
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const { showChildren } = (0, useSearchScopeHook_1.useSearchScopeHook)();
    // Just like the room list, if none are selected we select the last one. Possible refactor here & room list?
    // TODO
    const basketRow = (_a = basketContext.selectedBasketRow) !== null && _a !== void 0 ? _a : basketContext.currentBasketRows[basketContext.currentBasketRows.length - 1];
    const days = [];
    const totalDays = (_c = (_b = basketRow === null || basketRow === void 0 ? void 0 : basketRow.getEndDate()) === null || _b === void 0 ? void 0 : _b.diff(basketRow === null || basketRow === void 0 ? void 0 : basketRow.getStartDate(), 'day')) !== null && _c !== void 0 ? _c : 0;
    for (let i = 0; i <= totalDays; i++) {
        const currentDay = basketRow === null || basketRow === void 0 ? void 0 : basketRow.getStartDate().add(i, 'day');
        if (currentDay) {
            days.push(currentDay.format('dddd, MMM DD, YYYY'));
        }
    }
    const bookButtonLabel = props.getButtonLabel ? props.getButtonLabel(false, false) : t(Translation_1.Translation.Misc.BookNow);
    return (react_1.default.createElement("div", { className: "u-flex align-items-center" },
        react_1.default.createElement(Text_1.default, { color: Color_1.Color.DarkGrey },
            basketRow !== null && (react_1.default.createElement(react_1.default.Fragment, null,
                StringHelper_1.default.pluralWithDictAndCount(basketRow === null || basketRow === void 0 ? void 0 : basketRow.getAdults(), Translation_1.Translation.Step.Date.Adult, t),
                ",\u00A0",
                showChildren ? react_1.default.createElement(react_1.default.Fragment, null,
                    " ",
                    StringHelper_1.default.pluralWithDictAndCount(basketRow === null || basketRow === void 0 ? void 0 : basketRow.getChildren(), Translation_1.Translation.Step.Date.Child, t),
                    ",\u00A0") : null,
                ((_d = hotel === null || hotel === void 0 ? void 0 : hotel.childConfiguration) === null || _d === void 0 ? void 0 : _d.supportInfants) ? (react_1.default.createElement(react_1.default.Fragment, null,
                    " ",
                    StringHelper_1.default.pluralWithDictAndCount(basketRow === null || basketRow === void 0 ? void 0 : basketRow.getInfants(), Translation_1.Translation.Step.Date.Infant, t),
                    ",\u00A0")) : null)),
            StringHelper_1.default.pluralWithDictAndCount(days.length - 1, Translation_1.Translation.Misc.Night, t)),
        react_1.default.createElement("div", { className: "u-flex align-items-center" },
            react_1.default.createElement(BookNowButton_1.default, { isFilled: true, rate: props.rate, buttonLabel: bookButtonLabel, onAddToBasketOverride: props.onAddToBasketOverride }))));
};
exports.PriceBreakdownFooter = PriceBreakdownFooter;
const renderNightPrice = (rate, nightRate) => {
    const { t } = (0, react_i18next_1.useTranslation)();
    const isStayXPayY = !!(rate.nightsToDiscount && Number(rate.nightsToDiscount) > 0);
    const isFreeNight = Number(nightRate.cost) === 0 && Number(nightRate.discount) > 0;
    // Show "Free" for Stay X Pay Y free nights
    if (isStayXPayY && isFreeNight) {
        return (react_1.default.createElement("span", { className: "u-primary u-bold", style: { color: Color_1.Color.Success } }, t(Translation_1.Translation.Step.Room.FreeNight)));
    }
    // Regular pricing, with strikethrough of undiscounted price (if there is a discount)
    return (react_1.default.createElement(react_1.default.Fragment, null,
        Number(nightRate.discount) > 0 && (react_1.default.createElement("span", { style: {
                textDecoration: 'line-through',
                fontSize: '14px',
                height: '1.1em',
                position: 'absolute',
                top: '0',
                right: '0',
                transform: 'translateY(-100%)',
            }, className: "u-cross-out" },
            react_1.default.createElement(Currency_1.default, null, Number(nightRate.cost) + Number(nightRate.discount)))),
        react_1.default.createElement("span", { className: "u-primary u-bold" },
            react_1.default.createElement(Currency_1.default, null, nightRate.cost))));
};
function PriceBreakdownBlock(props) {
    var _a, _b;
    const { t } = (0, react_i18next_1.useTranslation)();
    const ccx = (0, react_1.useContext)(contexts_1.CompanyContext);
    const basketContext = react_1.default.useContext(contexts_1.BasketContext);
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const selectedRow = (_a = basketContext.selectedBasketRow) !== null && _a !== void 0 ? _a : basketContext.currentBasketRows[basketContext.currentBasketRows.length - 1];
    const { rate } = props;
    const days = rate.getLabelsByDate({ startDate: selectedRow.getStartDate(), endDate: selectedRow.getEndDate() });
    const nightRates = Object.values(rate.nightRates);
    let isLocked = false;
    if (rate.isMemberOnly && !ccx.signedInUser) {
        isLocked = true;
    }
    const totalFees = rate.getTotalFees();
    const totalTax = rate.getTotalTaxes();
    const itemisedFees = rate.getItemisedFees((_b = hotel === null || hotel === void 0 ? void 0 : hotel.fees) !== null && _b !== void 0 ? _b : []);
    const { isMobile } = (0, WindowSize_1.useWindowSize)();
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement("div", { className: "row" },
            react_1.default.createElement("div", { className: "col-md-6" },
                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Label, color: Color_1.Color.DarkGrey }, t(Translation_1.Translation.Step.Room.Filter.RoomType)),
                react_1.default.createElement("div", { className: "u-flex" },
                    react_1.default.createElement(Headline_1.default, null,
                        react_1.default.createElement("strong", null, rate.getRoom().name)))),
            react_1.default.createElement("div", { className: "col-md-6" },
                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Label, color: Color_1.Color.DarkGrey }, t(Translation_1.Translation.Step.Room.Filter.RateType)),
                react_1.default.createElement("div", { className: "u-flex" },
                    react_1.default.createElement(Headline_1.default, null,
                        react_1.default.createElement("strong", null, rate.name))))),
        react_1.default.createElement(LineBreak_1.default, null),
        days.map((day, index) => {
            const isLastDay = index === days.length - 1;
            const nightRate = nightRates[index];
            if (!nightRate && !isLastDay) {
                return;
            }
            return (react_1.default.createElement("div", { className: "u-flex u-marg-bottom", key: index },
                react_1.default.createElement(Text_1.default, { className: isMobile ? PriceBreakdownBlock_module_scss_1.default['price-breakdown-label'] : null },
                    react_1.default.createElement("span", null,
                        day.label,
                        " "),
                    day.alternateLabel && !isLastDay && (react_1.default.createElement(react_1.default.Fragment, null,
                        react_1.default.createElement("span", { className: PriceBreakdownBlock_module_scss_1.default['price-breakdown-label-override'] },
                            !isMobile && '-',
                            " ",
                            day.alternateLabel,
                            day.shortDescription && (react_1.default.createElement(Tooltip_1.default, { title: day.shortDescription },
                                react_1.default.createElement("div", { className: "u-marg-left--light d-flex" },
                                    react_1.default.createElement(ui_1.Icon, { icon: ui_1.IconType.Info })))))))),
                isLastDay ? (react_1.default.createElement(Text_1.default, { color: Color_1.Color.Success }, t(Translation_1.Translation.Misc.CheckOut))) : (react_1.default.createElement(Text_1.default, { color: Color_1.Color.DarkGrey, className: "u-pos-rel" }, renderNightPrice(rate, nightRate)))));
        }),
        !!totalFees && !(itemisedFees === null || itemisedFees === void 0 ? void 0 : itemisedFees.length) && (react_1.default.createElement("div", { className: "u-flex u-marg-bottom" },
            react_1.default.createElement(Text_1.default, null, t(Translation_1.Translation.Step.Room.Summary.Fees)),
            react_1.default.createElement(Text_1.default, { color: Color_1.Color.DarkGrey },
                react_1.default.createElement(Currency_1.default, null, totalFees)))),
        !!(itemisedFees === null || itemisedFees === void 0 ? void 0 : itemisedFees.length) && (react_1.default.createElement(react_1.default.Fragment, null,
            react_1.default.createElement(LineBreak_1.default, null),
            react_1.default.createElement("div", { className: "u-marg-bottom" }, itemisedFees.map((fee) => (react_1.default.createElement("div", { key: fee.hotelFee.name, className: "u-flex" },
                react_1.default.createElement(Text_1.default, null,
                    react_1.default.createElement(Text_1.default, { inline: true, bold: true }, fee.hotelFee.name),
                    fee.hotelFee.description && ` (${fee.hotelFee.description})`),
                react_1.default.createElement(Text_1.default, { color: Color_1.Color.DarkGrey },
                    react_1.default.createElement("span", { className: "u-primary u-bold" },
                        react_1.default.createElement(Currency_1.default, null, fee.amount))))))))),
        !!totalTax && (react_1.default.createElement("div", { className: "u-flex u-marg-bottom" },
            react_1.default.createElement(Text_1.default, null, t(Translation_1.Translation.Step.Room.Summary.Taxes)),
            react_1.default.createElement(Text_1.default, { color: Color_1.Color.DarkGrey },
                react_1.default.createElement(Currency_1.default, null, totalTax)))),
        react_1.default.createElement("div", { className: "u-marg-top-bottom--heavy u-flex flex-column align-items-end" },
            react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.Navy }, t(Translation_1.Translation.Misc.Total)),
            react_1.default.createElement(RoomDetailsPriceBlock_1.default, { isLocked: isLocked, averagePreDiscount: rate.getTotalPriceBeforeDiscount(), averagePrice: rate.getTotalPrice(), isMemberOnly: props.rate.isMemberOnly, hidePerNight: true }),
            react_1.default.createElement("div", { className: "u-marg-bottom" },
                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Label, color: Color_1.Color.DarkGrey, align: TextAlignment_1.TextAlign.Right },
                    react_1.default.createElement(TaxInclusionNotice_1.TaxInclusionNotice, { rate: rate }))))));
}
//# sourceMappingURL=PriceBreakdownBlock.js.map