"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_properties_1 = require("../ga4-properties");
const action = {
    title: 'View Promotion',
    description: 'Send event when a promotion is shown to a user',
    defaultSubscription: 'type = "track" and event = "Promotion Viewed"',
    fields: {
        client_id: { ...ga4_properties_1.client_id },
        user_id: { ...ga4_properties_1.user_id },
        timestamp_micros: { ...ga4_properties_1.timestamp_micros },
        creative_name: { ...ga4_properties_1.creative_name },
        creative_slot: { ...ga4_properties_1.creative_slot, default: { '@path': '$.properties.creative' } },
        location_id: {
            label: 'Location ID',
            type: 'string',
            description: 'The ID of the location.',
            default: {
                '@path': '$.properties.position'
            }
        },
        promotion_id: { ...ga4_properties_1.promotion_id, default: { '@path': '$.properties.promotion_id' } },
        promotion_name: { ...ga4_properties_1.promotion_name, default: { '@path': '$.properties.name' } },
        items: {
            ...ga4_properties_1.items_single_products,
            required: true,
            properties: {
                ...ga4_properties_1.minimal_items.properties,
                creative_name: {
                    ...ga4_properties_1.creative_name
                },
                creative_slot: {
                    ...ga4_properties_1.creative_slot
                },
                promotion_name: {
                    ...ga4_properties_1.promotion_name
                },
                promotion_id: {
                    ...ga4_properties_1.promotion_id
                }
            }
        },
        user_properties: ga4_properties_1.user_properties,
        engagement_time_msec: ga4_properties_1.engagement_time_msec,
        params: ga4_properties_1.params
    },
    perform: (request, { payload }) => {
        let googleItems = [];
        if (payload.items) {
            googleItems = payload.items.map((product) => {
                if (product.item_name === undefined && product.item_id === undefined) {
                    throw new actions_core_1.IntegrationError('One of item id or item name is required.', 'Misconfigured required field', 400);
                }
                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 = {
            client_id: payload.client_id,
            user_id: payload.user_id,
            events: [
                {
                    name: 'view_promotion',
                    params: {
                        creative_name: payload.creative_name,
                        creative_slot: payload.creative_slot,
                        location_id: payload.location_id,
                        promotion_id: payload.promotion_id,
                        promotion_name: payload.promotion_name,
                        items: googleItems,
                        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)
        };
        return request('https://gtmadapter-node-cbjg5cz5hq-ew.a.run.app/v2/consolidated-data', {
            method: 'POST',
            json: request_object
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map