"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.processPayload = processPayload;
const constants_1 = require("../constants");
const getCustomAudienceOperations = (payload, settings) => {
    let is_hashed = false;
    const audience_map = new Map([]);
    for (const p of payload) {
        if (p.segment_computation_action != constants_1.CONSTANTS.SUPPORTED_SEGMENT_COMPUTATION_ACTION) {
            continue;
        }
        let action_map = new Map([
            [constants_1.CONSTANTS.INCLUDE, []],
            [constants_1.CONSTANTS.EXCLUDE, []]
        ]);
        const existing_action_map_for_audience = audience_map.get(p.custom_audience_name);
        if (existing_action_map_for_audience !== undefined) {
            action_map = existing_action_map_for_audience;
        }
        else {
            audience_map.set(p.custom_audience_name, action_map);
        }
        if (p.traits_or_props[p.custom_audience_name] === true) {
            if (p.email_sha256 !== undefined) {
                action_map.get(constants_1.CONSTANTS.INCLUDE)?.push(p.email_sha256);
                is_hashed = true;
            }
            else {
                if (p.email !== undefined) {
                    action_map.get(constants_1.CONSTANTS.INCLUDE)?.push(p.email);
                }
            }
        }
        else if (p.traits_or_props[p.custom_audience_name] === false) {
            if (p.email_sha256 !== undefined) {
                action_map.get(constants_1.CONSTANTS.EXCLUDE)?.push(p.email_sha256);
                is_hashed = true;
            }
            else {
                if (p.email !== undefined) {
                    action_map.get(constants_1.CONSTANTS.EXCLUDE)?.push(p.email);
                }
            }
        }
    }
    const custom_audience_ops = [];
    audience_map.forEach((action_map_values, list) => {
        action_map_values.forEach((emails, action) => {
            let custom_audience_op;
            if (is_hashed) {
                custom_audience_op = {
                    accountId: settings.accountid,
                    list: list,
                    action: action,
                    emails: [],
                    sha256s: emails
                };
            }
            else {
                custom_audience_op = {
                    accountId: settings.accountid,
                    list: list,
                    action: action,
                    emails: emails,
                    sha256s: []
                };
            }
            custom_audience_ops.push(custom_audience_op);
        });
    });
    return custom_audience_ops;
};
async function processPayload(request, settings, events) {
    const custom_audience_ops = getCustomAudienceOperations(events, settings);
    const promises = [];
    for (const op of custom_audience_ops) {
        if (op.emails.length > 0 || op.sha256s.length > 0) {
            promises.push(request(constants_1.CONSTANTS.ROKT_API_BASE_URL + constants_1.CONSTANTS.ROKT_API_CUSTOM_AUDIENCE_ENDPOINT, {
                method: 'POST',
                json: op
            }));
        }
    }
    return await Promise.all(promises);
}
//# sourceMappingURL=custom-audience-operations.js.map