"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("./types");
const actions_core_1 = require("@segment/actions-core");
const utils_1 = require("./utils");
const trackConversion_1 = __importDefault(require("./trackConversion"));
const destination = {
    name: 'Amazon Conversions Api',
    slug: 'actions-amazon-conversions-api',
    description: 'Amazon conversion API destination to send conversion event data to Amazon.',
    mode: 'cloud',
    authentication: {
        scheme: 'oauth2',
        fields: {
            region: {
                label: 'Region',
                description: 'Region for API Endpoint, either NA, EU, FE.',
                choices: [
                    { label: 'North America (NA)', value: types_1.Region.NA },
                    { label: 'Europe (EU)', value: types_1.Region.EU },
                    { label: 'Far East (FE)', value: types_1.Region.FE }
                ],
                default: types_1.Region.NA,
                type: 'string',
                required: true
            },
            advertiserId: {
                label: 'Amazon Advertiser ID',
                description: 'Your Amazon Advertiser Account ID.',
                type: 'string',
                required: true
            }
        },
        testAuthentication: async (request, { auth, settings }) => {
            if (!auth?.accessToken) {
                throw new actions_core_1.InvalidAuthenticationError('Please authenticate via Oauth before enabling the destination.');
            }
            return await request(`${settings.region}/v2/profiles`, {
                method: 'GET',
                headers: {
                    'Content-Type': 'application/json'
                },
                timeout: 2500
            });
        },
        refreshAccessToken: async (request, { auth }) => {
            const authToken = await (0, utils_1.getAuthToken)(request, auth);
            return { accessToken: authToken };
        }
    },
    extendRequest({ auth }) {
        return {
            headers: {
                authorization: `Bearer ${auth?.accessToken}`,
                'Amazon-Advertising-API-ClientID': process.env.ACTIONS_AMAZON_CONVERSIONS_API_CLIENT_ID || ''
            }
        };
    },
    presets: [
        {
            name: 'Add to Shopping Cart',
            subscribe: 'type = "track" AND event = "Product Added"',
            partnerAction: 'trackConversion',
            mapping: {
                ...(0, actions_core_1.defaultValues)(trackConversion_1.default.fields),
                eventType: 'ADD_TO_SHOPPING_CART'
            },
            type: 'automatic'
        },
        {
            name: 'Application',
            subscribe: 'type = "track" AND event = "Application Submitted"',
            partnerAction: 'trackConversion',
            mapping: {
                ...(0, actions_core_1.defaultValues)(trackConversion_1.default.fields),
                eventType: 'APPLICATION'
            },
            type: 'automatic'
        },
        {
            name: 'Checkout',
            subscribe: 'type = "track" AND event = "Checkout Started"',
            partnerAction: 'trackConversion',
            mapping: {
                ...(0, actions_core_1.defaultValues)(trackConversion_1.default.fields),
                eventType: 'CHECKOUT'
            },
            type: 'automatic'
        },
        {
            name: 'Contact',
            subscribe: 'type = "track" AND event = "Callback Started"',
            partnerAction: 'trackConversion',
            mapping: {
                ...(0, actions_core_1.defaultValues)(trackConversion_1.default.fields),
                eventType: 'CONTACT'
            },
            type: 'automatic'
        },
        {
            name: 'Lead',
            subscribe: 'type = "track" AND event = "Lead Generated"',
            partnerAction: 'trackConversion',
            mapping: {
                ...(0, actions_core_1.defaultValues)(trackConversion_1.default.fields),
                eventType: 'LEAD'
            },
            type: 'automatic'
        },
        {
            name: 'Off Amazon Purchases',
            subscribe: 'type = "track" AND event = "Order Completed"',
            partnerAction: 'trackConversion',
            mapping: {
                ...(0, actions_core_1.defaultValues)(trackConversion_1.default.fields),
                eventType: 'OFF_AMAZON_PURCHASES'
            },
            type: 'automatic'
        },
        {
            name: 'Mobile App First Start',
            subscribe: 'type = "track" AND event = "Application Opened"',
            partnerAction: 'trackConversion',
            mapping: {
                ...(0, actions_core_1.defaultValues)(trackConversion_1.default.fields),
                eventType: 'MOBILE_APP_FIRST_START'
            },
            type: 'automatic'
        },
        {
            name: 'Page View',
            subscribe: 'type = "page"',
            partnerAction: 'trackConversion',
            mapping: {
                ...(0, actions_core_1.defaultValues)(trackConversion_1.default.fields),
                eventType: 'PAGE_VIEW'
            },
            type: 'automatic'
        },
        {
            name: 'Search',
            subscribe: 'type = "track" AND event = "Products Searched"',
            partnerAction: 'trackConversion',
            mapping: {
                ...(0, actions_core_1.defaultValues)(trackConversion_1.default.fields),
                eventType: 'SEARCH'
            },
            type: 'automatic'
        },
        {
            name: 'Sign Up',
            subscribe: 'type = "track" AND event = "Signed Up"',
            partnerAction: 'trackConversion',
            mapping: {
                ...(0, actions_core_1.defaultValues)(trackConversion_1.default.fields),
                eventType: 'SIGN_UP'
            },
            type: 'automatic'
        },
        {
            name: 'Subscribe',
            subscribe: 'type = "track" AND event = "Subscription Created"',
            partnerAction: 'trackConversion',
            mapping: {
                ...(0, actions_core_1.defaultValues)(trackConversion_1.default.fields),
                eventType: 'SUBSCRIBE'
            },
            type: 'automatic'
        },
        {
            name: 'Other',
            subscribe: 'type = "track" AND event = "Other"',
            partnerAction: 'trackConversion',
            mapping: {
                ...(0, actions_core_1.defaultValues)(trackConversion_1.default.fields),
                eventType: 'OTHER'
            },
            type: 'automatic'
        },
    ],
    actions: {
        trackConversion: trackConversion_1.default
    }
};
exports.default = destination;
//# sourceMappingURL=index.js.map