"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: 'Purchase',
    description: 'Send event when a user completes a purchase',
    defaultSubscription: 'type = "track" and event = "Order Completed"',
    fields: {
        client_id: { ...ga4_properties_1.client_id },
        user_id: { ...ga4_properties_1.user_id },
        timestamp_micros: { ...ga4_properties_1.timestamp_micros },
        affiliation: { ...ga4_properties_1.affiliation },
        coupon: { ...ga4_properties_1.coupon, default: { '@path': '$.properties.coupon' } },
        currency: { ...ga4_properties_1.currency, required: true },
        items: {
            ...ga4_properties_1.items_multi_products,
            required: true
        },
        transaction_id: { ...ga4_properties_1.transaction_id, required: true },
        shipping: { ...ga4_properties_1.shipping },
        tax: { ...ga4_properties_1.tax },
        value: { ...ga4_properties_1.value, default: { '@path': '$.properties.total' } },
        user_properties: ga4_properties_1.user_properties,
        engagement_time_msec: ga4_properties_1.engagement_time_msec,
        params: ga4_properties_1.params
    },
    perform: (request, { payload }) => {
        (0, ga4_functions_1.verifyCurrency)(payload.currency);
        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 product name or product id is required for product or impression data.', '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: 'purchase',
                    params: {
                        affiliation: payload.affiliation,
                        coupon: payload.coupon,
                        currency: payload.currency,
                        items: googleItems,
                        transaction_id: payload.transaction_id,
                        shipping: payload.shipping,
                        value: payload.value,
                        tax: payload.tax,
                        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