"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 = ReservationRow;
const contexts_1 = require("../../contexts/index.js");
const hooks_1 = require("../../hooks/index.js");
const core_1 = require("@roomstay/core");
const dayjs_1 = __importDefault(require("dayjs"));
const react_1 = __importStar(require("react"));
const react_i18next_1 = require("react-i18next");
const Translation_1 = require("translations/Translation");
const BookingAPI_1 = __importDefault(require("../../api/BookingAPI"));
const BEButton_1 = __importDefault(require("../../components/generic/BEButton"));
const Currency_1 = __importDefault(require("../../components/generic/Currency"));
const Icon_1 = __importStar(require("../../components/generic/Icon/Icon"));
const ConfirmationModal_1 = __importDefault(require("../../components/generic/modal/ConfirmationModal"));
const TaxInclusionNotice_1 = require("../../components/generic/TaxInclusionNotice/TaxInclusionNotice");
const Text_1 = __importStar(require("../../components/generic/Text"));
const ImageGallerySlider_1 = __importDefault(require("../../components/steps/room/ImageGallerySlider"));
const CancelledBookingEvent_1 = require("../../events/views/CancelledBookingEvent");
const ReservationRowModal_1 = __importDefault(require("./ReservationRowModal"));
const Color_1 = require("../../util/Color");
const DataLayer_1 = __importDefault(require("../../util/DataLayer"));
const StringHelper_1 = __importDefault(require("../../util/StringHelper"));
const TextAlignment_1 = require("../../util/TextAlignment");
function ReservationRow(props) {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    const { t } = (0, react_i18next_1.useTranslation)();
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const { raise } = (0, hooks_1.useEvent)();
    const obj = props.reservation;
    const [isModalOpen, setIsModalOpen] = (0, react_1.useState)(false);
    const [isCancelled, setIsCancelled] = (0, react_1.useState)(obj.status === 'Cancelled');
    const [isCancelLoading, setIsCancelLoading] = (0, react_1.useState)(false);
    const [confirmOpen, setConfirmOpen] = (0, react_1.useState)(false);
    let adultsAndChildren = StringHelper_1.default.pluralWithDictAndCount(obj.guestCounts.adults, Translation_1.Translation.Step.Date.Adult, t);
    if (obj.guestCounts.children) {
        adultsAndChildren += ', ' + StringHelper_1.default.pluralWithDictAndCount(obj.guestCounts.children, Translation_1.Translation.Step.Date.Child, t);
    }
    const nightKeys = Object.keys(obj.nights);
    const openModalOnClick = () => {
        setIsModalOpen(true);
    };
    const closeModalOnClick = () => {
        setIsModalOpen(false);
    };
    const confirmModalOnClick = () => {
        setConfirmOpen(true);
    };
    const onConfirmHandler = () => {
        cancelRow();
        setConfirmOpen(false);
    };
    const openConfirmationHandler = () => {
        setConfirmOpen(false);
    };
    const firstNight = (0, dayjs_1.default)(nightKeys[0]);
    const lastNight = (0, dayjs_1.default)(nightKeys[nightKeys.length - 1]).add(1, 'day');
    let images = obj.images;
    if ((!images || !images.length) && (hotel === null || hotel === void 0 ? void 0 : hotel.rooms[obj.roomCode])) {
        images = hotel === null || hotel === void 0 ? void 0 : hotel.rooms[obj.roomCode].images;
    }
    const cancelRow = () => {
        setIsCancelLoading(true);
        BookingAPI_1.default.Reservation.cancelReservation(obj.reservationNumber, obj.guest.email, hotel === null || hotel === void 0 ? void 0 : hotel.hotelID)
            .then(() => {
            DataLayer_1.default.instance.sendCancellation(obj);
            raise(new CancelledBookingEvent_1.CancelledBookingEvent(obj.reservationNumber, obj.guest.email));
            setIsCancelled(true);
            setIsCancelLoading(false);
            if (props.onCancelled) {
                props.onCancelled();
            }
        })
            .catch((ex) => {
            contexts_1.ErrorHandler.add({
                title: t(Translation_1.Translation.Step.Thanks.Errors.CancelFailed),
                message: ex.Message,
            });
        });
    };
    const roomName = obj.roomType ? obj.roomType : (_a = hotel === null || hotel === void 0 ? void 0 : hotel.rooms[obj.roomCode]) === null || _a === void 0 ? void 0 : _a.name;
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement("div", { className: "reservation-row" },
            react_1.default.createElement("div", { className: "room-details +is-open " },
                react_1.default.createElement("div", { className: "room-details--image" },
                    react_1.default.createElement(ImageGallerySlider_1.default, { images: images })),
                react_1.default.createElement("div", { className: "room-details--content u-pad--heavy u-pad@m-" },
                    react_1.default.createElement("div", { className: "u-marg-bottom" },
                        react_1.default.createElement("div", { className: "room-details--content-title u-marg-bottom--light" }, !isCancelled ? (react_1.default.createElement(Text_1.default, null,
                            react_1.default.createElement("strong", null, roomName),
                            react_1.default.createElement("small", { style: { color: Color_1.Color.Grey } },
                                " #",
                                obj.reservationNumber))) : (react_1.default.createElement(Text_1.default, null,
                            react_1.default.createElement("del", null,
                                react_1.default.createElement("strong", null, roomName)),
                            react_1.default.createElement("small", { style: { color: Color_1.Color.Alert } },
                                " ",
                                t(Translation_1.Translation.Misc.Cancelled))))),
                        react_1.default.createElement("div", { className: "room-details--content-description u-marg-bottom--light" },
                            react_1.default.createElement(Text_1.default, { color: Color_1.Color.DarkGrey, type: Text_1.TextType.Small },
                                react_1.default.createElement(react_1.default.Fragment, null, hotel === null || hotel === void 0 ? void 0 :
                                    hotel.name,
                                    " - ", (_b = hotel === null || hotel === void 0 ? void 0 : hotel.address) === null || _b === void 0 ? void 0 :
                                    _b.line1)))),
                    react_1.default.createElement("div", { className: "" },
                        react_1.default.createElement("ul", null,
                            react_1.default.createElement("li", { className: "u-flex justify-content-start u-marg-bottom--light" },
                                react_1.default.createElement(Icon_1.default, { color: Color_1.Color.Grey, icon: Icon_1.IconType.Person }),
                                react_1.default.createElement("div", { className: "u-marg-left--light" },
                                    react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small },
                                        obj.guest.firstName,
                                        " ",
                                        obj.guest.lastName,
                                        " ",
                                        react_1.default.createElement("span", { style: { color: Color_1.Color.DarkGrey } },
                                            "- ",
                                            adultsAndChildren)))),
                            react_1.default.createElement("li", { className: "u-flex justify-content-start u-marg-bottom--light" },
                                react_1.default.createElement(Icon_1.default, { icon: Icon_1.IconType.Email, color: Color_1.Color.Grey }),
                                react_1.default.createElement("div", { className: "u-marg-left--light" },
                                    react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, obj.guest.email))),
                            react_1.default.createElement("li", { className: "u-flex justify-content-start u-marg-bottom--light" },
                                react_1.default.createElement(Icon_1.default, { icon: Icon_1.IconType.Calendar, color: Color_1.Color.Grey }),
                                react_1.default.createElement("div", { className: "u-marg-left--light" },
                                    react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, firstNight.format('dddd, MMM D, YYYY')),
                                    react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, lastNight.format('dddd, MMM D, YYYY')))),
                            react_1.default.createElement("li", { className: "u-flex justify-content-start" },
                                react_1.default.createElement(Icon_1.default, { icon: Icon_1.IconType.CreditCard, color: Color_1.Color.Grey }),
                                react_1.default.createElement("div", { className: "u-marg-left--light" },
                                    react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, (obj === null || obj === void 0 ? void 0 : obj.paymentMethod) === core_1.EBookingPaymentMethod.Planpay ? (t(Translation_1.Translation.Step.Confirmation.PaidWithPlanpay)) : (react_1.default.createElement(react_1.default.Fragment, null,
                                        "\u2022\u2022\u2022\u2022 \u2022\u2022\u2022\u2022 \u2022\u2022\u2022\u2022 ", (_d = (_c = obj.paymentCard) === null || _c === void 0 ? void 0 : _c.last4) !== null && _d !== void 0 ? _d : 'N/A',
                                        ' ',
                                        react_1.default.createElement("span", { style: { color: Color_1.Color.DarkGrey } },
                                            "(", (_f = (_e = obj.paymentCard) === null || _e === void 0 ? void 0 : _e.type) !== null && _f !== void 0 ? _f : 'N/A',
                                            ")")))))))))),
            react_1.default.createElement("div", { className: "reservation-row--actions" },
                react_1.default.createElement("div", { className: "u-flex justify-content-start align-items-center" },
                    react_1.default.createElement("div", { className: "u-marg-right--heavy u-inline-flex" },
                        react_1.default.createElement(BEButton_1.default, { icon: Icon_1.IconType.ArrowRight2, isText: true, primary: true, size: "tiny", iconPosition: "right", onClick: openModalOnClick }, t(Translation_1.Translation.Step.Room.RoomInfo.MoreDetails))),
                    react_1.default.createElement("div", { className: "u-inline-flex" }, (hotel === null || hotel === void 0 ? void 0 : hotel.preventCancellation) !== true && (react_1.default.createElement(BEButton_1.default, { isText: true, size: "tiny", isLoading: isCancelLoading, disabled: !obj.isCancelable || isCancelled, textColor: Color_1.Color.DarkGrey, onClick: confirmModalOnClick }, t(Translation_1.Translation.Misc.Cancel))))),
                react_1.default.createElement("div", { className: "u-flex u-flex-direction-row align-items-center" },
                    react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.DarkGrey }, t(Translation_1.Translation.Misc.Total)),
                    react_1.default.createElement("div", { className: "u-marg-left" },
                        !isCancelled ? (react_1.default.createElement(Text_1.default, { color: Color_1.Color.Navy },
                            react_1.default.createElement("strong", null,
                                react_1.default.createElement(Currency_1.default, null, (_g = obj.total.beforeFees) !== null && _g !== void 0 ? _g : obj.total.afterTax)))) : (react_1.default.createElement(Text_1.default, { color: Color_1.Color.Navy },
                            react_1.default.createElement("del", null,
                                react_1.default.createElement("strong", { className: "u-flex u-flex-direction-row align-items-center" },
                                    react_1.default.createElement(Currency_1.default, null, (_h = obj.total.beforeFees) !== null && _h !== void 0 ? _h : obj.total.afterTax))))),
                        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(ReservationRowModal_1.default, { open: isModalOpen, onClose: closeModalOnClick, reservation: props.reservation, images: images }),
        react_1.default.createElement(ConfirmationModal_1.default, { open: confirmOpen, message: "Are you sure you want to cancel this reservation?", onConfirm: onConfirmHandler, onClose: openConfirmationHandler })));
}
//# sourceMappingURL=ReservationRow.js.map