"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildVisitorAttributes = buildVisitorAttributes;
exports.getEventId = getEventId;
exports.getEventKeys = getEventKeys;
exports.isValidJson = isValidJson;
const reduceRight_1 = __importDefault(require("lodash/reduceRight"));
function buildVisitorAttributes(configObj, userAttributes) {
    if (!userAttributes)
        return [];
    const attributeKeyMap = (0, reduceRight_1.default)(configObj.attributes, (prev, curr) => {
        return Object.assign(prev, {
            [curr.key]: curr
        });
    }, {});
    return (Object.keys(userAttributes)
        .filter((key) => Object.prototype.hasOwnProperty.call(attributeKeyMap, key))
        .filter((key) => isValidValue(userAttributes[key]))
        .map((key) => ({
        entity_id: attributeKeyMap[key].id,
        key: key,
        value: userAttributes[key],
        type: 'custom'
    })));
}
function getEventId(configObj, eventKey) {
    const eventMap = (0, reduceRight_1.default)(configObj.events, (prev, curr) => {
        return Object.assign(prev, {
            [curr.key]: curr
        });
    }, {});
    if (eventMap[eventKey]) {
        return eventMap[eventKey].id;
    }
}
function isValidValue(value) {
    return ['string', 'number', 'boolean'].includes(typeof value);
}
async function getEventKeys(request, settings) {
    try {
        const response = await request(settings.dataFileUrl, {
            skipResponseCloning: true
        });
        const choices = response.data.events.map((event) => ({
            label: event.key,
            value: event.key
        }));
        return {
            choices: [...choices]
        };
    }
    catch (err) {
        return {
            choices: [],
            error: {
                message: err?.response?.statusText ?? 'Unknown error',
                code: `${err?.response?.status ?? 'Unknown code'}`
            }
        };
    }
}
function isValidJson(dataFile) {
    try {
        JSON.parse(dataFile);
        return true;
    }
    catch (err) {
        return false;
    }
}
//# sourceMappingURL=functions.js.map