"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../common/utils");
const functions_1 = require("./functions");
const mixpanel_properties_1 = require("../mixpanel-properties");
const utils_2 = require("../common/utils");
const getEventFromPayload = (payload, settings) => {
    const event = {
        event: payload.event,
        properties: {
            ...(0, functions_1.getEventProperties)(payload, settings)
        }
    };
    return event;
};
const processData = async (request, settings, payload, features) => {
    const events = payload.map((value) => {
        return getEventFromPayload(value, settings);
    });
    const throwHttpErrors = features && features['mixpanel-multistatus'] ? false : true;
    const response = await callMixpanelApi(request, settings, events, throwHttpErrors);
    if (features && features['mixpanel-multistatus']) {
        return (0, utils_2.handleMixPanelApiResponse)(payload.length, response, events);
    }
    return response;
};
const callMixpanelApi = async (request, settings, events, throwHttpErrors) => {
    return await request(`${(0, utils_1.getApiServerUrl)(settings.apiRegion)}/import?strict=${settings.strictMode ?? `1`}`, {
        method: 'post',
        json: events,
        headers: {
            authorization: `Basic ${Buffer.from(`${settings.apiSecret}:`).toString('base64')}`
        },
        throwHttpErrors: throwHttpErrors
    });
};
const action = {
    title: 'Track Event',
    description: 'Send an event to Mixpanel. [Learn more about Events in Mixpanel](https://help.mixpanel.com/hc/en-us/articles/360041995352-Mixpanel-Concepts-Events?source=segment-actions)',
    defaultSubscription: 'type = "track"',
    fields: {
        event: {
            label: 'Event Name',
            type: 'string',
            description: 'The name of the action being performed.',
            required: true,
            default: {
                '@path': '$.event'
            }
        },
        ...mixpanel_properties_1.eventProperties
    },
    performBatch: async (request, { settings, payload, features }) => {
        return processData(request, settings, payload, features);
    },
    perform: async (request, { settings, payload, features }) => {
        return processData(request, settings, [payload], features);
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map