"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.InteractionStep = exports.InteractionType = exports.DatalayerAnalyticsMode = void 0;
const react_gtm_module_1 = __importDefault(require("react-gtm-module"));
const UniversalAnalytics_1 = require("./Analytics/UniversalAnalytics");
const DATALAYER_DATE_FORMAT = 'YYYY-M-D';
var DatalayerAnalyticsMode;
(function (DatalayerAnalyticsMode) {
    DatalayerAnalyticsMode["UniversalAnaltics"] = "UniversalAnalytics";
    DatalayerAnalyticsMode["GoogleAnalytics4"] = "GoogleAnalytics4";
})(DatalayerAnalyticsMode || (exports.DatalayerAnalyticsMode = DatalayerAnalyticsMode = {}));
let analytics = UniversalAnalytics_1.UniversalAnalytics;
class DataLayer {
    constructor() {
        this.enableLogging = true;
        this.currentCurrencyCode = '';
        this.impressions = [];
        this.setUserId = (id) => {
            analytics.setUserId(id);
            return this;
        };
    }
    initialiseGTM(company, hotel) {
        var _a, _b, _c;
        let datalayer;
        if ((_b = (_a = hotel === null || hotel === void 0 ? void 0 : hotel.datalayer) === null || _a === void 0 ? void 0 : _a.gtmIds) === null || _b === void 0 ? void 0 : _b.length) {
            datalayer = hotel.datalayer;
        }
        else if (company) {
            datalayer = company.datalayer;
        }
        const gtmIds = (_c = datalayer === null || datalayer === void 0 ? void 0 : datalayer.gtmIds) !== null && _c !== void 0 ? _c : [];
        const itemListName = (datalayer === null || datalayer === void 0 ? void 0 : datalayer.itemListName) || 'BE List';
        gtmIds.forEach((gtmId) => {
            react_gtm_module_1.default.initialize({ gtmId });
        });
        this.hotel = hotel;
        this.company = company;
        // We set the analytics mode as it will also initilaise it.
        analytics.initialise(this.company, this.hotel);
        analytics.setItemListName(itemListName);
    }
    // Use for testing change analytics type only
    setAnalyticsMode(mode) {
        analytics = mode;
        analytics.initialise(this.company, this.hotel);
    }
    setItemListName(itemListName) {
        analytics.setItemListName(itemListName);
    }
    setCurrencyCodes(currentCurrencyCode, originalCurrencyCode) {
        var _a;
        this.currentCurrencyCode = currentCurrencyCode;
        if (originalCurrencyCode) {
            this.originalCurrencyCode = originalCurrencyCode;
        }
        if (analytics) {
            analytics.updateCurrency(this.currentCurrencyCode, (_a = this.originalCurrencyCode) !== null && _a !== void 0 ? _a : '');
        }
    }
    addRoomImpression(room, dateRange, promoCode) {
        this.impressions.push({
            room: room,
            promoCode,
            dateRange,
        });
    }
    addRoomRateImpression(room, rate, dateRange, promoCode) {
        this.impressions.push({
            room: room,
            rate: rate,
            promoCode,
            dateRange,
        });
    }
    sendRoomImpressions() {
        this.log('sending ' + this.impressions.length + ' impressions');
        if (this.impressions.length > 0) {
            const eventObject = analytics.withUserData(analytics.generateRoomImpressionMessageObject(this.impressions));
            react_gtm_module_1.default.dataLayer({
                dataLayer: eventObject,
            });
            this.impressions = [];
        }
    }
    sendRoomClick(room, startDate, endDate, promoCode) {
        const eventObject = analytics.withUserData(analytics.generateProductClickMessageObject({ room, dateRange: { from: startDate, to: endDate }, promoCode }));
        this.log('sending room click', eventObject);
        react_gtm_module_1.default.dataLayer({
            dataLayer: eventObject,
        });
    }
    sendAwayWindowOpen() {
        this.log('away window triggered');
        react_gtm_module_1.default.dataLayer({
            dataLayer: analytics.withClientDetailsData({
                event: 'awayWindow',
            }),
        });
    }
    sendRoomRateAddToCart(rate, quantity, startDate, endDate, originalRoomRate) {
        this.log('sending room rate add to cart');
        react_gtm_module_1.default.dataLayer({
            dataLayer: analytics.withUserData(analytics.generateAddToCartMessageObject({
                room: rate.getRoom(),
                rate,
                dateRange: { from: startDate, to: endDate },
                quantity,
                promoCode: rate.promoCode,
                originalRoomRate,
            })),
        });
    }
    sendRoomAddonAddToCart(basketAddonRow, quantity, startDate, endDate) {
        this.log('sending room addons add to cart');
        react_gtm_module_1.default.dataLayer({
            dataLayer: analytics.withUserData(analytics.generateAddonsAddToCartMessageObject({
                dateRange: { from: startDate, to: endDate },
                basketAddonRow,
                quantity,
            })),
        });
    }
    sendUpsellConfirmed(dateRange, oldRate, newRate) {
        this.log('sending room upgrade notification');
        const event = analytics.generateUpsellConfirmedMessageObject({ dateRange, room: oldRate.getRoom(), rate: oldRate, promoCode: oldRate.promoCode }, { dateRange, room: newRate.getRoom(), rate: newRate, promoCode: newRate.promoCode });
        if (!event) {
            console.error('Failed sending upsell confirmation event');
            return;
        }
        react_gtm_module_1.default.dataLayer({
            dataLayer: analytics.withUserData(event),
        });
    }
    sendUpsellCancelled(dateRange, rate) {
        this.log('sending room upgrade cancellation event');
        const event = analytics.withUserData(analytics.generateUpsellCancelledMessageObject({ dateRange, room: rate.getRoom(), rate, promoCode: rate.promoCode }));
        react_gtm_module_1.default.dataLayer({
            dataLayer: event,
        });
    }
    sendRoomView(startDate, endDate) {
        this.log('sending room view');
        react_gtm_module_1.default.dataLayer({
            dataLayer: analytics.withUserData({
                event: 'room',
                ecommerce: {
                    currencyCode: this.originalCurrencyCode,
                    StartDate: startDate === null || startDate === void 0 ? void 0 : startDate.format(DATALAYER_DATE_FORMAT),
                    EndDate: endDate === null || endDate === void 0 ? void 0 : endDate.format(DATALAYER_DATE_FORMAT),
                    PageType: 'offerdetail',
                },
            }),
        });
    }
    sendCheckoutView(basketRows, addonRows) {
        this.log('sending checkout view');
        const event = analytics.withUserData(analytics.generateCheckoutViewMessageObject(basketRows, addonRows));
        react_gtm_module_1.default.dataLayer({
            dataLayer: event,
        });
    }
    sendReservationConfirmed(basketRows, addonRows, reservations) {
        const event = analytics.generateRoomPurchaseMessageObject(basketRows, addonRows, reservations);
        for (const element of event) {
            this.log('sending reservation confirmation', element);
            react_gtm_module_1.default.dataLayer({
                dataLayer: analytics.withUserData(element),
            });
        }
    }
    sendCancellation(reservation) {
        this.log('sending cancellation for reservation ' + reservation);
        react_gtm_module_1.default.dataLayer({
            dataLayer: analytics.withUserData(analytics.generateRefundMessageObject(reservation)),
        });
    }
    sendCancellationByReservationNumber(reservationNumber) {
        this.log('sending cancellation for reservation number ' + reservationNumber);
        react_gtm_module_1.default.dataLayer({
            dataLayer: analytics.withUserData(analytics.generateRefundMessageObjectByReservationNumber(reservationNumber)),
        });
    }
    sendInteraction(name, type, step) {
        const interaction = {
            'Interaction Name': name,
            'Interaction Type': type,
            'Interaction Step': step,
        };
        this.log('sending interaction', interaction);
        react_gtm_module_1.default.dataLayer({
            dataLayer: interaction,
        });
    }
    sendDateSelection({ startDate, endDate, adult, children, infant }) {
        if (!startDate || !endDate) {
            return;
        }
        this.log('sending date selection ', startDate, endDate);
        react_gtm_module_1.default.dataLayer({
            dataLayer: analytics.withUserData(analytics.generateDateSelectionMessageObject({ from: startDate, to: endDate }, adult || 0, children || 0, infant || 0)),
        });
    }
    sendMemberSignUp() {
        react_gtm_module_1.default.dataLayer({
            dataLayer: analytics.withClientDetailsData({
                event: 'Email Sign up',
            }),
        });
    }
    sendMemberSignedIn() {
        react_gtm_module_1.default.dataLayer({
            dataLayer: analytics.withUserData({
                event: 'login',
            }),
        });
    }
    log(...output) {
        if (!this.enableLogging) {
            return;
        }
        console.log('[DATALAYER]: ' +
            output
                .map((it) => {
                if (typeof it === 'object') {
                    return JSON.stringify(it);
                }
                return it;
            })
                .join('; '));
    }
}
DataLayer.instance = new DataLayer();
exports.default = DataLayer;
var InteractionType;
(function (InteractionType) {
    InteractionType["NUMBER_INPUT"] = "+-widget";
    InteractionType["CALENDAR"] = "Calendar";
    InteractionType["BUTTON"] = "Button";
    InteractionType["DROPDOWN"] = "Dropdown";
    InteractionType["LINK_DROPDOWN"] = "Link dropdown";
    InteractionType["LINK"] = "Link";
    InteractionType["FORM_FIELD"] = "Form field";
    InteractionType["CHECKBOX"] = "Checkbox";
})(InteractionType || (exports.InteractionType = InteractionType = {}));
var InteractionStep;
(function (InteractionStep) {
    InteractionStep["DATES"] = "Dates";
    InteractionStep["ROOMS"] = "Rooms";
    InteractionStep["GUEST_DETAILS"] = "Guest Details";
})(InteractionStep || (exports.InteractionStep = InteractionStep = {}));
//# sourceMappingURL=DataLayer.js.map