"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.ReservationAction = void 0;
const contexts_1 = require("../../contexts/index.js");
const core_1 = require("@roomstay/core");
const classnames_1 = __importDefault(require("classnames"));
const dayjs_1 = __importDefault(require("dayjs"));
const react_1 = __importStar(require("react"));
const react_i18next_1 = require("react-i18next");
const react_router_dom_1 = require("react-router-dom");
const Translation_1 = require("translations/Translation");
const EventCarouselContainer_1 = __importDefault(require("../Event/EventCarouselContainer"));
const BEButton_1 = __importDefault(require("../generic/BEButton"));
const Headline_1 = __importDefault(require("../generic/Headline"));
const Icon_1 = __importStar(require("../generic/Icon/Icon"));
const NoPrint_1 = __importDefault(require("../generic/print/NoPrint"));
const Text_1 = __importStar(require("../generic/Text"));
const ImageGallerySlider_1 = __importDefault(require("../steps/room/ImageGallerySlider"));
const hooks_1 = require("../../hooks");
const AccountRoutes_1 = require("../../pages/account/AccountRoutes");
const ModifyBookingFeature_1 = __importDefault(require("../../providers/feature/ModifyBookingFeature"));
const ImageProvider_1 = require("../../providers/ImageProvider");
const Color_1 = require("../../util/Color");
const EventsHelper_1 = require("../../util/EventsHelper");
const ScreenSize_1 = __importDefault(require("../../util/ScreenSize"));
const StepManager_1 = __importDefault(require("../../util/StepManager"));
const TextAlignment_1 = require("../../util/TextAlignment");
const GroupedCheckInOutTimes_1 = require("../generic/GroupedCheckInOutTimes/GroupedCheckInOutTimes");
const ReservationItem_module_scss_1 = __importDefault(require("./ReservationItem.module.scss"));
var ReservationAction;
(function (ReservationAction) {
    ReservationAction["MoreDetails"] = "MoreDetails";
    ReservationAction["BookAgain"] = "BookAgain";
    ReservationAction["Cancel"] = "Cancel";
    ReservationAction["Modify"] = "Modify";
})(ReservationAction || (exports.ReservationAction = ReservationAction = {}));
const defaultActions = [ReservationAction.MoreDetails];
const ReservationItem = (props) => {
    var _a, _b, _c, _d, _e;
    const { itineraryItem, disableMarginBottom, actions = defaultActions, showCancelledNotifications = false, defaultEventsExpanded, navPath } = props;
    // no events should be shown on “Past Stays”  or “Cancelled Stays”
    const hideEvents = (0, EventsHelper_1.isCancelledBooking)(itineraryItem) || (0, EventsHelper_1.isPastBooking)(itineraryItem);
    const { t } = (0, react_i18next_1.useTranslation)();
    const context = (0, react_1.useContext)(contexts_1.BookingEngineContext);
    const { getHotel } = (0, react_1.useContext)(contexts_1.CompanyContext);
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const [bookingHotel, setBookingHotel] = (0, react_1.useState)();
    const isMobile = context.screenSize <= ScreenSize_1.default.Large;
    const nameSize = isMobile ? 'normal' : 'large';
    const renderActions = () => actions.map((action, index) => {
        const defaultButtonProps = {
            mobileWidth: true,
            rounded: true,
            wide: isMobile,
            key: index,
            className: (0, classnames_1.default)({
                'u-marg-right@m': index === 0,
            }),
        };
        const onActionClick = () => {
            if (props.onActionClick) {
                props.onActionClick(action, itineraryItem);
            }
        };
        /**
         * Only renders Cancel/Modify if the booking is NOT cancelled
         */
        if (cancelled && [ReservationAction.Cancel, ReservationAction.Modify].includes(action)) {
            return;
        }
        /**
         * Only renders BookAgain if booking IS cancelled
         */
        if (!cancelled && action === ReservationAction.BookAgain) {
            return;
        }
        switch (action) {
            case ReservationAction.Modify:
                return ModifyBookingFeature_1.default.isActive() ? (react_1.default.createElement(BEButton_1.default, Object.assign({}, defaultButtonProps, { icon: Icon_1.IconType.ArrowRight, iconPosition: "right", onClick: onActionClick, disabled: !itineraryItem.isCancellable }), t(Translation_1.Translation.Reservation.ModifyThisRoom))) : null;
            case ReservationAction.Cancel:
                return (react_1.default.createElement(BEButton_1.default, Object.assign({}, defaultButtonProps, { danger: true, onClick: onActionClick, disabled: !itineraryItem.isCancellable }), t(Translation_1.Translation.Reservation.CancelReservation)));
            case ReservationAction.BookAgain:
                return (react_1.default.createElement(react_router_dom_1.Link, { to: StepManager_1.default.getFirstStep().getStepUrl(), key: index },
                    react_1.default.createElement(BEButton_1.default, Object.assign({}, defaultButtonProps, { icon: Icon_1.IconType.ArrowRight, iconPosition: "right", onClick: onActionClick }), t(Translation_1.Translation.Reservation.BookAgain))));
            case ReservationAction.MoreDetails:
                return (react_1.default.createElement(react_router_dom_1.Link, { to: `${AccountRoutes_1.AccountRoutes.SingleReservation.path}${`${navPath}/`}${itineraryItem.bookingId}/item`, key: index },
                    react_1.default.createElement(BEButton_1.default, Object.assign({}, defaultButtonProps, { primary: true, filled: true, icon: Icon_1.IconType.ArrowRight, iconPosition: "right" }), t(Translation_1.Translation.Reservation.MoreDetails))));
        }
    });
    if (!itineraryItem) {
        return react_1.default.createElement(react_1.default.Fragment, null);
    }
    (0, react_1.useEffect)(() => {
        if (itineraryItem === null || itineraryItem === void 0 ? void 0 : itineraryItem.hotelId) {
            getHotel(itineraryItem.hotelId).then((hotel) => {
                setBookingHotel(hotel);
            });
        }
    }, [itineraryItem === null || itineraryItem === void 0 ? void 0 : itineraryItem.hotelId, getHotel]);
    const itineraryItemNights = Object.keys(itineraryItem.nights);
    const checkInDate = (0, dayjs_1.default)(itineraryItem.checkInDate);
    const checkOutDate = (0, dayjs_1.default)(itineraryItem.checkOutDate);
    const reservationOccupancy = itineraryItem.adults + ((itineraryItem === null || itineraryItem === void 0 ? void 0 : itineraryItem.children) || 0);
    const cancelled = itineraryItem.status === core_1.EBookingStatus.Cancelled;
    const colorProfile = (_a = itineraryItem === null || itineraryItem === void 0 ? void 0 : itineraryItem.hotel) === null || _a === void 0 ? void 0 : _a.colors;
    return (react_1.default.createElement("div", { id: `reservation-${itineraryItem.id}`, className: (0, classnames_1.default)({ 'u-marg-bottom--heavy u-marg-bottom@m-': !disableMarginBottom }) },
        showCancelledNotifications && cancelled && (react_1.default.createElement("div", { className: ReservationItem_module_scss_1.default['cancelledNotification'] },
            react_1.default.createElement("div", { className: ReservationItem_module_scss_1.default['cancelledNotification-icon'] },
                react_1.default.createElement(Icon_1.default, { icon: Icon_1.IconType.Error, size: "44px", color: Color_1.Color.Danger })),
            react_1.default.createElement("div", null,
                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, bold: true, color: Color_1.Color.Danger, className: ReservationItem_module_scss_1.default['cancelledNotification-title'] }, t(Translation_1.Translation.Reservation.Notification.ThisRoomIsCancelled.Title)),
                react_1.default.createElement(react_i18next_1.Trans, { i18nKey: Translation_1.Translation.Reservation.Notification.ThisRoomIsCancelled.Detail, components: [react_1.default.createElement(react_router_dom_1.Link, { key: 0, to: AccountRoutes_1.AccountRoutes.Help.path, style: { color: Color_1.Color.Accent } })] })))),
        react_1.default.createElement("div", { className: "reservation-item-container" },
            !!colorProfile && (react_1.default.createElement("style", null, `
                        #reservation-${itineraryItem.id} {
                            ${(0, Color_1.getAccentOverrideStyleContent)(colorProfile)}
                            }
                        `)),
            react_1.default.createElement("div", { className: "reservation-item" },
                react_1.default.createElement("div", { className: (0, classnames_1.default)('reservation-item--image u-border-rounded u-print-none', { 'reservation-item--disabled': cancelled }) },
                    react_1.default.createElement(ImageGallerySlider_1.default, { images: itineraryItem.images, imageSize: ImageProvider_1.ImageSize.Medium }),
                    react_1.default.createElement("div", { className: "reservation-item--floating-date" },
                        react_1.default.createElement(Headline_1.default, { size: "extra-large", color: cancelled ? Color_1.Color.DarkGrey : Color_1.Color.White, align: TextAlignment_1.TextAlign.Center }, checkInDate.format('DD')),
                        react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: cancelled ? Color_1.Color.DarkGrey : Color_1.Color.White, bold: true }, checkInDate.format('MMM YYYY')))),
                react_1.default.createElement("div", { className: "reservation-item--content u-pad--heavy u-pad-left-none@m- u-pad-right-none@m- u-pad-bottom-none@m- u-print-pad-left-none" },
                    react_1.default.createElement("div", { className: "reservation-item--content-title u-marg-bottom--light" },
                        (hotel === null || hotel === void 0 ? void 0 : hotel.hotelID) !== itineraryItem.hotelId && (react_1.default.createElement(Text_1.default, { color: Color_1.Color.Accent, size: nameSize, bold: true }, itineraryItem.hotel.name)),
                        react_1.default.createElement(Headline_1.default, { bold: true, size: nameSize }, itineraryItem.roomName)),
                    react_1.default.createElement("div", { className: "text-body u-marg-bottom" }, ((_c = (_b = itineraryItem === null || itineraryItem === void 0 ? void 0 : itineraryItem.profile) === null || _b === void 0 ? void 0 : _b.firstName) !== null && _c !== void 0 ? _c : '') + ' ' + ((_e = (_d = itineraryItem === null || itineraryItem === void 0 ? void 0 : itineraryItem.profile) === null || _d === void 0 ? void 0 : _d.lastName) !== null && _e !== void 0 ? _e : '')),
                    react_1.default.createElement("div", { className: "reservation-item--info u-marg-bottom--heavy" },
                        react_1.default.createElement("div", { className: "u-flex u-flex-gap--light" },
                            react_1.default.createElement("div", { className: "u-flex u-flex-nowrap u-flex-gap--light" },
                                react_1.default.createElement(Icon_1.default, { icon: Icon_1.IconType.Calendar, color: Color_1.Color.Grey }),
                                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, checkInDate.format('D MMM, YYYY'))),
                            react_1.default.createElement("div", { className: "u-flex u-flex-nowrap u-flex-gap--light" },
                                react_1.default.createElement(Icon_1.default, { icon: Icon_1.IconType.ArrowRight2, color: Color_1.Color.Grey }),
                                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, checkOutDate.format('D MMM, YYYY')))),
                        react_1.default.createElement("div", { className: "u-flex u-flex-gap" },
                            react_1.default.createElement("div", { className: "u-flex u-flex-nowrap u-flex-gap--light" },
                                react_1.default.createElement(Icon_1.default, { icon: Icon_1.IconType.LateCheckout, color: Color_1.Color.Grey }),
                                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, `${itineraryItemNights.length} night${itineraryItemNights.length > 1 ? 's' : ''}`)),
                            react_1.default.createElement("div", { className: "u-flex u-flex-nowrap u-flex-gap--light" },
                                react_1.default.createElement(Icon_1.default, { icon: Icon_1.IconType.Person, color: Color_1.Color.Grey }),
                                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, `${reservationOccupancy} guest${reservationOccupancy > 1 ? 's' : ''}`)))),
                    react_1.default.createElement(GroupedCheckInOutTimes_1.GroupedCheckInOutTimes, { hotel: bookingHotel, wrapperClassName: "reservation-item--checkin-checkout u-flex justify-content-start u-pad u-border-rounded", colClassName: "u-marg-right--heavy", CheckInRender: () => (react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.DarkGrey, className: "u-marg-bottom--light" }, t(Translation_1.Translation.Misc.CheckIn))), CheckOutRender: () => (react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.DarkGrey, className: "u-marg-bottom--light" }, t(Translation_1.Translation.Misc.CheckOut))), LineRender: ({ line }) => {
                            const TextContainer = isMobile ? Text_1.default : Headline_1.default;
                            return react_1.default.createElement(TextContainer, null, line);
                        } }),
                    react_1.default.createElement("div", { className: "reservation-item--actions u-flex justify-content-start u-print-none u-marg-top--heavy" }, renderActions())))),
        hideEvents ? null : (react_1.default.createElement(NoPrint_1.default, null,
            react_1.default.createElement("div", { className: "u-marg-top--medium" },
                react_1.default.createElement(EventCarouselContainer_1.default, { defaultExpanded: defaultEventsExpanded, itineraryItem: itineraryItem }))))));
};
exports.default = ReservationItem;
//# sourceMappingURL=ReservationItem.js.map