"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const segment_properties_1 = require("../segment-properties");
const errors_1 = require("../errors");
const action = {
    title: 'Send Track',
    description: 'Send a track call to Segment’s tracking API. This is used to record actions your users perform.',
    fields: {
        engage_space: segment_properties_1.engage_space,
        user_id: segment_properties_1.user_id,
        anonymous_id: segment_properties_1.anonymous_id,
        timestamp: segment_properties_1.timestamp,
        event_name: segment_properties_1.event_name,
        group_id: segment_properties_1.group_id,
        properties: segment_properties_1.properties,
        message_id: segment_properties_1.message_id,
        consent: segment_properties_1.consent
    },
    perform: (_, { payload, statsContext }) => {
        if (!payload.anonymous_id && !payload.user_id) {
            throw errors_1.MissingUserOrAnonymousIdThrowableError;
        }
        const isValidConsentObject = (0, segment_properties_1.validateConsentObject)(payload?.consent);
        statsContext?.statsClient?.incr('tapi_internal', 1, [...statsContext.tags, `action:sendTrack`]);
        return {
            batch: [
                {
                    userId: payload?.user_id,
                    anonymousId: payload?.anonymous_id,
                    timestamp: payload?.timestamp,
                    event: payload?.event_name,
                    messageId: payload?.message_id,
                    integrations: {
                        All: false
                    },
                    properties: {
                        ...payload?.properties
                    },
                    context: {
                        consent: isValidConsentObject ? { ...payload?.consent } : {}
                    },
                    type: 'track'
                }
            ]
        };
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map