"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../utils");
const launchpad_properties_1 = require("./launchpad-properties");
function getEventProperties(payload, settings) {
    const integration = payload.context?.integration;
    return {
        time: payload.timestamp,
        ip: payload.context?.ip,
        anonymous_id: payload.anonymousId,
        distinct_id: payload.userId ? payload.userId : payload.anonymousId,
        context: payload.context,
        group_id: payload.groupId,
        properties: payload.properties,
        traits: payload.traits ? payload.traits : payload.context?.traits,
        messageId: payload.messageId,
        source: integration?.name != 'Segment' ? integration?.name : 'Segment',
        user_id: payload.userId,
        segment_source_name: settings.sourceName
    };
}
const getEventFromPayload = (payload, settings) => {
    const event = {
        event: payload.event,
        properties: {
            ...getEventProperties(payload, settings)
        },
        api_key: settings.apiSecret
    };
    return event;
};
const processData = async (request, settings, payload) => {
    const event = getEventFromPayload(payload, settings);
    const urlAddendum = 'capture';
    const requestURL = `${(0, utils_1.getApiServerUrl)(settings.apiRegion)}` + urlAddendum;
    return request(requestURL, {
        method: 'post',
        json: event
    });
};
const trackEvent = {
    title: 'Track Event',
    description: 'Send an event to Launchpad. [Learn more about Events in Launchpad](https://help.launchpad.pm)',
    defaultSubscription: 'type = "track"',
    fields: {
        event: {
            label: 'Event Name',
            type: 'string',
            description: 'The name of the action being performed.',
            required: true,
            default: {
                '@path': '$.event'
            }
        },
        ...launchpad_properties_1.eventProperties
    },
    perform: async (request, { settings, payload }) => {
        return processData(request, settings, payload);
    }
};
exports.default = trackEvent;
//# sourceMappingURL=index.js.map