"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 constants_1 = require("./utils/constants");
const trackEvent_1 = __importDefault(require("./trackEvent"));
const identifyUser_1 = __importDefault(require("./identifyUser"));
const identifyGroup_1 = __importDefault(require("./identifyGroup"));
const alias_1 = __importDefault(require("./alias"));
const destination = {
    name: 'Calliper Cloud (Actions)',
    slug: 'calliper-cloud-actions',
    mode: 'cloud',
    authentication: {
        scheme: 'custom',
        fields: {
            companyId: {
                label: 'Company id',
                description: 'Your company id in Calliper. During the onboarding you can find it by selecting Segment integration. After onboarding is complete you can find it by opening "Data Sources" in the main menu, pressing "New Data Source" and pressing "Connect" on a Segment card.',
                type: 'string',
                required: true
            },
            segmentKey: {
                label: 'Segment Key',
                description: 'Your Segment Key in Calliper. Please follow the instructions to find the Company id, secret key is located in a field next to it.',
                type: 'string',
                required: true
            }
        },
        testAuthentication: (request, { settings }) => {
            return request(`${constants_1.API_URL}/validate-credentials`, {
                method: 'post',
                json: {
                    companyId: settings.companyId,
                    key: settings.segmentKey
                }
            });
        }
    },
    onDelete: async (request, { settings, payload }) => {
        if (!payload.userId)
            throw new actions_core_1.IntegrationError('User id is required for deletion', 'Missing required field', 400);
        return request(`${constants_1.API_URL}/user/${payload.userId}`, {
            method: 'delete',
            json: {
                companyId: settings.companyId,
                key: settings.segmentKey
            }
        });
    },
    presets: [
        {
            name: 'Track Event',
            subscribe: 'type = "track"',
            partnerAction: 'trackEvent',
            mapping: (0, actions_core_1.defaultValues)(trackEvent_1.default.fields),
            type: 'automatic'
        },
        {
            name: 'Identify User',
            subscribe: 'type = "identify"',
            partnerAction: 'identifyUser',
            mapping: (0, actions_core_1.defaultValues)(identifyUser_1.default.fields),
            type: 'automatic'
        },
        {
            name: 'Identify Group',
            subscribe: 'type = "group"',
            partnerAction: 'identifyGroup',
            mapping: (0, actions_core_1.defaultValues)(identifyGroup_1.default.fields),
            type: 'automatic'
        }
    ],
    actions: {
        trackEvent: trackEvent_1.default,
        identifyUser: identifyUser_1.default,
        identifyGroup: identifyGroup_1.default,
        alias: alias_1.default
    }
};
exports.default = destination;
//# sourceMappingURL=index.js.map