"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
const postConversion_1 = __importDefault(require("./postConversion"));
const uploadCallConversion_1 = __importDefault(require("./uploadCallConversion"));
const uploadClickConversion_1 = __importDefault(require("./uploadClickConversion"));
const uploadConversionAdjustment_1 = __importDefault(require("./uploadConversionAdjustment"));
const functions_1 = require("./functions");
const uploadCallConversion2_1 = __importDefault(require("./uploadCallConversion2"));
const userList_1 = __importDefault(require("./userList"));
const uploadClickConversion2_1 = __importDefault(require("./uploadClickConversion2"));
const uploadConversionAdjustment2_1 = __importDefault(require("./uploadConversionAdjustment2"));
const destination = {
    name: 'Google Ads Conversions',
    slug: 'actions-google-enhanced-conversions',
    mode: 'cloud',
    authentication: {
        scheme: 'oauth2',
        fields: {
            conversionTrackingId: {
                label: 'Conversion ID',
                description: 'You will find this information in the event snippet for your conversion action, for example `send_to: AW-CONVERSION_ID/AW-CONVERSION_LABEL`. In the sample snippet, AW-CONVERSION_ID stands for the conversion ID unique to your account. Enter the conversion ID, without the AW- prefix. **Required if you are using a mapping that sends data to the legacy Google Enhanced Conversions API (i.e. Upload Enhanced Conversion (Legacy) Action).**',
                type: 'string'
            },
            customerId: {
                label: 'Customer ID',
                description: 'ID of your Google Ads Account. This should be 10-digits and in XXX-XXX-XXXX format. **Required if you are using a mapping that sends data to the Google Ads API.**',
                type: 'string'
            }
        },
        testAuthentication: async (_request) => {
            return true;
        },
        refreshAccessToken: async (request, { auth }) => {
            const res = await request('https://www.googleapis.com/oauth2/v4/token', {
                method: 'POST',
                body: new URLSearchParams({
                    refresh_token: auth.refreshToken,
                    client_id: auth.clientId,
                    client_secret: auth.clientSecret,
                    grant_type: 'refresh_token'
                })
            });
            return { accessToken: res.data.access_token };
        }
    },
    extendRequest({ auth }) {
        return {
            headers: {
                authorization: `Bearer ${auth?.accessToken}`
            }
        };
    },
    audienceFields: {
        supports_conversions: {
            type: 'boolean',
            label: 'Supports Conversions',
            description: 'Mark true if you are using uploadCallConversion, uploadClickConversion or uploadConversionAdjustment. If you plan to use userLists alone or in combination with the others, mark as false.',
            default: false
        },
        external_id_type: {
            type: 'string',
            label: 'External ID Type',
            description: 'Customer match upload key types. Required if you are using UserLists. Not used by the other actions.',
            choices: [
                { label: 'CONTACT INFO', value: 'CONTACT_INFO' },
                { label: 'CRM ID', value: 'CRM_ID' },
                { label: 'MOBILE ADVERTISING ID', value: 'MOBILE_ADVERTISING_ID' }
            ]
        },
        app_id: {
            label: 'App ID',
            description: 'A string that uniquely identifies a mobile application from which the data was collected. Required if external ID type is mobile advertising ID',
            type: 'string',
            depends_on: {
                match: 'all',
                conditions: [
                    {
                        fieldKey: 'external_id_type',
                        operator: 'is',
                        value: 'MOBILE_ADVERTISING_ID'
                    }
                ]
            }
        }
    },
    audienceConfig: {
        mode: {
            type: 'synced',
            full_audience_sync: false
        },
        async createAudience(request, createAudienceInput) {
            if (createAudienceInput.audienceSettings.supports_conversions) {
                return {
                    externalId: 'segment'
                };
            }
            createAudienceInput.settings.customerId = (0, functions_1.verifyCustomerId)(createAudienceInput.settings.customerId);
            const auth = createAudienceInput.settings.oauth;
            let userListId;
            try {
                userListId = await (0, functions_1.createGoogleAudience)(request, createAudienceInput, auth, createAudienceInput.features, createAudienceInput.statsContext);
            }
            catch (err) {
                let status = err.status || err.code;
                if (!status && err.response && err.response.status) {
                    status = Number(err.response.status);
                }
                const message = err.response?.content || err.message;
                throw new actions_core_1.IntegrationError(message, 'CREATE_AUDIENCE_FAILED', status || 400);
            }
            return {
                externalId: userListId
            };
        },
        async getAudience(request, getAudienceInput) {
            if (getAudienceInput.externalId === 'segment') {
                return {
                    externalId: getAudienceInput.externalId
                };
            }
            getAudienceInput.settings.customerId = (0, functions_1.verifyCustomerId)(getAudienceInput.settings.customerId);
            const response = await (0, functions_1.getGoogleAudience)(request, getAudienceInput.settings, getAudienceInput.externalId, getAudienceInput.settings.oauth, getAudienceInput.features, getAudienceInput.statsContext);
            return {
                externalId: response.results[0].userList.id
            };
        }
    },
    actions: {
        postConversion: postConversion_1.default,
        uploadClickConversion: uploadClickConversion_1.default,
        uploadCallConversion: uploadCallConversion_1.default,
        uploadConversionAdjustment: uploadConversionAdjustment_1.default,
        uploadConversionAdjustment2: uploadConversionAdjustment2_1.default,
        uploadClickConversion2: uploadClickConversion2_1.default,
        uploadCallConversion2: uploadCallConversion2_1.default,
        userList: userList_1.default
    },
    presets: [
        {
            name: 'Entities Audience Membership Changed',
            partnerAction: 'userList',
            mapping: (0, actions_core_1.defaultValues)(userList_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'warehouse_audience_membership_changed_identify'
        },
        {
            name: 'Associated Entity Added',
            partnerAction: 'userList',
            mapping: (0, actions_core_1.defaultValues)(userList_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'warehouse_entity_added_track'
        },
        {
            name: 'Associated Entity Removed',
            partnerAction: 'userList',
            mapping: (0, actions_core_1.defaultValues)(userList_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'warehouse_entity_removed_track'
        },
        {
            name: 'Journeys Step Entered',
            partnerAction: 'userList',
            mapping: (0, actions_core_1.defaultValues)(userList_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'journeys_step_entered_track'
        },
        {
            name: 'Journeys Step Entered',
            partnerAction: 'postConversion',
            mapping: (0, actions_core_1.defaultValues)(postConversion_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'journeys_step_entered_track'
        },
        {
            name: 'Journeys Step Entered',
            partnerAction: 'uploadClickConversion',
            mapping: (0, actions_core_1.defaultValues)(uploadClickConversion_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'journeys_step_entered_track'
        },
        {
            name: 'Journeys Step Entered',
            partnerAction: 'uploadCallConversion',
            mapping: (0, actions_core_1.defaultValues)(uploadCallConversion_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'journeys_step_entered_track'
        },
        {
            name: 'Journeys Step Entered',
            partnerAction: 'uploadConversionAdjustment',
            mapping: (0, actions_core_1.defaultValues)(uploadConversionAdjustment_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'journeys_step_entered_track'
        },
        {
            name: 'Journeys Step Entered',
            partnerAction: 'uploadConversionAdjustment2',
            mapping: (0, actions_core_1.defaultValues)(uploadConversionAdjustment2_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'journeys_step_entered_track'
        },
        {
            name: 'Journeys Step Entered',
            partnerAction: 'uploadClickConversion2',
            mapping: (0, actions_core_1.defaultValues)(uploadClickConversion2_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'journeys_step_entered_track'
        },
        {
            name: 'Journeys Step Entered',
            partnerAction: 'uploadCallConversion2',
            mapping: (0, actions_core_1.defaultValues)(uploadCallConversion2_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'journeys_step_entered_track'
        }
    ]
};
exports.default = destination;
//# sourceMappingURL=index.js.map