"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.BESummaryLayoutOrder = void 0;
exports.BESummary = BESummary;
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 Alert_1 = __importStar(require("../generic/Alert"));
const Currency_1 = __importDefault(require("../generic/Currency"));
const Headline_1 = __importDefault(require("../generic/Headline"));
const Icon_1 = require("../generic/Icon/Icon");
const LineBreak_1 = __importDefault(require("../generic/LineBreak"));
const BEHotelLocationMapBlock_1 = __importDefault(require("../generic/map/BEHotelLocationMapBlock"));
const Text_1 = __importStar(require("../generic/Text"));
const ItemisedFeesListing_1 = require("../steps/common/ItemisedFeesListing/ItemisedFeesListing");
const BESummaryAddonRow_1 = __importDefault(require("./BESummaryAddonRow"));
const BESummaryPerkBlock_1 = require("./BESummaryPerkBlock");
const BESummaryRoomRow_1 = __importDefault(require("./BESummaryRoomRow"));
const TransportDistanceFromHotelBlock_1 = __importDefault(require("./TransportDistanceFromHotelBlock"));
const StepConfirmation_1 = __importDefault(require("../../pages/steps/StepConfirmation/StepConfirmation"));
const Color_1 = require("../../util/Color");
const TextAlignment_1 = require("../../util/TextAlignment");
const AddonDiscountCalculator_1 = require("../../utils/AddonDiscountCalculator");
const OverlayLoader_1 = require("../generic/loader/OverlayLoader");
const Pill_1 = __importStar(require("../generic/Pill"));
const TaxInclusionNotice_1 = require("../generic/TaxInclusionNotice/TaxInclusionNotice");
const TransportDistanceFromHotelBlockContainer = () => {
    var _a;
    const overridesContext = (0, react_1.useContext)(contexts_1.HotelOverridesContext);
    return (react_1.default.createElement("div", { className: (0, classnames_1.default)('u-flex u-flex-column u-marg-top u-w-100', {
            'u-flex-justify-around': Object.entries((_a = overridesContext === null || overridesContext === void 0 ? void 0 : overridesContext.transportDistances) !== null && _a !== void 0 ? _a : {}).filter(([key, value]) => !!value).length <= 2,
        }) },
        react_1.default.createElement(TransportDistanceFromHotelBlock_1.default, { vertical: true })));
};
exports.BESummaryLayoutOrder = {
    TransportDistanceBlock: {
        component: react_1.default.createElement(TransportDistanceFromHotelBlockContainer, null),
        priority: 5,
    },
    PerksBlock: {
        component: (react_1.default.createElement("div", { className: "u-marg-bottom--light u-marg-top--heavy", style: { marginTop: '16px' } },
            react_1.default.createElement(BESummaryPerkBlock_1.BESummaryPerkBlock, { size: "desktop" }))),
        priority: 10,
    },
};
function BESummary(props) {
    var _a, _b, _c, _d, _e;
    const basketContext = (0, react_1.useContext)(contexts_1.BasketContext);
    const ccx = (0, react_1.useContext)(contexts_1.CompanyContext);
    const overridesContext = (0, react_1.useContext)(contexts_1.HotelOverridesContext);
    const engineContext = (0, react_1.useContext)(contexts_1.FullPageEngineContext);
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const selectedBasketRows = basketContext.getAllValidRows();
    const addons = basketContext.basketAddonRows;
    const { t } = (0, react_i18next_1.useTranslation)();
    const addressLine1 = [(_a = hotel === null || hotel === void 0 ? void 0 : hotel.address) === null || _a === void 0 ? void 0 : _a.line1, (_b = hotel === null || hotel === void 0 ? void 0 : hotel.address) === null || _b === void 0 ? void 0 : _b.city];
    const addressLine2 = [(_c = hotel === null || hotel === void 0 ? void 0 : hotel.address) === null || _c === void 0 ? void 0 : _c.postalCode, (_d = hotel === null || hotel === void 0 ? void 0 : hotel.address) === null || _d === void 0 ? void 0 : _d.state];
    const { sortedComponents } = (0, hooks_1.useElementSort)(exports.BESummaryLayoutOrder);
    const toString = (val) => val.filter((it) => !!it && it !== '0').join(', ');
    const renderLine = (line, key) => {
        if (line) {
            return (react_1.default.createElement(Text_1.default, { key: key, type: Text_1.TextType.Small }, line));
        }
        return null;
    };
    const combineAddress = (rawLine1, rawLine2) => {
        const line1 = toString(rawLine1);
        const line2 = toString(rawLine2);
        if (line1 && line1.length > 40)
            return renderLine([line1, line2].filter(Boolean).join(', '));
        return [renderLine(line1, 'addressLine1'), renderLine(line2, 'addressLine2')];
    };
    const showBasketItems = !!selectedBasketRows.length;
    const depositFees = basketContext.getDepositQuoteFees();
    const totalFees = basketContext.getTotalFees();
    const itemisedFees = basketContext.getItemisedFees();
    const totalSection = (0, react_1.useMemo)(() => {
        var _a;
        const totalPayNow = basketContext.getTotalPayNow();
        if (hotel && totalPayNow && ((_a = engineContext.currentStep) === null || _a === void 0 ? void 0 : _a.getStepUrl()) === StepConfirmation_1.default.getStepUrl()) {
            const isLoading = basketContext.isLoadingPriceQuote;
            return (react_1.default.createElement("div", { className: "u-marg-top--light", style: { position: 'relative' } },
                isLoading && react_1.default.createElement(OverlayLoader_1.OverlayLoader, null),
                totalFees > 0 && !(itemisedFees === null || itemisedFees === void 0 ? void 0 : itemisedFees.length) && (
                // We only show the total fees here if we don't have itemised fees available.
                react_1.default.createElement("div", { className: "u-marg-top--light" },
                    react_1.default.createElement("div", { className: "u-flex" },
                        react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small },
                            " ",
                            t(Translation_1.Translation.Step.Room.Summary.CreditCardSurcharge)),
                        react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small },
                            react_1.default.createElement(Currency_1.default, null, totalFees))))),
                !!(itemisedFees === null || itemisedFees === void 0 ? void 0 : itemisedFees.length) && depositFees > 0 && hotel.addDepositFeeToBookingTotal && (
                // Because we're already showing itemised fees above the totals section, we only need to show the deposit fee here.
                // And only if the hotel is configured to add the deposit fee to the booking total.
                react_1.default.createElement("div", { className: "u-marg-top--light" },
                    react_1.default.createElement("div", { className: "u-flex" },
                        react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small },
                            " ",
                            t(Translation_1.Translation.Step.Room.Summary.CreditCardSurcharge)),
                        react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small },
                            react_1.default.createElement(Currency_1.default, null, depositFees))))),
                react_1.default.createElement("div", { className: "u-marg-top--light" },
                    react_1.default.createElement("div", { className: "u-flex" },
                        react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, t(Translation_1.Translation.Misc.Total)),
                        react_1.default.createElement(Headline_1.default, { bold: true, size: "large" },
                            react_1.default.createElement(Currency_1.default, null, basketContext.getTotalPrice()))),
                    hotel.defaultCurrency && ccx.currentCurrency !== (hotel === null || hotel === void 0 ? void 0 : hotel.defaultCurrency) && (react_1.default.createElement("div", { className: "u-align-right rs-summary-currency-display-enhancements" },
                        react_1.default.createElement(Pill_1.default, { type: Pill_1.PillType.Grey },
                            react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.Navy, align: TextAlignment_1.TextAlign.Right, inline: true },
                                react_1.default.createElement(Currency_1.default, { toCurrencyCode: hotel.defaultCurrency }, basketContext.getTotalPrice()))))),
                    react_1.default.createElement(Text_1.default, { color: Color_1.Color.DarkGrey, type: Text_1.TextType.Small, align: TextAlignment_1.TextAlign.Right },
                        react_1.default.createElement(TaxInclusionNotice_1.TaxInclusionNotice, null))),
                react_1.default.createElement("div", { className: "u-marg-top--light u-flex" },
                    react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, t(Translation_1.Translation.Misc.PayNow)),
                    react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small },
                        react_1.default.createElement(Currency_1.default, null, totalPayNow)))));
        }
        else {
            return (react_1.default.createElement("div", { className: "u-marg-top--light" },
                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, align: TextAlignment_1.TextAlign.Right }, t(Translation_1.Translation.Misc.Total)),
                react_1.default.createElement(Headline_1.default, { bold: true, size: "large", align: TextAlignment_1.TextAlign.Right },
                    react_1.default.createElement(Currency_1.default, null, basketContext.getTotalPrice())),
                react_1.default.createElement(Text_1.default, { color: Color_1.Color.DarkGrey, type: Text_1.TextType.Small, align: TextAlignment_1.TextAlign.Right },
                    react_1.default.createElement(TaxInclusionNotice_1.TaxInclusionNotice, null))));
        }
    }, [
        basketContext.currentBasketRows,
        basketContext.isLoadingPriceQuote,
        basketContext.getTotalPrice,
        hotel === null || hotel === void 0 ? void 0 : hotel.rmsUseDepositQuote,
        (_e = engineContext.currentStep) === null || _e === void 0 ? void 0 : _e.getStepUrl(),
        ccx.currentCurrency,
        hotel === null || hotel === void 0 ? void 0 : hotel.defaultCurrency,
    ]);
    return (react_1.default.createElement("div", { className: "booking-engine-summary" },
        react_1.default.createElement("div", { className: "u-marg-bottom" },
            react_1.default.createElement(Headline_1.default, { bold: true }, t(Translation_1.Translation.Misc.Summary))),
        showBasketItems ? (react_1.default.createElement(react_1.default.Fragment, null,
            selectedBasketRows.map((row, index) => {
                if (!row.isValid())
                    return;
                return react_1.default.createElement(BESummaryRoomRow_1.default, { key: row.getID(), row: row, index: index, showImages: !!props.showImages });
            }),
            addons.length > 0 && (react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement(LineBreak_1.default, { noMargins: true, darker: true }),
                react_1.default.createElement("div", { className: "u-marg-top" },
                    react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Label, color: Color_1.Color.DarkGrey },
                        react_1.default.createElement("strong", null, t(Translation_1.Translation.Step.Thanks.AddOnItems)))),
                AddonDiscountCalculator_1.AddonDiscountCalculator.calculateIndividualDiscounts(addons, selectedBasketRows, basketContext.promoCodeDetails).map(({ addon, remainingDiscount }) => {
                    const onRemoveClickHandler = () => {
                        return basketContext.removeBasketAddonRow(addon);
                    };
                    return (react_1.default.createElement(BESummaryAddonRow_1.default, { row: addon, key: addon.getID(), onRemoveClick: onRemoveClickHandler, promoCodeDetails: basketContext.promoCodeDetails, remainingDiscount: remainingDiscount }));
                }))),
            totalFees > 0 && !(itemisedFees === null || itemisedFees === void 0 ? void 0 : itemisedFees.length) && (react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement("div", { className: "line-break", style: { marginTop: 0, marginBottom: '24px' } }),
                react_1.default.createElement("div", { className: "u-flex" },
                    react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small },
                        " ",
                        t(Translation_1.Translation.Step.Room.Summary.CreditCardSurcharge)),
                    react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small },
                        react_1.default.createElement(Currency_1.default, null, totalFees))))),
            !!(itemisedFees === null || itemisedFees === void 0 ? void 0 : itemisedFees.length) && react_1.default.createElement(ItemisedFeesListing_1.ItemisedFeesListing, { fees: itemisedFees }))) : (react_1.default.createElement("div", { className: "u-marg-bottom rs-summary-hotel-info" },
            react_1.default.createElement("div", { className: "u-marg-bottom" },
                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, t(Translation_1.Translation.Step.General.YourCartIsEmpty))),
            react_1.default.createElement("div", { className: "rs-summary-hotel-location-map", style: { height: '150px', position: 'relative' } },
                react_1.default.createElement(BEHotelLocationMapBlock_1.default, null)),
            react_1.default.createElement("div", { className: "u-marg-top" },
                react_1.default.createElement("div", { className: "u-marg-bottom rs-summary-hotel-name" },
                    react_1.default.createElement(Text_1.default, { bold: true }, hotel === null || hotel === void 0 ? void 0 : hotel.name)),
                combineAddress(addressLine1, addressLine2),
                (overridesContext === null || overridesContext === void 0 ? void 0 : overridesContext.phoneNumberInSummary) && (hotel === null || hotel === void 0 ? void 0 : hotel.phone) && react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, hotel === null || hotel === void 0 ? void 0 : hotel.phone)))),
        sortedComponents,
        ccx.currentCurrency !== (hotel === null || hotel === void 0 ? void 0 : hotel.defaultCurrency) && (react_1.default.createElement(Alert_1.default, { iconAlignCenter: true, icon: Icon_1.IconType.Money, type: Alert_1.AlertType.LightGray },
            react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, t(Translation_1.Translation.Step.General.AlternateCurrencyInformation)))),
        react_1.default.createElement(LineBreak_1.default, { darker: true }),
        totalSection));
}
//# sourceMappingURL=BESummary.js.map