"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 logEvent_1 = __importDefault(require("./logEvent"));
const identify_1 = __importDefault(require("./identify"));
const group_1 = __importDefault(require("./group"));
const page_1 = __importDefault(require("./page"));
const properties_1 = require("./properties");
const presets = [
    {
        name: 'Track Calls',
        subscribe: 'type = "track"',
        partnerAction: 'logEvent',
        mapping: (0, actions_core_1.defaultValues)(logEvent_1.default.fields),
        type: 'automatic'
    },
    {
        name: 'Page Calls',
        subscribe: 'type = "page"',
        partnerAction: 'page',
        mapping: (0, actions_core_1.defaultValues)(page_1.default.fields),
        type: 'automatic'
    },
    {
        name: 'Group Calls',
        subscribe: 'type = "group"',
        partnerAction: 'group',
        mapping: (0, actions_core_1.defaultValues)(group_1.default.fields),
        type: 'automatic'
    },
    {
        name: 'Identify Calls',
        subscribe: 'type = "identify"',
        partnerAction: 'identify',
        mapping: (0, actions_core_1.defaultValues)(identify_1.default.fields),
        type: 'automatic'
    }
];
const destination = {
    name: 'Kameleoon (Actions)',
    slug: 'actions-kameleoon',
    mode: 'cloud',
    description: 'Send Segment events to Kameleoon',
    authentication: {
        scheme: 'custom',
        fields: {
            apiKey: {
                label: 'API Key',
                description: 'Kameleoon API key. You can generate one using the link in the help doc (https://help.kameleoon.com/setting-up-segment/).',
                type: 'password',
                required: true
            },
            sitecode: {
                label: 'Sitecode',
                description: 'Kameleoon project sitecode. You can find this project dashboard (https://help.kameleoon.com/question/how-do-i-find-my-site-id/).',
                type: 'string',
                required: true
            }
        },
        testAuthentication: (request, { settings }) => {
            if (settings.sitecode.toString().length !== 10) {
                throw new Error('Invalid project sitecode. Please check your sitecode');
            }
            const apiKey = Object.entries(JSON.parse(Buffer.from(settings.apiKey, 'base64').toString('ascii')))
                .map(([key, value]) => key + '=' + value)
                .join('&');
            return request(properties_1.BASE_URL + '/getapikey?' + apiKey, {
                method: 'GET'
            });
        }
    },
    presets,
    actions: {
        logEvent: logEvent_1.default,
        identify: identify_1.default,
        group: group_1.default,
        page: page_1.default
    }
};
exports.default = destination;
//# sourceMappingURL=index.js.map