"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 constants_1 = require("../constants");
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_2 = require("../shared/constants");
const action = {
    title: 'Add to Cart V2',
    description: 'Send event when a user adds a product to the shopping cart',
    defaultSubscription: 'type = "track" and event = "Product Added"',
    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.addToCartFields,
    perform: (request, { payload, settings, features, statsContext, syncMode }) => {
        if (syncMode === 'add') {
            if (features && features[constants_2.FEATURE_FLAG_ADD_TO_CART]) {
                return (0, functions_1.send)(request, payload, settings, functions_1.getAddToCartEventData, constants_2.EventType.AddToCart, features, statsContext);
            }
            if (payload.currency && !constants_1.CURRENCY_ISO_CODES.has(payload.currency)) {
                throw new actions_core_1.IntegrationError(`${payload.currency} is not a valid currency code.`, 'Misconfigured required field', 400);
            }
            if (!payload.user_data) {
                throw new actions_core_1.IntegrationError('Must include at least one user data property', 'Misconfigured required field', 400);
            }
            if (payload.action_source === 'website' && payload.user_data.client_user_agent === undefined) {
                throw new actions_core_1.IntegrationError('If action source is "Website" then client_user_agent must be defined', 'Misconfigured required field', 400);
            }
            if (payload.contents) {
                const err = (0, fb_capi_properties_1.validateContents)(payload.contents);
                if (err)
                    throw err;
            }
            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: 'AddToCart',
                            event_time: payload.event_time,
                            event_source_url: payload.event_source_url,
                            event_id: payload.event_id,
                            action_source: payload.action_source,
                            user_data: (0, fb_capi_user_data_1.hash_user_data)({ user_data: payload.user_data }),
                            custom_data: {
                                ...payload.custom_data,
                                currency: payload.currency,
                                value: payload.value,
                                content_ids: payload.content_ids,
                                content_name: payload.content_name,
                                contents: payload.contents,
                                content_type: payload.content_type
                            },
                            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 required field', 400);
        }
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map