"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendToAppcues = sendToAppcues;
exports.send = send;
const actions_core_1 = require("@segment/actions-core");
const constants_1 = require("../constants");
async function sendToAppcues(request, endpoint, data) {
    return request(endpoint, {
        method: 'POST',
        json: data
    });
}
function buildBaseFields(payload) {
    const { userId, anonymousId, context, integrations, timestamp, messageId } = payload;
    return {
        ...(userId ? { userId } : {}),
        ...(anonymousId ? { anonymousId } : {}),
        ...(context ? { context } : {}),
        ...(integrations ? { integrations } : {}),
        ...(timestamp ? { timestamp } : {}),
        ...(messageId ? { messageId } : {})
    };
}
async function send(request, payload, settings) {
    const { region, accountId } = settings;
    if (!constants_1.REGION_ENDPOINTS[region]) {
        throw new actions_core_1.PayloadValidationError(`Invalid region: ${region}. Must be one of: ${Object.keys(constants_1.REGION_ENDPOINTS).join(', ')}`);
    }
    const endpoint = `${constants_1.REGION_ENDPOINTS[region]}/v2/accounts/${accountId}/segment/direct`;
    const { type, event, name, properties, user_traits, groupId, group_traits } = payload;
    const baseFields = buildBaseFields(payload);
    const requests = [];
    if (type === 'track') {
        if (!event) {
            throw new actions_core_1.PayloadValidationError('Event name is required for track events');
        }
        const trackRequest = {
            type: 'track',
            event,
            ...(properties ? { properties } : {}),
            ...baseFields
        };
        requests.push(sendToAppcues(request, endpoint, trackRequest));
    }
    if (type === 'page') {
        const pageRequest = {
            type: 'page',
            ...(name ? { name } : {}),
            ...(properties ? { properties } : {}),
            ...baseFields
        };
        requests.push(sendToAppcues(request, endpoint, pageRequest));
    }
    if (type === 'screen') {
        const screenRequest = {
            type: 'screen',
            ...(name ? { name } : {}),
            ...(properties ? { properties } : {}),
            ...baseFields
        };
        requests.push(sendToAppcues(request, endpoint, screenRequest));
    }
    if (type === 'identify' ||
        (['track', 'page', 'screen'].includes(type) && user_traits && Object.keys(user_traits).length > 0)) {
        const identifyRequest = {
            type: 'identify',
            ...(user_traits && Object.keys(user_traits).length > 0 ? { traits: user_traits } : {}),
            ...baseFields
        };
        requests.push(sendToAppcues(request, endpoint, identifyRequest));
    }
    if (groupId && (type === 'group' || ['track', 'page', 'screen'].includes(type))) {
        const groupRequest = {
            type: 'group',
            groupId,
            ...(group_traits && Object.keys(group_traits).length > 0 ? { traits: group_traits } : {}),
            ...baseFields
        };
        requests.push(sendToAppcues(request, endpoint, groupRequest));
    }
    await Promise.all(requests);
}
//# sourceMappingURL=functions.js.map