"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 = StepThanksComponent;
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 react_router_dom_1 = require("react-router-dom");
const Translation_1 = require("translations/Translation");
const BEButton_1 = __importDefault(require("../../../components/generic/BEButton"));
const Currency_1 = __importDefault(require("../../../components/generic/Currency"));
const Headline_1 = __importDefault(require("../../../components/generic/Headline"));
const Icon_1 = __importStar(require("../../../components/generic/Icon/Icon"));
const LineBreak_1 = __importDefault(require("../../../components/generic/LineBreak"));
const TaxInclusionNotice_1 = require("../../../components/generic/TaxInclusionNotice/TaxInclusionNotice");
const Text_1 = __importStar(require("../../../components/generic/Text"));
const InlineSignUpSection_1 = require("../../../components/members/InlineSignUpSection");
const CommonAddonSummary_1 = require("../../../components/steps/common/CommonAddonSummary/CommonAddonSummary");
const ItemisedFeesSummary_1 = require("../../../components/steps/common/ItemisedFeesSummary/ItemisedFeesSummary");
const events_1 = require("../../../events");
const ReservationRow_1 = __importDefault(require("../../findReservation/ReservationRow"));
const Color_1 = require("../../../util/Color");
const TextAlignment_1 = require("../../../util/TextAlignment");
function StepThanksComponent() {
    var _a, _b, _c, _d, _e;
    const basketContext = (0, react_1.useContext)(contexts_1.BasketContext);
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const ccx = (0, react_1.useContext)(contexts_1.CompanyContext);
    const { raise } = (0, hooks_1.useEvent)();
    const [itemisedFees, setItemisedFees] = (0, react_1.useState)([]);
    const checkoutData = basketContext.reservationData;
    const isFromRoomstay = (_a = checkoutData === null || checkoutData === void 0 ? void 0 : checkoutData.itineraryNumber) === null || _a === void 0 ? void 0 : _a.startsWith('RS');
    const { t } = (0, react_i18next_1.useTranslation)();
    const total = (0, react_1.useCallback)(() => {
        var _a;
        if (checkoutData === null || checkoutData === void 0 ? void 0 : checkoutData.reservations) {
            const reservations = (_a = checkoutData === null || checkoutData === void 0 ? void 0 : checkoutData.reservations) === null || _a === void 0 ? void 0 : _a.map((obj) => {
                const nightKeys = Object.keys(obj.nights);
                const dateFormat = 'YYYY-MM-DDTHH:mm:ss';
                return {
                    reservationId: obj.reservationNumber,
                    checkInTime: (0, dayjs_1.default)(nightKeys[0]).format(dateFormat),
                    checkOutTime: (0, dayjs_1.default)(nightKeys[nightKeys.length - 1])
                        .add(1, 'day')
                        .format(dateFormat),
                    totalPrice: obj.total.afterTax,
                    priceCurrency: ccx.currentCurrency,
                    email: obj.guest.email,
                    phone: obj.guest.phone,
                    postalCode: obj.guest.address.postalCode,
                    city: obj.guest.address.city,
                    state: obj.guest.address.state,
                };
            });
            const event = new events_1.StepThanksViewEvent(reservations, basketContext.currentBasketRows, hotel);
            raise(event);
            return core_1.TotalCalculator.calculateTotalForReservation(checkoutData, !isFromRoomstay);
        }
        return 0;
    }, [checkoutData]);
    (0, react_1.useEffect)(() => {
        setItemisedFees(checkoutData.feesSummary || []);
    }, [checkoutData]);
    const addons = [];
    const comments = [];
    (0, react_1.useEffect)(() => {
        return () => {
            basketContext.finish();
        };
    }, []);
    return (react_1.default.createElement("div", { className: "container u-marg-top--heavy rs-step-thanks" },
        react_1.default.createElement("div", { className: "row justify-content-center" },
            react_1.default.createElement("div", { className: "col-lg-8 col-md-12 u-flex align-items-center u-flex-direction-column" },
                react_1.default.createElement("div", { className: "u-marg-bottom" },
                    react_1.default.createElement(Icon_1.default, { icon: Icon_1.IconType.Check, size: "48px", color: Color_1.Color.White, backgroundColor: Color_1.Color.Accent, borderRadius: "round", noPadding: true })),
                react_1.default.createElement("div", { className: "u-marg-bottom", style: { maxWidth: '530px' } },
                    react_1.default.createElement(Headline_1.default, { size: "large", align: TextAlignment_1.TextAlign.Center }, t(Translation_1.Translation.Step.Thanks.ThankYouHeadline, { hotel: hotel === null || hotel === void 0 ? void 0 : hotel.name }))),
                react_1.default.createElement("div", { className: "u-marg-bottom--heavy" },
                    react_1.default.createElement(Text_1.default, { color: Color_1.Color.DarkGrey },
                        t(Translation_1.Translation.Step.Thanks.InvoiceNumber),
                        " ",
                        checkoutData.itineraryNumber)),
                react_1.default.createElement("div", { className: "u-marg-bottom--massive" },
                    react_1.default.createElement(react_router_dom_1.Link, { to: "/" },
                        react_1.default.createElement(BEButton_1.default, null, t(Translation_1.Translation.Step.Thanks.GoBackHome)))),
                ((_c = (_b = checkoutData === null || checkoutData === void 0 ? void 0 : checkoutData.reservations) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.guest) && react_1.default.createElement(InlineSignUpSection_1.InlineSignUpSection, { reservation: checkoutData.reservations[0], hotelId: (hotel === null || hotel === void 0 ? void 0 : hotel.hotelID) || '' }),
                react_1.default.createElement(react_1.default.Fragment, null,
                    react_1.default.createElement("div", { className: "u-marg-top w-100" }, (_d = checkoutData === null || checkoutData === void 0 ? void 0 : checkoutData.reservations) === null || _d === void 0 ? void 0 : _d.map((obj, index) => {
                        if (obj.addons) {
                            const nights = Object.keys(obj.nights);
                            obj.addons.forEach((addon) => {
                                if (isFromRoomstay) {
                                    addons.push(Object.assign(Object.assign({}, addon), { nights: nights.length }));
                                }
                                else {
                                    addons.push(addon);
                                }
                            });
                        }
                        if (obj.comments && index === 0) {
                            obj.comments.map((comment) => {
                                comments.push(comment);
                            });
                        }
                        return react_1.default.createElement(ReservationRow_1.default, { reservation: obj, key: obj.reservationNumber });
                    })),
                    react_1.default.createElement(LineBreak_1.default, null),
                    react_1.default.createElement("div", { className: "w-100" },
                        react_1.default.createElement(CommonAddonSummary_1.CommonAddonSummary, { addons: addons }),
                        comments.length >= 1 && (react_1.default.createElement("div", { className: "u-marg-bottom--heavy" },
                            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.Grey }, t(Translation_1.Translation.Step.Confirmation.Comments))),
                            comments.map((comment, index) => (react_1.default.createElement(Text_1.default, { style: {
                                    whiteSpace: 'pre',
                                }, key: index, type: Text_1.TextType.Small }, comment))))),
                        react_1.default.createElement(ItemisedFeesSummary_1.ItemisedFeesSummary, { fees: itemisedFees }),
                        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, total)),
                        react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, align: TextAlignment_1.TextAlign.Right, color: Color_1.Color.DarkGrey },
                            react_1.default.createElement(TaxInclusionNotice_1.TaxInclusionNotice, null)))))), (_e = checkoutData === null || checkoutData === void 0 ? void 0 : checkoutData.reservations) === null || _e === void 0 ? void 0 :
        _e.map((obj, index) => {
            const nightKeys = Object.keys(obj.nights);
            const firstNight = (0, dayjs_1.default)(nightKeys[0]);
            const lastNight = (0, dayjs_1.default)(nightKeys[nightKeys.length - 1]).add(1, 'day');
            return (react_1.default.createElement("script", { key: index, type: "application/ld+json" }, JSON.stringify({
                '@context': 'http://schema.org',
                '@type': 'LodgingReservation',
                reservationId: obj.reservationNumber,
                reservationStatus: 'http://schema.org/ReservationConfirmed',
                checkinTime: firstNight.format('YYYY-MM-DDTHH:mm:ss'),
                checkoutTime: lastNight.format('YYYY-MM-DDTHH:mm:ss'),
                totalPrice: obj.total.afterTax,
                basePrice: obj.total.beforeTax,
                priceCurrency: ccx.currentCurrency,
            })));
        })));
}
//# sourceMappingURL=StepThanksComponent.js.map