"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const criteo_audiences_1 = require("../criteo-audiences");
const getOperationFromPayload = async (request, advertiser_id, payload, credentials) => {
    const add_user_list = [];
    let audience_key = '';
    for (const event of payload) {
        let email = undefined;
        if (!audience_key && event.audience_key)
            audience_key = event.audience_key;
        email = event.hash_emails ? (0, criteo_audiences_1.hash)(event.email) : event.email;
        if (email)
            add_user_list.push(email);
    }
    const contactlist_id = await (0, criteo_audiences_1.getContactListId)(request, advertiser_id, audience_key, credentials);
    const operation = {
        operation_type: 'add',
        contactlist_id: contactlist_id,
        user_list: add_user_list
    };
    return operation;
};
const processPayload = async (request, settings, payload) => {
    const credentials = {
        client_id: settings.client_id,
        client_secret: settings.client_secret
    };
    const operation = await getOperationFromPayload(request, settings.advertiser_id, payload, credentials);
    return await (0, criteo_audiences_1.patchContactList)(request, operation, credentials);
};
const action = {
    title: 'Add users to Audience',
    description: 'Add users from Criteo audience by connecting to Criteo API',
    defaultSubscription: 'type = "track" and event = "Audience Entered"',
    fields: {
        audience_key: {
            label: 'Audience key',
            description: 'Unique name for personas audience',
            type: 'string',
            default: {
                '@path': '$.properties.audience_key'
            }
        },
        event: {
            label: 'Event name',
            description: 'Event for audience entering or exiting',
            type: 'string',
            default: {
                '@path': '$.event'
            }
        },
        email: {
            label: 'Email',
            description: "The user's email",
            type: 'string',
            default: {
                '@path': '$.context.traits.email'
            }
        },
        hash_emails: {
            label: 'Hash Emails',
            description: "Hash emails before sending them to Criteo (may lower your audience's match rate). If deactivated, emails will be sent unhashed to Criteo's API and will be hashed upon reception at Criteo's server.",
            type: 'boolean',
            default: false,
            required: false
        },
        enable_batching: {
            label: 'Enable Batching',
            description: 'Enable batching for this request',
            type: 'boolean',
            default: true,
            readOnly: true
        }
    },
    perform: async (request, { settings, payload }) => {
        return await processPayload(request, settings, [payload]);
    },
    performBatch: async (request, { settings, payload }) => {
        return await processPayload(request, settings, payload);
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map