"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: 'Pendo Audiences',
    slug: 'actions-pendo-audiences',
    mode: 'cloud',
    description: 'Sync Segment Engage Audiences to Pendo Segments.',
    authentication: {
        scheme: 'custom',
        fields: {
            integrationKey: {
                label: 'Integration Key',
                description: 'Your Pendo Integration Key. Found in Pendo under Settings > Integrations > Integration Keys.',
                type: 'password',
                required: true
            },
            region: {
                label: 'Region',
                description: 'The region your Pendo account is hosted in.',
                type: 'string',
                required: true,
                choices: Object.keys(constants_1.REGIONS).map((key) => ({
                    label: constants_1.REGIONS[key].name,
                    value: constants_1.REGIONS[key].name
                })),
                default: 'DEFAULT'
            }
        },
        testAuthentication: async (request, { settings }) => {
            const { region } = settings;
            return await request(`${(0, functions_1.getDomain)(region)}/api/v1/token/verify`, {
                method: 'GET'
            });
        }
    },
    extendRequest({ settings }) {
        return {
            headers: {
                'x-pendo-integration-key': settings.integrationKey,
                'Content-Type': 'application/json'
            }
        };
    },
    audienceFields: {
        audienceName: {
            label: 'Pendo Segment Name',
            description: 'A name for the Pendo Segment. Leave blank to use the Segment audience name.',
            type: 'string',
            required: false
        }
    },
    audienceConfig: {
        mode: {
            type: 'synced',
            full_audience_sync: false
        },
        async createAudience(request, createAudienceInput) {
            const { audienceName, audienceSettings, settings: { region } } = createAudienceInput;
            const segmentName = audienceSettings?.audienceName ?? audienceName;
            if (!segmentName) {
                throw new actions_core_1.IntegrationError('A Pendo Segment name is required', 'MISSING_REQUIRED_FIELD', 422);
            }
            const segmentId = await (0, functions_1.createSegment)(request, region, segmentName);
            return { externalId: segmentId };
        },
        async getAudience(request, getAudienceInput) {
            const { externalId, settings: { region } } = getAudienceInput;
            const segmentId = await (0, functions_1.getSegment)(request, region, externalId);
            return { externalId: segmentId };
        }
    },
    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