"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fields_1 = require("../fields");
const utils_1 = require("../utils");
const sendRequest = async (request, payloads, settings) => {
    const host = utils_1.hosts[settings.region];
    const requestBody = payloads.map((payload) => {
        return {
            type: 'email',
            action: payload.event_action,
            campaign: payload.campaign,
            campaign_id: payload.campaign_id,
            user_identifiers: payload.user_identifiers,
            campaign_event_value: payload.link_url ?? null,
            timestamp: payload.timestamp
        };
    });
    return request(`${host}/batch_email_event`, {
        method: 'post',
        json: requestBody
    });
};
const action = {
    title: 'Email Event',
    description: 'Send email related Segment track() events to Optimizely Data Platform',
    fields: {
        user_identifiers: fields_1.email_action_identifiers,
        event_action: {
            label: 'Optimizely Event Action',
            description: 'The name of the Optimizely Event Action.',
            type: 'string',
            required: true
        },
        campaign: {
            label: 'Campaign Name',
            description: 'The campaign name',
            type: 'string',
            required: true,
            default: {
                '@path': '$.properties.campaign_name'
            }
        },
        campaign_id: {
            label: 'Campaign ID',
            description: 'The campaign unique identifier',
            type: 'string',
            default: {
                '@path': '$.properties.campaign_id'
            }
        },
        link_url: {
            label: 'Link URL',
            description: 'URL of the link which was clicked',
            type: 'string',
            default: {
                '@path': '$.properties.link_url'
            }
        },
        timestamp: fields_1.timestamp,
        enable_batching: fields_1.enable_batching,
        batch_size: fields_1.batch_size
    },
    perform: (request, { payload, settings }) => {
        return sendRequest(request, [payload], settings);
    },
    performBatch: (request, { payload, settings }) => {
        return sendRequest(request, payload, settings);
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map