"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
const functions_1 = require("../functions");
const constants_1 = require("../constants");
const action = {
    title: 'Upload Call Conversion',
    description: 'Upload an offline call conversion to the Google Ads API.',
    fields: {
        conversion_action: {
            label: 'Conversion Action ID',
            description: 'The ID of the conversion action associated with this conversion.',
            type: 'number',
            required: true,
            dynamic: true
        },
        caller_id: {
            label: 'Caller ID',
            description: 'The caller ID from which this call was placed. Caller ID is expected to be in E.164 format with preceding + sign, e.g. "+16502531234".',
            type: 'string',
            required: true
        },
        call_timestamp: {
            label: 'Call Timestamp',
            description: 'The date time at which the call occurred. The timezone must be specified. The format is "yyyy-mm-dd hh:mm:ss+|-hh:mm", e.g. "2019-01-01 12:32:45-08:00".',
            type: 'string',
            required: true
        },
        conversion_timestamp: {
            label: 'Conversion Timestamp',
            description: 'The date time at which the conversion occurred. Must be after the click time. The timezone must be specified. The format is "yyyy-mm-dd hh:mm:ss+|-hh:mm", e.g. "2019-01-01 12:32:45-08:00".',
            type: 'string',
            required: true,
            default: {
                '@path': '$.timestamp'
            }
        },
        value: {
            label: 'Value',
            description: 'The value of the conversion for the advertiser.',
            type: 'number',
            default: {
                '@path': '$.properties.total'
            }
        },
        currency: {
            label: 'Currency',
            description: 'Currency associated with the conversion value. This is the ISO 4217 3-character currency code.',
            type: 'string',
            default: {
                '@path': '$.properties.currency'
            }
        },
        custom_variables: {
            label: 'Custom Variables',
            description: 'The custom variables associated with this conversion. On the left-hand side, input the name of the custom variable as it appears in your Google Ads account. On the right-hand side, map the Segment field that contains the corresponding value See [Google’s documentation on how to create custom conversion variables](https://developers.google.com/google-ads/api/docs/conversions/conversion-custom-variables).',
            type: 'object',
            additionalProperties: true,
            defaultObjectUI: 'keyvalue:only'
        },
        ad_user_data_consent_state: {
            label: 'Ad User Data Consent State',
            description: 'This represents consent for ad user data. For more information on consent, refer to [Google Ads API Consent](https://developers.google.com/google-ads/api/rest/reference/rest/v21/Consent).',
            type: 'string',
            choices: [
                { label: 'GRANTED', value: 'GRANTED' },
                { label: 'DENIED', value: 'DENIED' },
                { label: 'UNSPECIFIED', value: 'UNSPECIFIED' }
            ]
        },
        ad_personalization_consent_state: {
            label: 'Ad Personalization Consent State',
            type: 'string',
            description: 'This represents consent for ad personalization. This can only be set for OfflineUserDataJobService and UserDataService.For more information on consent, refer to [Google Ads API Consent](https://developers.google.com/google-ads/api/rest/reference/rest/v21/Consent).',
            choices: [
                { label: 'GRANTED', value: 'GRANTED' },
                { label: 'DENIED', value: 'DENIED' },
                { label: 'UNSPECIFIED', value: 'UNSPECIFIED' }
            ]
        },
        enable_batching: {
            type: 'boolean',
            label: 'Batch Data to Google Enhanced Conversions',
            description: 'If true, Segment will batch events before sending to Google’s APIs. Google accepts batches of up to 2000 events.',
            unsafe_hidden: false,
            default: false
        },
        batch_size: {
            label: 'Batch Size',
            description: 'Maximum number of events to include in each batch.',
            type: 'number',
            unsafe_hidden: true,
            default: constants_1.GOOGLE_ENHANCED_CONVERSIONS_BATCH_SIZE
        }
    },
    dynamicFields: {
        conversion_action: async (request, { settings, auth, features, statsContext }) => {
            return (0, functions_1.getConversionActionDynamicData)(request, settings, auth, features, statsContext);
        }
    },
    perform: async (request, { auth, settings, payload, features, statsContext }) => {
        if (!settings.customerId) {
            throw new actions_core_1.PayloadValidationError('Customer ID is required for this action. Please set it in destination settings.');
        }
        settings.customerId = settings.customerId.replace(/-/g, '');
        const request_object = {
            conversionAction: `customers/${settings.customerId}/conversionActions/${payload.conversion_action}`,
            callerId: payload.caller_id,
            callStartDateTime: (0, functions_1.convertTimestamp)(payload.call_timestamp),
            conversionDateTime: (0, functions_1.convertTimestamp)(payload.conversion_timestamp),
            conversionValue: payload.value,
            currencyCode: payload.currency
        };
        if (payload.ad_user_data_consent_state) {
            request_object['consent'] = {
                adUserData: payload.ad_user_data_consent_state
            };
        }
        if (payload.ad_personalization_consent_state) {
            request_object['consent'] = {
                ...request_object['consent'],
                adPersonalization: payload.ad_personalization_consent_state
            };
        }
        if (payload.custom_variables) {
            const customVariableIds = await (0, functions_1.getCustomVariables)(settings.customerId, auth, request, features, statsContext);
            request_object.customVariables = (0, functions_1.formatCustomVariables)(payload.custom_variables, customVariableIds.data[0].results);
        }
        const response = await request(`https://googleads.googleapis.com/${(0, functions_1.getApiVersion)(features, statsContext)}/customers/${settings.customerId}:uploadCallConversions`, {
            method: 'post',
            headers: {
                'developer-token': `${process.env.ADWORDS_DEVELOPER_TOKEN}`
            },
            json: {
                conversions: [request_object],
                partialFailure: true
            }
        });
        (0, functions_1.handleGoogleErrors)(response);
        return response;
    },
    performBatch: async (request, { auth, settings, payload, features, statsContext }) => {
        if (!settings.customerId) {
            throw new actions_core_1.PayloadValidationError('Customer ID is required for this action. Please set it in destination settings.');
        }
        const customerId = settings.customerId.replace(/-/g, '');
        const getCustomVariables = (0, functions_1.memoizedGetCustomVariables)();
        const request_objects = await Promise.all(payload.map(async (payloadItem) => {
            const request_object = {
                conversionAction: `customers/${customerId}/conversionActions/${payloadItem.conversion_action}`,
                callerId: payloadItem.caller_id,
                callStartDateTime: (0, functions_1.convertTimestamp)(payloadItem.call_timestamp),
                conversionDateTime: (0, functions_1.convertTimestamp)(payloadItem.conversion_timestamp),
                conversionValue: payloadItem.value,
                currencyCode: payloadItem.currency
            };
            if (payloadItem.ad_user_data_consent_state) {
                request_object['consent'] = {
                    adUserData: payloadItem.ad_user_data_consent_state
                };
            }
            if (payloadItem.ad_personalization_consent_state) {
                request_object['consent'] = {
                    ...request_object['consent'],
                    adPersonalization: payloadItem.ad_personalization_consent_state
                };
            }
            if (payloadItem.custom_variables) {
                const customVariableIds = await getCustomVariables(customerId, auth, request, features, statsContext);
                if (customVariableIds?.data?.length) {
                    request_object.customVariables = (0, functions_1.formatCustomVariables)(payloadItem.custom_variables, customVariableIds.data[0].results);
                }
            }
            return request_object;
        }));
        const response = await request(`https://googleads.googleapis.com/${(0, functions_1.getApiVersion)(features, statsContext)}/customers/${settings.customerId}:uploadCallConversions`, {
            method: 'post',
            headers: {
                'developer-token': `${process.env.ADWORDS_DEVELOPER_TOKEN}`
            },
            json: {
                conversions: request_objects,
                partialFailure: true
            }
        });
        (0, functions_1.handleGoogleErrors)(response);
        return response;
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map