"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 = BESummaryAddonRow;
const hooks_1 = require("../../hooks/index.js");
const core_1 = require("@roomstay/core");
const dayjs_1 = __importDefault(require("dayjs"));
const react_1 = __importDefault(require("react"));
const react_i18next_1 = require("react-i18next");
const Translation_1 = require("translations/Translation");
const BEButton_1 = __importDefault(require("../generic/BEButton"));
const Currency_1 = __importDefault(require("../generic/Currency"));
const Text_1 = __importStar(require("../generic/Text"));
const Tooltip_1 = __importDefault(require("../generic/Tooltip/Tooltip"));
const Addon_1 = require("../../models/Addon/Addon");
const Color_1 = require("../../util/Color");
function BESummaryAddonRow(props) {
    var _a, _b, _c, _d, _e, _f;
    const { row } = props;
    const { t } = (0, react_i18next_1.useTranslation)();
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const addon = row.getAddon();
    const date = row.getSelectedDate();
    const priceInfo = addon.availability[date];
    let selectedTime;
    if (row.getSelectedTime()) {
        const [hours, minutes, seconds] = row
            .getSelectedTime()
            .substr(1)
            .split(':')
            .map((it) => parseInt(it, 10));
        selectedTime = (0, dayjs_1.default)().hour(hours).minute(minutes).second(seconds);
    }
    const removeOnClickHandler = () => {
        props.onRemoveClick();
    };
    const printLine = (left, right) => {
        return (react_1.default.createElement("div", { className: "u-marg-top--light u-marg-bottom--light u-flex" },
            react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, left),
            react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.DarkGrey }, right)));
    };
    const pricingType = addon.pricingType;
    const totalFees = row.getTotalFees();
    // Calculate discount if promo code is applied
    const discount = row.getDiscountAmount((_a = props.promoCodeDetails) !== null && _a !== void 0 ? _a : undefined, props.remainingDiscount || 0);
    const priceBeforeDiscount = row.getPriceBeforeDiscount();
    let displayPrice = row.getTotalPrice();
    if (discount > 0) {
        displayPrice = row.getDiscountedTotalPrice((_b = props.promoCodeDetails) !== null && _b !== void 0 ? _b : undefined, props.remainingDiscount || 0);
    }
    // we'll show the total price without fee.
    displayPrice -= totalFees;
    return (react_1.default.createElement(react_1.default.Fragment, null, priceInfo && (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement("div", { className: "u-marg-bottom--light u-marg-top" },
            react_1.default.createElement(Text_1.default, { color: Color_1.Color.Navy },
                react_1.default.createElement("strong", null, addon.name))),
        [
            {
                price: priceInfo.price,
                quantity: row.getQuantity(),
            },
            ...(((_c = priceInfo.childPrice) !== null && _c !== void 0 ? _c : 0) > 0
                ? [
                    {
                        name: 'Adult',
                        price: (_d = priceInfo.adultPrice) !== null && _d !== void 0 ? _d : priceInfo.price,
                        quantity: row.getAdultQuantity(),
                    },
                ]
                : []),
            {
                name: 'Child',
                price: (_e = priceInfo.childPrice) !== null && _e !== void 0 ? _e : priceInfo.price,
                quantity: row.getChildQuantity(),
            },
        ].map(({ price, quantity, name }, index) => {
            let pluralName = name;
            if (quantity > 1) {
                switch (name) {
                    case 'Adult':
                        pluralName += 's';
                        break;
                    case 'Child':
                        pluralName += 'ren';
                        break;
                }
            }
            // If this is a per stay or per night addon, we only want to show the price once, if we show the name it will result in something like:
            // 2 x $100 = $200
            // 1 x Adult = $100
            // Total = $200
            if ((pricingType === core_1.EAddonPricingType.PER_STAY || pricingType === core_1.EAddonPricingType.PER_NIGHT) && name !== undefined) {
                return;
            }
            return (react_1.default.createElement(react_1.default.Fragment, { key: index }, price && quantity > 0 && (react_1.default.createElement("div", { className: "u-marg-top--light u-marg-bottom--light u-flex align-items-center" },
                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, inline: true },
                    quantity,
                    " ",
                    pluralName ? `${pluralName} ` : '',
                    "\u00D7\u00A0",
                    react_1.default.createElement(Currency_1.default, null, price)),
                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.DarkGrey, inline: true },
                    react_1.default.createElement(Currency_1.default, null, quantity * price))))));
        }),
        !row.getAddon().hideDatePicker && row.getSelectedDate() && printLine('Date', (0, dayjs_1.default)(row.getSelectedDate(), 'YYYY-MM-DD').format('D MMM')),
        selectedTime && printLine('Time', selectedTime.format('h:mma')),
        Addon_1.ADDON_PER_PERSON_PER_NIGHT_TYPES.includes(pricingType) &&
            row.getAdultQuantity() > 0 &&
            row.getChildQuantity() > 0 &&
            printLine('Sub-total', react_1.default.createElement(Currency_1.default, null, row.getAddonSubtotal())),
        core_1.ADDON_PER_NIGHT_TYPES.includes(pricingType) && printLine('Nights', row.getAddon().nights),
        discount > 0 && (react_1.default.createElement(react_1.default.Fragment, null,
            printLine(t(Translation_1.Translation.Misc.Rates), react_1.default.createElement(Currency_1.default, null, priceBeforeDiscount)),
            ((_f = props.promoCodeDetails) === null || _f === void 0 ? void 0 : _f.code) && printLine(t(Translation_1.Translation.Step.Date.PromoCode), props.promoCodeDetails.code),
            printLine(t(Translation_1.Translation.Misc.Discount), react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement("span", { className: "u-ellip-overflow" }, "-"),
                react_1.default.createElement(Currency_1.default, null, discount))))),
        printLine('Total', react_1.default.createElement(Currency_1.default, null, displayPrice)),
        react_1.default.createElement("div", { className: "u-marg-top--light u-marg-bottom--heavy u-flex justify-content-start" },
            react_1.default.createElement(Tooltip_1.default, { title: "Remove this addon" },
                react_1.default.createElement("div", null,
                    react_1.default.createElement(BEButton_1.default, { isText: true, size: "tiny", primary: true, onClick: removeOnClickHandler }, t(Translation_1.Translation.Misc.Remove)))))))));
}
//# sourceMappingURL=BESummaryAddonRow.js.map