"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 customEvents_1 = __importDefault(require("./customEvents"));
const setAttributes_1 = __importDefault(require("./setAttributes"));
const manageTags_1 = __importDefault(require("./manageTags"));
const utilities_1 = require("./utilities");
const registerAndAssociate_1 = __importDefault(require("./registerAndAssociate"));
const destination = {
    name: 'Airship (Actions)',
    slug: 'actions-airship',
    mode: 'cloud',
    description: 'Send events from Segment to Airship for message triggering, audience targeting, personalization, and analytics.',
    authentication: {
        scheme: 'custom',
        fields: {
            access_token: {
                label: 'Access Token',
                description: 'Create in the Airship Go dashboard in Settings->Partner Integrations->Segment',
                type: 'password',
                required: true
            },
            app_key: {
                label: 'App Key',
                description: 'The App Key identifies the Airship Project to which API requests are made.',
                type: 'password',
                required: true
            },
            endpoint: {
                label: 'Data Center',
                description: 'US or EU',
                type: 'string',
                choices: [
                    { label: 'US', value: 'US' },
                    { label: 'EU', value: 'EU' }
                ],
                default: 'US',
                required: true
            }
        },
        testAuthentication: (request, { settings }) => {
            const endpoint = (0, utilities_1.map_endpoint)(settings.endpoint);
            return request(`${endpoint}/api/custom-events/`, {
                method: 'post',
                json: {
                    body: {
                        name: 'test_segment_auth'
                    },
                    user: {
                        named_user_id: '549ce761-56c9-4899-9026-816d0c3bffd1'
                    }
                }
            });
        }
    },
    presets: [
        {
            name: 'Custom Events',
            type: 'automatic',
            subscribe: 'type = "track"',
            partnerAction: 'customEvents',
            mapping: (0, actions_core_1.defaultValues)(customEvents_1.default.fields)
        },
        {
            name: 'Set Attributes',
            type: 'automatic',
            subscribe: 'type = "identify"',
            partnerAction: 'setAttributes',
            mapping: (0, actions_core_1.defaultValues)(setAttributes_1.default.fields)
        }
    ],
    onDelete: async (request, { settings, payload }) => {
        const endpoint = (0, utilities_1.map_endpoint)(settings.endpoint);
        return request(`${endpoint}/api/named_users/uninstall`, {
            method: 'post',
            json: {
                named_user_id: [payload.userId]
            }
        });
    },
    extendRequest({ settings }) {
        return {
            headers: {
                Authorization: `Bearer ${settings.access_token}`,
                'X-UA-Appkey': `${settings.app_key}`,
                Accept: 'application/vnd.urbanairship+json; version=3',
                'Content-Type': 'application/json'
            }
        };
    },
    actions: {
        customEvents: customEvents_1.default,
        setAttributes: setAttributes_1.default,
        manageTags: manageTags_1.default,
        registerAndAssociate: registerAndAssociate_1.default
    }
};
exports.default = destination;
//# sourceMappingURL=index.js.map