"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const segment_properties_1 = require("../segment-properties");
const errors_1 = require("../errors");
const action = {
    title: 'Send Group',
    description: 'Send a group call to Segment’s tracking API. This is used to associate an individual user with a group',
    defaultSubscription: 'type = "group"',
    fields: {
        user_id: segment_properties_1.user_id,
        anonymous_id: segment_properties_1.anonymous_id,
        group_id: { ...segment_properties_1.group_id, required: true },
        timestamp: segment_properties_1.timestamp,
        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,
        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 groupPayload = {
            userId: payload?.user_id,
            anonymousId: payload?.anonymous_id,
            groupId: payload?.group_id,
            timestamp: payload?.timestamp,
            messageId: payload?.message_id,
            context: {
                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
            },
            traits: {
                ...payload?.traits
            },
            type: 'group'
        };
        statsContext?.statsClient?.incr('tapi_internal', 1, [...statsContext.tags, 'action:sendGroup']);
        return { batch: [groupPayload] };
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map