"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 sendEvent_1 = __importDefault(require("./sendEvent"));
const request_params_1 = require("./request-params");
const identifyUser_1 = __importDefault(require("./identifyUser"));
const destination = {
    name: 'GWEN (Actions)',
    slug: 'actions-cloud-gwen',
    mode: 'cloud',
    authentication: {
        scheme: 'custom',
        fields: {
            apiKey: {
                label: 'API Key',
                description: 'GWEN API key. Can be found [here](http://gwen.insertcoin.se/iam/api-token) (login required)',
                type: 'password',
                required: true
            }
        },
        testAuthentication: (request) => {
            return request(request_params_1.baseURL, {
                ...request_params_1.defaultRequestParams,
                json: {
                    operationName: 'ValidateAPIKey',
                    query: `query ValidateAPIKey {
                validateApiKey
              }`
                }
            }).then(async (response) => {
                const { errors } = await response.json();
                if (errors && errors.length > 0) {
                    throw new Error('Invalid API key. Make sure you have the correct API key. If the problem persists please contant support@gwenplatform.com.');
                }
            });
        }
    },
    extendRequest: ({ settings }) => {
        return {
            headers: {
                'Content-Type': 'application/json',
                Authorization: settings.apiKey
            }
        };
    },
    actions: {
        sendEvent: sendEvent_1.default,
        identifyUser: identifyUser_1.default
    },
    presets: [
        {
            name: 'Send an event to GWEN',
            subscribe: 'type = "track"',
            partnerAction: 'sendEvent',
            mapping: (0, actions_core_1.defaultValues)(sendEvent_1.default.fields),
            type: 'automatic'
        },
        {
            name: 'Identify a user',
            subscribe: 'type = "identify"',
            partnerAction: 'identifyUser',
            mapping: (0, actions_core_1.defaultValues)(identifyUser_1.default.fields),
            type: 'automatic'
        }
    ]
};
exports.default = destination;
//# sourceMappingURL=index.js.map