"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 trackEvent_1 = __importDefault(require("./trackEvent"));
const identifyUser_1 = __importDefault(require("./identifyUser"));
const groupIdentifyUser_1 = __importDefault(require("./groupIdentifyUser"));
const incrementProperties_1 = __importDefault(require("./incrementProperties"));
const alias_1 = __importDefault(require("./alias"));
const utils_1 = require("./common/utils");
const trackPurchase_1 = __importDefault(require("./trackPurchase"));
const presets = [
    {
        name: 'Track Calls',
        subscribe: 'type = "track" and event != "Order Completed"',
        partnerAction: 'trackEvent',
        mapping: (0, actions_core_1.defaultValues)(trackEvent_1.default.fields),
        type: 'automatic'
    },
    {
        name: 'Order Completed Calls',
        subscribe: 'type = "track" and event = "Order Completed"',
        partnerAction: 'trackPurchase',
        mapping: (0, actions_core_1.defaultValues)(trackPurchase_1.default.fields),
        type: 'automatic'
    },
    {
        name: 'Page Calls',
        subscribe: 'type = "page"',
        partnerAction: 'trackEvent',
        mapping: {
            ...(0, actions_core_1.defaultValues)(trackEvent_1.default.fields),
            event: {
                '@template': 'Viewed {{name}}'
            }
        },
        type: 'automatic'
    },
    {
        name: 'Screen Calls',
        subscribe: 'type = "screen"',
        partnerAction: 'trackEvent',
        mapping: {
            ...(0, actions_core_1.defaultValues)(trackEvent_1.default.fields),
            event: {
                '@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: 'Group Calls',
        subscribe: 'type = "group"',
        partnerAction: 'groupIdentifyUser',
        mapping: (0, actions_core_1.defaultValues)(groupIdentifyUser_1.default.fields),
        type: 'automatic'
    }
];
const destination = {
    name: 'Mixpanel (Actions)',
    slug: 'actions-mixpanel',
    mode: 'cloud',
    authentication: {
        scheme: 'custom',
        fields: {
            projectToken: {
                label: 'Project Token',
                description: 'Mixpanel project token.',
                type: 'string',
                required: true
            },
            apiSecret: {
                label: 'Secret Key',
                description: 'Mixpanel project secret.',
                type: 'password',
                required: true
            },
            apiRegion: {
                label: 'Data Residency',
                description: 'Learn about [EU data residency](https://docs.mixpanel.com/docs/privacy/eu-residency) and [India data residency](https://docs.mixpanel.com/docs/privacy/in-residency)',
                type: 'string',
                choices: Object.values(utils_1.ApiRegions).map((apiRegion) => ({ label: apiRegion, value: apiRegion })),
                default: utils_1.ApiRegions.US
            },
            sourceName: {
                label: 'Source Name',
                description: "This value, if it's not blank, will be sent as segment_source_name to Mixpanel for every event/page/screen call.",
                type: 'string'
            },
            strictMode: {
                label: 'Strict Mode',
                description: "This value, if it's 1 (recommended), Mixpanel will validate the events you are trying to send and return errors per event that failed. Learn more about the Mixpanel [Import Events API](https://developer.mixpanel.com/reference/import-events)",
                type: 'string',
                choices: Object.values(utils_1.StrictMode).map((strictMode) => ({ label: strictMode, value: strictMode })),
                default: utils_1.StrictMode.ON
            }
        },
        testAuthentication: (request, { settings }) => {
            return request(`https://mixpanel.com/api/app/validate-project-credentials/`, {
                method: 'post',
                body: JSON.stringify({
                    api_secret: settings.apiSecret,
                    project_token: settings.projectToken
                })
            });
        }
    },
    presets,
    actions: {
        trackEvent: trackEvent_1.default,
        identifyUser: identifyUser_1.default,
        groupIdentifyUser: groupIdentifyUser_1.default,
        alias: alias_1.default,
        trackPurchase: trackPurchase_1.default,
        incrementProperties: incrementProperties_1.default
    }
};
exports.default = destination;
//# sourceMappingURL=index.js.map