"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.logDerbyBookingCompleteEvent = void 0;
const __1 = require("..");
const logDerbyBookingCompleteEvent = (hotel, reservationResponse, basketRows) => __awaiter(void 0, void 0, void 0, function* () {
    let i = 0;
    const reservations = reservationResponse.reservations;
    for (const reservation of reservations) {
        const basketRow = basketRows[i];
        const adults = basketRow.getAdults();
        const children = basketRow.getChildren();
        const startDate = basketRow.getStartDate();
        const endDate = basketRow.getEndDate();
        const room = basketRow.getRoom();
        const rate = basketRow.getRate();
        const totals = reservation.total;
        const _bparams = {
            pixel_id: 10172,
            account_id: 'dijitally-roomstay',
            event_type: 'booking_complete',
            is_landing_page: 'FALSE',
            hotel_id: hotel.hotelID,
            booking_id: reservation.reservationNumber,
            check_in_date: startDate.format('YYYY-MM-DD'), //in the yyyy-mm-dd format
            check_out_date: endDate.format('YYYY-MM-DD'), //in the yyyy-mm-dd format
            stay_length: endDate.diff(startDate, 'days'),
            total_guests: adults + children,
            rooms: 1,
            adults,
            children,
            room_type_id: room.code,
            room_type: room.name,
            rate_plan_id: rate === null || rate === void 0 ? void 0 : rate.code,
            rate_plan_name: rate === null || rate === void 0 ? void 0 : rate.name,
            price_base: totals.beforeTax,
            price_tax_fees: totals.afterTax - totals.beforeTax,
            price_total: totals.afterTax,
            price_currency: hotel.defaultCurrency, //ISO 4217 three-letter currency code
        };
        // Since Derby's script assumes global scope, we can apply it to window.
        window._bparams = _bparams;
        const trackingPromise = waitForDerbySoftPixel();
        yield __1.RoomstayThemeEngine.loadScript('https://linkcenterus.derbysoftsec.com/pixel/v1/pixelmin.js?v=' + Math.round(new Date().getTime()), 'derbysoft-pixel', true);
        yield trackingPromise;
        i++;
    }
});
exports.logDerbyBookingCompleteEvent = logDerbyBookingCompleteEvent;
const MAX_TRACKING_REQUEST_WAIT_TIME_MS = 2000;
const waitForDerbySoftPixel = () => {
    return new Promise((resolve) => {
        const timeout = setTimeout(() => {
            observer.disconnect();
            resolve();
        }, MAX_TRACKING_REQUEST_WAIT_TIME_MS);
        const observer = new MutationObserver((mutations) => {
            for (const mutation of mutations) {
                for (const node of mutation.addedNodes) {
                    if (node instanceof HTMLImageElement && node.src.includes('pixelTagging') && node.width === 1 && node.height === 1) {
                        if (node.complete) {
                            clearTimeout(timeout);
                            observer.disconnect();
                            resolve();
                        }
                        else {
                            node.addEventListener('load', () => {
                                clearTimeout(timeout);
                                observer.disconnect();
                                resolve();
                            });
                            node.addEventListener('error', () => {
                                clearTimeout(timeout);
                                observer.disconnect();
                                resolve();
                            });
                        }
                    }
                }
            }
        });
        observer.observe(document.body, { childList: true, subtree: false });
    });
};
//# sourceMappingURL=DerbysoftPixel.js.map