"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 fields_1 = require("../shared/fields");
const functions_1 = require("../shared/functions");
const constants_1 = require("../shared/constants");
const action = {
    title: 'Custom Event V2',
    description: 'Send a custom event',
    syncMode: {
        description: 'Define how the records from your destination will be synced.',
        label: 'How to sync records',
        default: 'add',
        choices: [{ label: 'Insert Records', value: 'add' }]
    },
    fields: fields_1.customFields,
    perform: (request, { payload, settings, features, statsContext, syncMode }) => {
        if (syncMode === 'add') {
            if (features && features[constants_1.FEATURE_FLAG_CUSTOM]) {
                return (0, functions_1.send)(request, payload, settings, functions_1.getCustomEventData, constants_1.EventType.Custom, 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 })
                }
            });
        }
        else {
            throw new actions_core_1.IntegrationError(`Sync mode ${syncMode} is not supported`, 'Misconfigured sync mode', 400);
        }
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map