"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.send = send;
exports.sendJSON = sendJSON;
const constants_1 = require("../constants");
function send(request, payload) {
    const { messageId, timestamp, type, event, name, properties, traits, userId, anonymousId, groupId, context, previousId } = payload;
    const baseJSON = {
        messageId,
        timestamp,
        ...(userId && { userId }),
        ...(anonymousId && { anonymousId }),
        ...(context && { context })
    };
    if (type === 'track') {
        const trackJSON = {
            ...baseJSON,
            type: 'track',
            event: event,
            ...(properties && { properties }),
            ...(traits && { traits })
        };
        return sendJSON(request, trackJSON);
    }
    else if (type === 'page' || type === 'screen') {
        const pageOrScreenJSON = {
            ...baseJSON,
            type,
            ...(name && { name }),
            ...(properties && { properties }),
            ...(traits && { traits })
        };
        return sendJSON(request, pageOrScreenJSON);
    }
    else if (type === 'identify') {
        const identifyJSON = {
            ...baseJSON,
            type,
            ...(traits && { traits })
        };
        return sendJSON(request, identifyJSON);
    }
    else if (type === 'group') {
        const groupJSON = {
            ...baseJSON,
            type,
            ...(groupId && { groupId }),
            ...(traits && { traits })
        };
        return sendJSON(request, groupJSON);
    }
    else if (type === 'alias') {
        const aliasJSON = {
            ...baseJSON,
            type,
            userId: userId,
            ...(previousId && { previousId })
        };
        return sendJSON(request, aliasJSON);
    }
}
function sendJSON(request, json) {
    const { type } = json;
    const url = `${constants_1.ATTRIBUTION_ENDPOINT}/v1/${type[0]}`;
    return request(url, {
        method: 'POST',
        json
    });
}
//# sourceMappingURL=functions.js.map