"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
const fb_capi_properties_1 = require("../fb-capi-properties");
const fb_capi_user_data_1 = require("../fb-capi-user-data");
const utils_1 = require("../utils");
const fb_capi_app_data_1 = require("../fb-capi-app-data");
const action = {
    title: 'Custom Event',
    description: 'Send a custom event',
    fields: {
        action_source: { ...fb_capi_properties_1.action_source, required: true },
        event_name: {
            label: 'Event Name',
            description: 'A Facebook [standard event](https://developers.facebook.com/docs/meta-pixel/implementation/conversion-tracking#standard-events) or [custom event](https://developers.facebook.com/docs/meta-pixel/implementation/conversion-tracking#custom-events) name.',
            type: 'string',
            required: true,
            default: {
                '@path': '$.event'
            }
        },
        event_time: { ...fb_capi_properties_1.event_time, required: true },
        user_data: fb_capi_user_data_1.user_data_field,
        app_data_field: fb_capi_app_data_1.app_data_field,
        custom_data: fb_capi_properties_1.custom_data,
        event_id: fb_capi_properties_1.event_id,
        event_source_url: fb_capi_properties_1.event_source_url,
        data_processing_options: fb_capi_properties_1.data_processing_options,
        data_processing_options_country: fb_capi_properties_1.data_processing_options_country,
        data_processing_options_state: fb_capi_properties_1.data_processing_options_state,
        test_event_code: fb_capi_properties_1.test_event_code
    },
    perform: (request, { payload, settings, features, statsContext }) => {
        if (!payload.user_data) {
            throw new actions_core_1.IntegrationError('Must include at least one user data property', 'Misconfigured required field', 400);
        }
        const [data_options, country_code, state_code] = (0, fb_capi_properties_1.dataProcessingOptions)(payload.data_processing_options, payload.data_processing_options_country, payload.data_processing_options_state);
        const testEventCode = payload.test_event_code || settings.testEventCode;
        return request(`https://graph.facebook.com/v${(0, utils_1.get_api_version)(features, statsContext)}/${settings.pixelId}/events`, {
            method: 'POST',
            json: {
                data: [
                    {
                        event_name: payload.event_name,
                        event_time: payload.event_time,
                        action_source: payload.action_source,
                        event_id: payload.event_id,
                        event_source_url: payload.event_source_url,
                        user_data: (0, fb_capi_user_data_1.hash_user_data)({ user_data: payload.user_data }),
                        custom_data: payload.custom_data,
                        app_data: (0, fb_capi_app_data_1.generate_app_data)(payload.app_data_field),
                        data_processing_options: data_options,
                        data_processing_options_country: country_code,
                        data_processing_options_state: state_code
                    }
                ],
                ...(testEventCode && { test_event_code: testEventCode })
            }
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map