"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 actions_core_2 = require("@segment/actions-core");
const regional_endpoints_1 = require("./regional-endpoints");
const sendEvent_1 = __importDefault(require("./sendEvent"));
const defaultVals = { ...(0, actions_core_2.defaultValues)(sendEvent_1.default.fields) };
const destination = {
    name: 'Gainsight Px Cloud (Actions)',
    slug: 'actions-gainsight-px-cloud',
    mode: 'cloud',
    authentication: {
        scheme: 'basic',
        fields: {
            apiKey: {
                label: 'API Key',
                description: 'Gainsight PX API key. You can find this key in the "Administration/Products" screen.',
                type: 'password',
                required: true
            },
            dataCenter: {
                label: 'Data center',
                description: 'The PX data center where your PX subscription is hosted.',
                type: 'string',
                format: 'text',
                required: true,
                choices: [
                    {
                        label: 'North America',
                        value: 'north_america'
                    },
                    {
                        label: 'Europe',
                        value: 'europe'
                    },
                    {
                        label: 'US2',
                        value: 'us2'
                    }
                ]
            }
        },
        testAuthentication: async (request, { settings }) => {
            const endpoint = (0, regional_endpoints_1.getEndpointByRegion)('track', settings.dataCenter);
            const response = await request(endpoint, {
                method: 'post',
                username: settings.apiKey,
                password: '',
                json: [],
                throwHttpErrors: false
            });
            if (response.status === 400) {
                return true;
            }
            throw new actions_core_1.IntegrationError('Invalid API key', 'Invalid API Key', 401);
        }
    },
    extendRequest({ settings }) {
        return {
            username: settings.apiKey,
            password: ''
        };
    },
    actions: {
        sendEvent: sendEvent_1.default
    },
    presets: [
        {
            name: 'Identify User',
            subscribe: 'type = "identify"',
            partnerAction: 'sendEvent',
            mapping: defaultVals,
            type: 'automatic'
        },
        {
            name: 'Group User',
            subscribe: 'type = "group"',
            partnerAction: 'sendEvent',
            mapping: defaultVals,
            type: 'automatic'
        },
        {
            name: 'Track Event',
            subscribe: 'type = "track"',
            partnerAction: 'sendEvent',
            mapping: defaultVals,
            type: 'automatic'
        },
        {
            name: 'Track Page View',
            subscribe: 'type = "page"',
            partnerAction: 'sendEvent',
            mapping: defaultVals,
            type: 'automatic'
        }
    ]
};
exports.default = destination;
//# sourceMappingURL=index.js.map