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());
    });
};
import { UAParser } from 'ua-parser-js';
import { TrackingIntregrationType } from './shared.js';
export function track(event, eventData, options) {
    var _a, _b, _c;
    const windowOrIFrame = getWindowOrIframe((_a = options === null || options === void 0 ? void 0 : options.source) !== null && _a !== void 0 ? _a : 'client');
    return (_b = windowOrIFrame === null || windowOrIFrame === void 0 ? void 0 : windowOrIFrame.analytics) === null || _b === void 0 ? void 0 : _b.track(event, eventData, {
        integrations: getSegmentIntegrations((_c = options === null || options === void 0 ? void 0 : options.integrations) !== null && _c !== void 0 ? _c : TrackingIntregrationType.ALL)
    });
}
export function identify(userId, traits, options) {
    var _a, _b;
    const windowOrIFrame = getWindowOrIframe((_a = options === null || options === void 0 ? void 0 : options.source) !== null && _a !== void 0 ? _a : 'client');
    return (_b = windowOrIFrame === null || windowOrIFrame === void 0 ? void 0 : windowOrIFrame.analytics) === null || _b === void 0 ? void 0 : _b.identify(String(userId), traits);
}
export function identifyTalentUser({ id, email }) {
    if (!id)
        return;
    return identify(id, { email }, { source: 'talent' });
}
export function getPlatform() {
    const uaParserResult = UAParser();
    const isMobile = uaParserResult.device.type === 'mobile';
    return isMobile ? 'Responsive' : 'Web';
}
export function addToDataLayer(event) {
    return __awaiter(this, void 0, void 0, function* () {
        ;
        window.dataLayer = window.dataLayer || [];
        window.dataLayer.push({ event: event });
    });
}
function getWindowOrIframe(source) {
    if (source === 'client' || source === 'auth-spa') {
        return window;
    }
    if (source === 'talent') {
        const talentPixel = document === null || document === void 0 ? void 0 : document.getElementById('talent-pixel');
        const talentPixelWindow = talentPixel === null || talentPixel === void 0 ? void 0 : talentPixel.contentWindow;
        return talentPixelWindow;
    }
    return undefined;
}
function getSegmentIntegrations(presetName) {
    switch (presetName) {
        case 'only-google':
            return {
                All: false,
                'Google Analytics': true,
                'Google Analytics 4': true,
                'Google Analytics Web': true
            };
        case 'all-except-google':
            return {
                All: true,
                'Google Analytics': false,
                'Google Analytics 4': false,
                'Google Analytics Web': false
            };
        case 'all':
        default:
            return undefined;
    }
}
