"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: {
        engage_space: segment_properties_1.engage_space,
        user_id: segment_properties_1.user_id,
        anonymous_id: segment_properties_1.anonymous_id,
        group_id: { ...segment_properties_1.group_id, required: true },
        traits: segment_properties_1.traits,
        timestamp: segment_properties_1.timestamp,
        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,
            messageId: payload?.message_id,
            traits: {
                ...payload?.traits
            },
            timestamp: payload?.timestamp,
            integrations: {
                All: false
            },
            type: 'group',
            context: {
                consent: isValidConsentObject ? { ...payload?.consent } : {}
            }
        };
        statsContext?.statsClient?.incr('tapi_internal', 1, [...statsContext.tags, `action:sendGroup`]);
        return { batch: [groupPayload] };
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map