"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 identifyUser_1 = __importDefault(require("./identifyUser"));
const logEvent_1 = __importDefault(require("./logEvent"));
const mapUser_1 = __importDefault(require("./mapUser"));
const groupIdentifyUser_1 = __importDefault(require("./groupIdentifyUser"));
const logPurchase_1 = __importDefault(require("./logPurchase"));
const regional_endpoints_1 = require("./regional-endpoints");
const logEventV2_1 = __importDefault(require("./logEventV2"));
const presets = [
    {
        name: 'Track Calls',
        subscribe: 'type = "track" and event != "Order Completed"',
        partnerAction: 'logEventV2',
        mapping: (0, actions_core_1.defaultValues)(logEventV2_1.default.fields),
        type: 'automatic'
    },
    {
        name: 'Order Completed Calls',
        subscribe: 'type = "track" and event = "Order Completed"',
        partnerAction: 'logPurchase',
        mapping: (0, actions_core_1.defaultValues)(logPurchase_1.default.fields),
        type: 'automatic'
    },
    {
        name: 'Page Calls',
        subscribe: 'type = "page"',
        partnerAction: 'logEventV2',
        mapping: {
            ...(0, actions_core_1.defaultValues)(logEventV2_1.default.fields),
            event_type: {
                '@template': 'Viewed {{name}}'
            }
        },
        type: 'automatic'
    },
    {
        name: 'Screen Calls',
        subscribe: 'type = "screen"',
        partnerAction: 'logEventV2',
        mapping: {
            ...(0, actions_core_1.defaultValues)(logEventV2_1.default.fields),
            event_type: {
                '@template': 'Viewed {{name}}'
            }
        },
        type: 'automatic'
    },
    {
        name: 'Identify Calls',
        subscribe: 'type = "identify"',
        partnerAction: 'identifyUser',
        mapping: (0, actions_core_1.defaultValues)(identifyUser_1.default.fields),
        type: 'automatic'
    },
    {
        name: 'Browser Session Tracking',
        subscribe: 'type = "track" or type = "identify" or type = "group" or type = "page" or type = "alias"',
        partnerAction: 'sessionId',
        mapping: {},
        type: 'automatic'
    }
];
const destination = {
    name: 'Actions Amplitude',
    slug: 'actions-amplitude',
    mode: 'cloud',
    authentication: {
        scheme: 'custom',
        fields: {
            apiKey: {
                label: 'API Key',
                description: 'Amplitude project API key. You can find this key in the "General" tab of your Amplitude project.',
                type: 'password',
                required: true
            },
            secretKey: {
                label: 'Secret Key',
                description: 'Amplitude project secret key. You can find this key in the "General" tab of your Amplitude project.',
                type: 'password',
                required: true
            },
            endpoint: {
                label: 'Endpoint Region',
                description: 'The region to send your data.',
                type: 'string',
                format: 'text',
                choices: [
                    {
                        label: 'North America',
                        value: 'north_america'
                    },
                    {
                        label: 'Europe',
                        value: 'europe'
                    }
                ],
                default: 'north_america'
            }
        },
        testAuthentication: (request, { settings }) => {
            const endpoint = (0, regional_endpoints_1.getEndpointByRegion)('usersearch', settings.endpoint);
            return request(`${endpoint}?user=testUser@example.com`, {
                username: settings.apiKey,
                password: settings.secretKey
            });
        }
    },
    onDelete: async (request, { settings, payload }) => {
        return request((0, regional_endpoints_1.getEndpointByRegion)('deletions', settings.endpoint), {
            username: settings.apiKey,
            password: settings.secretKey,
            method: 'post',
            json: {
                user_ids: [payload.userId],
                requester: 'segment'
            }
        });
    },
    presets,
    actions: {
        logEvent: logEvent_1.default,
        identifyUser: identifyUser_1.default,
        mapUser: mapUser_1.default,
        groupIdentifyUser: groupIdentifyUser_1.default,
        logPurchase: logPurchase_1.default,
        logEventV2: logEventV2_1.default
    }
};
exports.default = destination;
//# sourceMappingURL=index.js.map