"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.',
    defaultSubscription: 'type = "track"',
    fields: {
        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,
        application: segment_properties_1.application,
        campaign_parameters: segment_properties_1.campaign_parameters,
        device: segment_properties_1.device,
        ip_address: segment_properties_1.ip_address,
        locale: segment_properties_1.locale,
        location: segment_properties_1.location,
        network: segment_properties_1.network,
        operating_system: segment_properties_1.operating_system,
        page: segment_properties_1.page,
        screen: segment_properties_1.screen,
        user_agent: segment_properties_1.user_agent,
        timezone: segment_properties_1.timezone,
        group_id: segment_properties_1.group_id,
        properties: segment_properties_1.properties,
        traits: segment_properties_1.traits,
        message_id: segment_properties_1.message_id,
        consent: segment_properties_1.consent
    },
    perform: (_request, { payload, statsContext }) => {
        if (!payload.anonymous_id && !payload.user_id) {
            throw errors_1.MissingUserOrAnonymousIdThrowableError;
        }
        const isValidConsentObject = (0, segment_properties_1.validateConsentObject)(payload?.consent);
        const trackPayload = {
            userId: payload?.user_id,
            anonymousId: payload?.anonymous_id,
            timestamp: payload?.timestamp,
            event: payload?.event_name,
            messageId: payload?.message_id,
            context: {
                traits: {
                    ...payload?.traits
                },
                app: payload?.application,
                campaign: payload?.campaign_parameters,
                consent: isValidConsentObject ? { ...payload?.consent } : {},
                device: payload?.device,
                ip: payload?.ip_address,
                locale: payload?.locale,
                location: payload?.location,
                network: payload?.network,
                os: payload?.operating_system,
                page: payload?.page,
                screen: payload?.screen,
                userAgent: payload?.user_agent,
                timezone: payload?.timezone,
                groupId: payload?.group_id
            },
            properties: {
                ...payload?.properties
            },
            type: 'track'
        };
        statsContext?.statsClient?.incr('tapi_internal', 1, [...statsContext.tags, 'action:sendTrack']);
        return { batch: [trackPayload] };
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map