"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getStickyAttributes = exports.getShouldTrackAttributes = exports.enrichProfile = exports.allProvidersDict = exports.allProvidersList = void 0;
const browserId_1 = __importDefault(require("./lib/browserId"));
const browserVersion_1 = __importDefault(require("./lib/browserVersion"));
const campaign_1 = __importDefault(require("./lib/campaign"));
const cookies_1 = __importDefault(require("./lib/cookies"));
const currentTimestamp_1 = __importDefault(require("./lib/currentTimestamp"));
const device_1 = __importDefault(require("./lib/device"));
const device_type_1 = __importDefault(require("./lib/device_type"));
const first_session_1 = __importDefault(require("./lib/first_session"));
const ip_1 = __importDefault(require("./lib/ip"));
const language_1 = __importDefault(require("./lib/language"));
const location_1 = __importDefault(require("./lib/location"));
const offset_1 = __importDefault(require("./lib/offset"));
const platform_1 = __importDefault(require("./lib/platform"));
const queryParams_1 = __importDefault(require("./lib/queryParams"));
const referrer_1 = __importDefault(require("./lib/referrer"));
const source_type_1 = __importDefault(require("./lib/source_type"));
const timezone_1 = __importDefault(require("./lib/timezone"));
const url_1 = __importDefault(require("./lib/url"));
exports.allProvidersList = [
    browserId_1.default,
    browserVersion_1.default,
    campaign_1.default,
    cookies_1.default,
    currentTimestamp_1.default,
    device_1.default,
    device_type_1.default,
    first_session_1.default,
    ip_1.default,
    language_1.default,
    location_1.default,
    offset_1.default,
    platform_1.default,
    queryParams_1.default,
    referrer_1.default,
    source_type_1.default,
    timezone_1.default,
    url_1.default,
];
exports.allProvidersDict = Object.fromEntries(exports.allProvidersList.map(provider => [provider.provide, provider]));
async function enrichProfile(listAttributes, visitorProfile, inputs) {
    for (const attribute of listAttributes) {
        const provider = exports.allProvidersDict[attribute];
        if (provider) {
            visitorProfile['profile'] ??= {};
            visitorProfile['metadata'] ??= {};
            // if sticky and already exists, skip
            if (provider.isSticky && visitorProfile['profile'][attribute])
                continue;
            try {
                const value = await provider.getValue(inputs);
                if (value !== undefined) {
                    visitorProfile['profile'][attribute] = value;
                    // TODO: add metadata around here
                }
            }
            catch (e) {
                throw new Error(`Error while enriching attribute '${attribute}': ${e.message}`);
            }
        }
        else {
            throw new Error(`Attribute '${attribute}' provider not found`);
        }
    }
}
exports.enrichProfile = enrichProfile;
/**
 * TODO: enrich this result with the __SEGMENTATION_FEATURES__ that came on the manifest.
 * __SEGMENTATION_FEATURES__ are based on the dimension yaml file on the monolith and could be changed.
 *
 * @returns list of attributes that should be tracked
 */
function getShouldTrackAttributes() {
    return exports.allProvidersList
        .filter(provider => provider.shouldTrack)
        .map(provider => provider.provide);
}
exports.getShouldTrackAttributes = getShouldTrackAttributes;
function getStickyAttributes() {
    return exports.allProvidersList
        .filter(provider => provider.isSticky)
        .map(provider => provider.provide);
}
exports.getStickyAttributes = getStickyAttributes;
exports.default = {
    allProvidersList: exports.allProvidersList,
    allProvidersDict: exports.allProvidersDict,
    enrichProfile,
    getShouldTrackAttributes,
    getStickyAttributes,
};
