"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
const ga4_functions_1 = require("../ga4-functions");
const ga4_types_1 = require("../ga4-types");
const ga4_properties_1 = require("../ga4-properties");
const action = {
    title: 'Select Item',
    description: 'Send event when a user selects an item from a list',
    defaultSubscription: 'type = "track" and event = "Product Clicked"',
    fields: {
        data_stream_type: { ...ga4_properties_1.data_stream_type },
        app_instance_id: { ...ga4_properties_1.app_instance_id },
        client_id: { ...ga4_properties_1.client_id },
        user_id: { ...ga4_properties_1.user_id },
        timestamp_micros: { ...ga4_properties_1.timestamp_micros },
        item_list_name: {
            label: 'Item List Name',
            description: 'The name of the list in which the item was presented to the user.',
            type: 'string'
        },
        item_list_id: {
            label: 'Item List Id',
            description: 'The ID of the list in which the item was presented to the user.',
            type: 'string'
        },
        items: {
            ...ga4_properties_1.items_single_products,
            required: true
        },
        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.client_id);
        let googleItems = [];
        if (payload.items) {
            googleItems = payload.items.map((product) => {
                if (product.item_name === undefined && product.item_id === undefined) {
                    throw new actions_core_1.PayloadValidationError('One of product name or product id is required for product or impression data.');
                }
                if (product.currency) {
                    (0, ga4_functions_1.verifyCurrency)(product.currency);
                }
                return product;
            });
        }
        (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: 'select_item',
                    params: {
                        items: googleItems,
                        item_list_name: payload.item_list_name,
                        item_list_id: payload.item_list_id,
                        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