"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const properties_1 = require("../properties");
const functions_1 = require("../functions");
const action = {
    title: 'Add to List',
    description: 'Add users to a list in Marketo.',
    defaultSubscription: 'event = "Audience Entered"',
    fields: {
        external_id: { ...properties_1.external_id },
        lookup_field: { ...properties_1.lookup_field },
        data: { ...properties_1.data },
        enable_batching: { ...properties_1.enable_batching },
        batch_size: { ...properties_1.batch_size },
        event_name: { ...properties_1.event_name }
    },
    hooks: {
        retlOnMappingSave: {
            label: 'Connect to a static list in Marketo',
            description: 'When saving this mapping, we will create a static list in Marketo using the fields you provided.',
            inputFields: {
                list_id: {
                    type: 'string',
                    label: 'Existing List ID',
                    description: 'The ID of the Marketo Static List that users will be synced to. If defined, we will not create a new list.',
                    required: false
                },
                list_name: {
                    type: 'string',
                    label: 'List Name',
                    description: 'The name of the Marketo Static List that you would like to create.',
                    required: false
                }
            },
            outputTypes: {
                id: {
                    type: 'string',
                    label: 'ID',
                    description: 'The ID of the created Marketo Static List that users will be synced to.',
                    required: false
                },
                name: {
                    type: 'string',
                    label: 'List Name',
                    description: 'The name of the created Marketo Static List that users will be synced to.',
                    required: false
                }
            },
            performHook: async (request, { settings, hookInputs, statsContext }) => {
                if (hookInputs.list_id) {
                    try {
                        return (0, functions_1.getList)(request, settings, hookInputs.list_id);
                    }
                    catch (e) {
                        const message = e.message || JSON.stringify(e) || 'Failed to get list';
                        const code = e.code || 'GET_LIST_FAILURE';
                        return {
                            error: {
                                message,
                                code
                            }
                        };
                    }
                }
                try {
                    const input = {
                        audienceName: hookInputs.list_name,
                        settings: settings
                    };
                    const listId = await (0, functions_1.createList)(request, input, statsContext);
                    return {
                        successMessage: `List '${hookInputs.list_name}' (id: ${listId}) created successfully!`,
                        savedData: {
                            id: listId,
                            name: hookInputs.list_name
                        }
                    };
                }
                catch (e) {
                    const message = e.message || JSON.stringify(e) || 'Failed to create list';
                    const code = e.code || 'CREATE_LIST_FAILURE';
                    return {
                        error: {
                            message,
                            code
                        }
                    };
                }
            }
        }
    },
    perform: async (request, { settings, payload, statsContext, hookOutputs }) => {
        statsContext?.statsClient?.incr('addToAudience', 1, statsContext?.tags);
        return (0, functions_1.addToList)(request, settings, payload, statsContext, hookOutputs?.retlOnMappingSave?.outputs);
    },
    performBatch: async (request, { settings, payload, statsContext, hookOutputs }) => {
        statsContext?.statsClient?.incr('addToAudience.batch', 1, statsContext?.tags);
        return (0, functions_1.addToListBatch)(request, settings, payload, statsContext, hookOutputs?.retlOnMappingSave?.outputs);
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map