"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.processPayloads = processPayloads;
const actions_core_1 = require("@segment/actions-core");
const constants_1 = require("../constants");
function processPayloads(request, payloads, settings) {
    const json = payloads.map(processPayload);
    const url = `${constants_1.apiBaseUrl}/applications/${settings.clientId}/segment-audience-sync/`;
    return request(url, {
        method: 'post',
        json
    });
}
function processPayload(payload) {
    const { audience_id, audience_name, action, timestamp, user_id, traits_or_properties_hidden, additional_user_traits } = payload;
    const actionValue = typeof action === 'boolean' ? action : traits_or_properties_hidden?.[audience_name];
    if (typeof actionValue !== 'boolean') {
        throw new actions_core_1.PayloadValidationError('Action must be a boolean value (true for add, false for remove). If connecting to an Engage Audience, leave this field empty and ensure the audience_id and audience_name field mappings are left to their default values.');
    }
    const json = {
        audience_id,
        audience_name,
        action: actionValue,
        timestamp,
        user_id,
        ...additional_user_traits ?? {}
    };
    return json;
}
//# sourceMappingURL=utils.js.map