"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseSections = parseSections;
exports.addUpdateEvents = addUpdateEvents;
const actions_core_1 = require("@segment/actions-core");
const get_1 = __importDefault(require("lodash/get"));
function parseSections(section, nestDepth) {
    const parseResults = {};
    if (nestDepth > 10)
        throw new actions_core_1.IntegrationError('Event data exceeds nesting depth. Plese use mapping to flatten the data to no more than three levels deep.', 'NESTING_DEPTH_EXCEEDED', 400);
    try {
        if (section === null)
            section = { null: '' };
        for (const key of Object.keys(section)) {
            if (typeof section[key] === 'object') {
                nestDepth++;
                const nested = parseSections(section[key], nestDepth);
                for (const nestedKey of Object.keys(nested)) {
                    parseResults[`${key}.${nestedKey}`] = nested[nestedKey];
                }
            }
            else {
                parseResults[key] = section[key];
            }
        }
    }
    catch (e) {
        const ie = e;
        if (ie.code === 'NESTING_DEPTH_EXCEEDED')
            throw new actions_core_1.IntegrationError('Event data exceeds nesting depth. Use Mapping to flatten data structures to no more than 3 levels deep', 'NESTING_DEPTH_EXCEEDED', 400);
        else
            throw new actions_core_1.IntegrationError(`Unexpected Exception while parsing Event payload.\n ${e}`, 'UNEXPECTED_EVENT_PARSING_EXCEPTION', 400);
    }
    return parseResults;
}
function addUpdateEvents(payload, uniqRecip) {
    let propertiesTraitsKV = {};
    propertiesTraitsKV = {
        ...propertiesTraitsKV,
        ...{ ['uniqueRecipient']: uniqRecip }
    };
    propertiesTraitsKV = {
        ...propertiesTraitsKV,
        ...{ ['eventSource']: (0, get_1.default)(payload, 'type', 'Null') + ' Event' }
    };
    propertiesTraitsKV = {
        ...propertiesTraitsKV,
        ...{ ['timestamp']: (0, get_1.default)(payload, 'timestamp', 'Null') }
    };
    if (payload.key_value_pairs)
        propertiesTraitsKV = {
            ...propertiesTraitsKV,
            ...parseSections(payload.key_value_pairs, 0)
        };
    if (payload.array_data)
        propertiesTraitsKV = {
            ...propertiesTraitsKV,
            ...parseSections(payload.array_data, 0)
        };
    if (payload.traits)
        propertiesTraitsKV = {
            ...propertiesTraitsKV,
            ...parseSections(payload.traits, 0)
        };
    if (payload.properties)
        propertiesTraitsKV = {
            ...propertiesTraitsKV,
            ...parseSections(payload.properties, 0)
        };
    if (payload.context)
        propertiesTraitsKV = {
            ...propertiesTraitsKV,
            ...parseSections(payload.context, 0)
        };
    const getValue = (o, part) => Object.entries(o).find(([k, _v]) => k.includes(part))?.[1];
    const getKey = (o, part) => Object.entries(o).find(([k, _v]) => k.includes(part))?.[0];
    let ak, av;
    if (getValue(propertiesTraitsKV, 'computation_class')?.toLowerCase() === 'audience') {
        ak = getValue(propertiesTraitsKV, 'computation_key');
        av = getValue(propertiesTraitsKV, `${ak}`);
        let x = getKey(propertiesTraitsKV, 'computation_class');
        delete propertiesTraitsKV[`${x}`];
        x = getKey(propertiesTraitsKV, 'computation_key');
        delete propertiesTraitsKV[`${x}`];
        delete propertiesTraitsKV[`${ak}`];
    }
    if (getValue(propertiesTraitsKV, 'audience_key')) {
        ak = getValue(propertiesTraitsKV, 'audience_key');
        av = getValue(propertiesTraitsKV, `${ak}`);
        const x = getKey(propertiesTraitsKV, 'audience_key');
        delete propertiesTraitsKV[`${x}`];
        delete propertiesTraitsKV[`${ak}`];
    }
    if (av !== null) {
        if (av)
            av = 'Audience Entered';
        if (!av)
            av = 'Audience Exited';
        propertiesTraitsKV = {
            ...propertiesTraitsKV,
            ...{ [`${ak}`]: av }
        };
    }
    const l = propertiesTraitsKV.length;
    return { propertiesTraitsKV, l };
}
//# sourceMappingURL=eventprocessing.js.map