"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 syncAudience_1 = __importDefault(require("./syncAudience"));
const functions_1 = require("./functions");
const constants_1 = require("./constants");
const destination = {
    name: 'Iterable Lists (Actions)',
    slug: 'actions-iterable-audiences',
    mode: 'cloud',
    description: 'Sync Segment Engage Audiences to Iterable Lists',
    authentication: {
        scheme: 'custom',
        fields: {
            apiKey: {
                type: 'password',
                label: 'API Key',
                description: "To obtain the API Key, go to the Iterable app and naviate to Integrations > API Keys. Create a new API Key with the 'Server-Side' type.",
                required: true
            },
            iterableProjectType: {
                type: 'string',
                label: 'Iterable Project Type',
                description: 'Select the type of your Iterable project. Hybrid projects support both email and user ID based identification, while User ID-Based projects only support user ID based identification.',
                choices: [
                    { label: 'Hybrid Project', value: constants_1.CONSTANTS.HYBRID_PROJECT_TYPE },
                    { label: 'User ID-Based Project', value: constants_1.CONSTANTS.USER_ID_PROJECT_TYPE }
                ],
                default: constants_1.CONSTANTS.HYBRID_PROJECT_TYPE
            }
        },
        testAuthentication: (request, { settings }) => {
            const { apiKey } = settings;
            return request('https://api.iterable.com/api/lists', {
                method: 'GET',
                skipResponseCloning: true,
                headers: { 'Api-Key': apiKey }
            });
        }
    },
    audienceFields: {
        updateExistingUsersOnly: {
            label: 'Update existing users only',
            description: 'When true, Iterable ignores requests for unknown userIds and email addresses. This field is only relevant for non-email based Iterable projects: For email-based projects users will continue to be created in Iterable if a user is added or removed from a List.',
            type: 'boolean',
            default: false,
            required: false
        },
        globalUnsubscribe: {
            label: 'Channel Unsubscribe',
            description: "Unsubscribe email from list's associated channel - essentially a global unsubscribe. Only valid when unsubscribing a user from a List.",
            type: 'boolean',
            default: false,
            required: false
        },
        campaignId: {
            label: 'Campaign ID',
            description: 'The numeric Campaign ID to associate with the unsubscribe. Only valid when unsubscribing a user from a List.',
            type: 'number',
            required: false
        }
    },
    audienceConfig: {
        mode: {
            type: 'synced',
            full_audience_sync: false
        },
        async createAudience(request, { settings, personas }) {
            if (!personas) {
                throw new actions_core_1.IntegrationError('Missing computation parameters: Key', 'MISSING_REQUIRED_FIELD', 422);
            }
            if (!personas.computation_key) {
                throw new actions_core_1.IntegrationError('Missing computation parameters: Key', 'MISSING_REQUIRED_FIELD', 422);
            }
            const audienceKey = personas.computation_key;
            let externalId = await (0, functions_1.getAudienceByName)(request, settings, audienceKey);
            if (externalId) {
                return { externalId };
            }
            externalId = await (0, functions_1.createAudience)(request, settings, audienceKey);
            return { externalId };
        },
        async getAudience(request, { settings, externalId }) {
            const id = await (0, functions_1.getAudienceByID)(request, settings, externalId);
            if (!id) {
                throw new actions_core_1.IntegrationError(`Audience with ID ${externalId} not found in Iterable`, 'AUDIENCE_NOT_FOUND', 404);
            }
            return { externalId: id };
        }
    },
    extendRequest({ settings }) {
        const { apiKey } = settings;
        return {
            headers: { 'Api-Key': apiKey }
        };
    },
    actions: {
        syncAudience: syncAudience_1.default
    },
    presets: [
        {
            name: 'Entities Audience Membership Changed',
            partnerAction: 'syncAudience',
            mapping: (0, actions_core_1.defaultValues)(syncAudience_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'warehouse_audience_membership_changed_identify'
        },
        {
            name: 'Associated Entity Added',
            partnerAction: 'syncAudience',
            mapping: (0, actions_core_1.defaultValues)(syncAudience_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'warehouse_entity_added_track'
        },
        {
            name: 'Associated Entity Removed',
            partnerAction: 'syncAudience',
            mapping: (0, actions_core_1.defaultValues)(syncAudience_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'warehouse_entity_removed_track'
        },
        {
            name: 'Journeys Step Entered',
            partnerAction: 'syncAudience',
            mapping: (0, actions_core_1.defaultValues)(syncAudience_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'journeys_step_entered_track'
        }
    ]
};
exports.default = destination;
//# sourceMappingURL=index.js.map