"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ga4_functions_1 = require("../ga4-functions");
const ga4_properties_1 = require("../ga4-properties");
const ga4_types_1 = require("../ga4-types");
const normalizeEventName = (name, lowercase) => {
    name = name.trim();
    name = name.replace(/\s/g, '_');
    if (lowercase) {
        name = name.toLowerCase();
    }
    return name;
};
const action = {
    title: 'Custom Event',
    description: 'Send any custom event',
    defaultSubscription: 'type = "track"',
    fields: {
        data_stream_type: { ...ga4_properties_1.data_stream_type },
        app_instance_id: { ...ga4_properties_1.app_instance_id },
        clientId: { ...ga4_properties_1.client_id },
        user_id: { ...ga4_properties_1.user_id },
        timestamp_micros: { ...ga4_properties_1.timestamp_micros },
        name: {
            label: 'Event Name',
            description: 'The unique name of the custom event created in GA4. GA4 does not accept spaces in event names so Segment will replace any spaces with underscores. More information about GA4 event name rules is available in [their docs](https://support.google.com/analytics/answer/10085872?hl=en&ref_topic=9756175#event-name-rules&zippy=%2Cin-this-article.%2Cin-this-article).',
            type: 'string',
            required: true,
            default: {
                '@path': '$.event'
            }
        },
        lowercase: {
            label: 'Lowercase Event Name',
            description: 'If true, the event name will be converted to lowercase before sending to Google. Event names are case sensitive in GA4 so enable this setting to avoid distinct events for casing differences. More information about GA4 event name rules is available in [their docs](https://support.google.com/analytics/answer/10085872?hl=en&ref_topic=9756175#event-name-rules&zippy=%2Cin-this-article.%2Cin-this-article).',
            type: 'boolean',
            default: false
        },
        user_properties: ga4_properties_1.user_properties,
        engagement_time_msec: ga4_properties_1.engagement_time_msec,
        params: { ...ga4_properties_1.params },
        ad_user_data_consent: ga4_properties_1.ad_user_data_consent,
        ad_personalization_consent: ga4_properties_1.ad_personalization_consent
    },
    perform: (request, { payload, settings }) => {
        const data_stream_type = payload.data_stream_type ?? ga4_types_1.DataStreamType.Web;
        const stream_params = data_stream_type === ga4_types_1.DataStreamType.MobileApp
            ? (0, ga4_functions_1.getMobileStreamParams)(settings.apiSecret, settings.firebaseAppId, payload.app_instance_id)
            : (0, ga4_functions_1.getWebStreamParams)(settings.apiSecret, settings.measurementId, payload.clientId);
        const event_name = normalizeEventName(payload.name, payload.lowercase);
        (0, ga4_functions_1.verifyParams)(payload.params);
        (0, ga4_functions_1.verifyUserProps)(payload.user_properties);
        const request_object = {
            ...stream_params.identifier,
            user_id: payload.user_id,
            events: [
                {
                    name: event_name,
                    params: {
                        engagement_time_msec: payload.engagement_time_msec,
                        ...payload.params
                    }
                }
            ],
            ...(0, ga4_properties_1.formatUserProperties)(payload.user_properties),
            timestamp_micros: (0, ga4_functions_1.convertTimestamp)(payload.timestamp_micros),
            ...(0, ga4_functions_1.formatConsent)({
                ad_personalization_consent: payload.ad_personalization_consent,
                ad_user_data_consent: payload.ad_user_data_consent
            })
        };
        return (0, ga4_functions_1.sendData)(request, stream_params.search_params, request_object);
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map