"use strict";
/**
 * Analytics Events Feature
 *
 * Used for Meta CAPI integration and Meta Pixel client-side tracking.
 */
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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const RoomstayThemeEngine_1 = __importDefault(require("../RoomstayThemeEngine"));
const AnalyticsEvents_1 = __importDefault(require("../../util/AnalyticsEvents/AnalyticsEvents"));
const Feature_1 = require("./Feature");
class AnalyticsEventsFeature extends Feature_1.Feature {
    constructor() {
        super('AnalyticsEventsFeature');
        this.options = {};
    }
    getOptions() {
        return this.options;
    }
    onLoad(options) {
        this.options = options || {};
        // Load Meta Pixel script if pixelId is provided
        if (this.options.metaPixelId) {
            this.loadMetaPixel(this.options.metaPixelId);
        }
        AnalyticsEvents_1.default.initialize({
            hotelId: this.options.hotelId,
            currency: this.options.currency,
            metaPixelId: this.options.metaPixelId,
            firePixelEvents: this.options.firePixelEvents,
            debug: this.options.debug,
        });
    }
    loadMetaPixel(pixelId) {
        return __awaiter(this, void 0, void 0, function* () {
            if (typeof window === 'undefined')
                return;
            const w = window;
            // Don't re-initialize if already loaded
            if (w.fbq)
                return;
            // Initialize fbq function (Meta's official snippet adapted for modern JS)
            const n = (w.fbq = (...args) => {
                n.callMethod ? n.callMethod(...args) : n.queue.push(args);
            });
            if (!w._fbq)
                w._fbq = n;
            n.push = n;
            n.loaded = true;
            n.version = '2.0';
            n.queue = [];
            try {
                yield RoomstayThemeEngine_1.default.loadScript('https://connect.facebook.net/en_US/fbevents.js', 'meta-pixel');
                // Initialize pixel and fire PageView
                w.fbq('init', pixelId);
                w.fbq('track', 'PageView');
                if (this.options.debug) {
                    console.log('[AnalyticsEventsFeature] Meta Pixel loaded with ID:', pixelId);
                }
            }
            catch (error) {
                if (this.options.debug) {
                    console.log('[AnalyticsEventsFeature] Failed to load Meta Pixel script:', error);
                }
            }
        });
    }
    onUnLoad() {
        this.options = {};
    }
}
exports.default = new AnalyticsEventsFeature();
//# sourceMappingURL=AnalyticsEventsFeature.js.map