"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../utils");
const getEventFromPayload = (payload) => {
    const event = {
        event: payload.event,
        user_id: payload.user_id || '',
        properties: payload.properties || {}
    };
    return event;
};
const processData = async (request, settings, payload) => {
    const events = payload.map((value) => getEventFromPayload(value));
    return request(`${utils_1.DAWN_API_ENDPOINT}/sinks/segment/track`, {
        method: 'post',
        json: events,
        headers: {
            authorization: `Bearer ${settings.writeKey}`
        }
    });
};
const action = {
    title: 'Track Event',
    description: 'Track regular or AI events in Dawn, adding desired data to properties',
    defaultSubscription: 'type = "track"',
    fields: {
        event: {
            label: 'Event Name',
            type: 'string',
            description: 'The name of the action being performed.',
            required: true,
            default: {
                '@path': '$.event'
            }
        },
        user_id: {
            label: 'User ID',
            type: 'string',
            description: 'The user ID performing the event.',
            required: true,
            default: {
                '@path': '$.userId'
            }
        },
        properties: {
            label: 'Properties',
            type: 'object',
            description: 'The properties of the event.',
            required: false,
            default: {
                '@path': '$.properties'
            }
        },
        enable_batching: {
            type: 'boolean',
            label: 'Use Dawn AI Batching',
            description: 'When enabled, the action will use batch requests to the Dawn AI API',
            required: true,
            default: true
        }
    },
    perform: async (request, { settings, payload }) => {
        return processData(request, settings, [payload]);
    },
    performBatch: async (request, { settings, payload }) => {
        return processData(request, settings, payload);
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map