"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 addToList_1 = __importDefault(require("./addToList"));
const removeFromList_1 = __importDefault(require("./removeFromList"));
const constants_1 = require("./constants");
const functions_1 = require("./functions");
const destination = {
    name: 'Marketo Static Lists (Actions)',
    slug: 'actions-marketo-static-lists',
    mode: 'cloud',
    authentication: {
        scheme: 'oauth2',
        fields: {
            client_id: {
                label: 'Client ID',
                description: 'Your Marketo REST API Client ID.',
                type: 'string',
                required: true
            },
            client_secret: {
                label: 'Client Secret',
                description: 'Your Marketo REST API Client Secret.',
                type: 'password',
                required: true
            },
            api_endpoint: {
                label: 'API Endpoint',
                description: 'Your Marketo REST API Endpoint in this format: https://<your_account_id>.mktorest.com.',
                type: 'string',
                required: true
            },
            folder_name: {
                label: 'Folder Name',
                description: 'Name of the folder in which to create static lists.',
                type: 'string',
                required: true
            }
        },
        refreshAccessToken: async (request, { settings }) => {
            return { accessToken: await (0, functions_1.getAccessToken)(request, settings) };
        }
    },
    extendRequest({ auth }) {
        return {
            headers: {
                authorization: `Bearer ${auth?.accessToken}`
            }
        };
    },
    audienceFields: {},
    audienceConfig: {
        mode: {
            type: 'synced',
            full_audience_sync: false
        },
        async createAudience(request, createAudienceInput) {
            const listId = await (0, functions_1.createList)(request, createAudienceInput, createAudienceInput.statsContext);
            return {
                externalId: listId
            };
        },
        async getAudience(request, getAudienceInput) {
            const endpoint = (0, functions_1.formatEndpoint)(getAudienceInput.settings.api_endpoint);
            const listId = getAudienceInput.externalId;
            const statsClient = getAudienceInput?.statsContext?.statsClient;
            const statsTags = getAudienceInput?.statsContext?.tags;
            const accessToken = await (0, functions_1.getAccessToken)(request, getAudienceInput.settings);
            const getListUrl = endpoint + constants_1.GET_LIST_ENDPOINT.replace('listId', listId);
            const getListResponse = await request(getListUrl, {
                method: 'GET',
                headers: {
                    authorization: `Bearer ${accessToken}`
                }
            });
            if (!getListResponse.data.success && getListResponse.data.errors) {
                statsClient?.incr('getAudience.error', 1, statsTags);
                throw new actions_core_1.IntegrationError(`${getListResponse.data.errors[0].message}`, 'INVALID_RESPONSE', 400);
            }
            statsClient?.incr('getAudience.success', 1, statsTags);
            return {
                externalId: listId
            };
        }
    },
    actions: {
        addToList: addToList_1.default,
        removeFromList: removeFromList_1.default
    },
    presets: [
        {
            name: 'Entities Audience Entered',
            partnerAction: 'addToList',
            mapping: { ...(0, actions_core_1.defaultValues)(addToList_1.default.fields) },
            type: 'specificEvent',
            eventSlug: 'warehouse_audience_entered_track'
        },
        {
            name: 'Entities Audience Exited',
            partnerAction: 'removeFromList',
            mapping: (0, actions_core_1.defaultValues)(removeFromList_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'warehouse_audience_exited_track'
        },
        {
            name: 'Associated Entity Added',
            partnerAction: 'addToList',
            mapping: (0, actions_core_1.defaultValues)(addToList_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'warehouse_entity_added_track'
        },
        {
            name: 'Associated Entity Removed',
            partnerAction: 'removeFromList',
            mapping: (0, actions_core_1.defaultValues)(removeFromList_1.default.fields),
            type: 'specificEvent',
            eventSlug: 'warehouse_entity_removed_track'
        },
        {
            name: 'Journeys Step Entered',
            partnerAction: 'addToList',
            mapping: { ...(0, actions_core_1.defaultValues)(addToList_1.default.fields) },
            type: 'specificEvent',
            eventSlug: 'journeys_step_entered_track'
        }
    ]
};
exports.default = destination;
//# sourceMappingURL=index.js.map