"use strict";
/**
 * Analytics Events Helpers
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMetaCookies = getMetaCookies;
exports.serializeBasketRow = serializeBasketRow;
exports.serializeBasketRows = serializeBasketRows;
exports.serializeBasketAddonRow = serializeBasketAddonRow;
exports.serializeBasketAddonRows = serializeBasketAddonRows;
exports.extractGuestData = extractGuestData;
exports.calculateTotalValue = calculateTotalValue;
/**
 * Get a cookie value by name
 */
function getCookie(name) {
    if (typeof document === 'undefined') {
        return undefined;
    }
    const cookies = document.cookie.split(';');
    for (const cookie of cookies) {
        const [cookieName, cookieValue] = cookie.trim().split('=');
        if (cookieName === name) {
            return decodeURIComponent(cookieValue);
        }
    }
    return undefined;
}
/**
 * Get Meta cookies for tracking
 * - fbc (_fbc): Click ID - tracks users who clicked on a Facebook ad
 * - fbp (_fbp): Browser ID - unique identifier for the browser
 */
function getMetaCookies() {
    return {
        fbc: getCookie('_fbc'),
        fbp: getCookie('_fbp'),
    };
}
/**
 * Serialize a BasketRow to IRoomstayBasketItem for analytics events.
 */
function serializeBasketRow(basketRow) {
    const room = basketRow.getRoom();
    const rate = basketRow.getRate();
    const startDate = basketRow.getStartDate();
    const endDate = basketRow.getEndDate();
    return {
        roomCode: (room === null || room === void 0 ? void 0 : room.code) || '',
        roomId: room === null || room === void 0 ? void 0 : room.id,
        roomName: room === null || room === void 0 ? void 0 : room.name,
        rateCode: (rate === null || rate === void 0 ? void 0 : rate.code) || '',
        rateName: rate === null || rate === void 0 ? void 0 : rate.name,
        startDate: (startDate === null || startDate === void 0 ? void 0 : startDate.format('YYYY-MM-DD')) || '',
        endDate: (endDate === null || endDate === void 0 ? void 0 : endDate.format('YYYY-MM-DD')) || '',
        numberOfNights: basketRow.getNumberOfDays(),
        adults: basketRow.getAdults(),
        children: basketRow.getChildren(),
        infants: basketRow.getInfants(),
        totalPrice: basketRow.getTotalPrice() || 0,
        averageNightlyRate: (rate === null || rate === void 0 ? void 0 : rate.getAveragePrice()) || 0,
        promoCode: basketRow.getPromoCode() || undefined,
    };
}
/**
 * Serialize multiple BasketRows
 */
function serializeBasketRows(basketRows) {
    return basketRows.map(serializeBasketRow);
}
/**
 * Serialize a BasketAddonRow to IRoomstayBasketAddonItem for analytics events.
 */
function serializeBasketAddonRow(addonRow) {
    const addon = addonRow.getAddon();
    return {
        addonCode: (addon === null || addon === void 0 ? void 0 : addon.code) || '',
        addonName: (addon === null || addon === void 0 ? void 0 : addon.name) || '',
        quantity: addonRow.getTotalQuantity(),
        unitPrice: addonRow.getAddonSubtotal(),
        totalPrice: addonRow.getTotalPrice(),
    };
}
/**
 * Serialize multiple BasketAddonRows
 */
function serializeBasketAddonRows(addonRows) {
    return addonRows.map(serializeBasketAddonRow);
}
/**
 * Extract guest data from a ReservationsDTO
 */
function extractGuestData(reservation) {
    var _a, _b, _c, _d, _e;
    const firstReservation = (_a = reservation.reservations) === null || _a === void 0 ? void 0 : _a[0];
    const guest = firstReservation === null || firstReservation === void 0 ? void 0 : firstReservation.guest;
    if (!guest) {
        return {};
    }
    return {
        email: guest.email,
        phone: guest.phone,
        firstName: guest.firstName,
        lastName: guest.lastName,
        city: (_b = guest.address) === null || _b === void 0 ? void 0 : _b.city,
        state: (_c = guest.address) === null || _c === void 0 ? void 0 : _c.state,
        postalCode: (_d = guest.address) === null || _d === void 0 ? void 0 : _d.postalCode,
        country: (_e = guest.address) === null || _e === void 0 ? void 0 : _e.country,
    };
}
/**
 * Calculate total value from basket items and addons
 */
function calculateTotalValue(items, addons) {
    var _a;
    const itemsTotal = items.reduce((sum, item) => sum + item.totalPrice, 0);
    const addonsTotal = (_a = addons === null || addons === void 0 ? void 0 : addons.reduce((sum, addon) => sum + addon.totalPrice, 0)) !== null && _a !== void 0 ? _a : 0;
    return itemsTotal + addonsTotal;
}
//# sourceMappingURL=helpers.js.map