"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
const emarsys_helper_1 = require("../emarsys-helper");
const action = {
    title: 'Remove from Contact List',
    description: 'Remove a contact from a contact list.',
    fields: {
        contactlistid: {
            label: 'Id of the contact list',
            description: 'The Id of the contactlist',
            type: 'integer',
            required: true,
            dynamic: true
        },
        key_field: {
            label: 'Key field',
            description: 'The field to use to find the contact',
            type: 'string',
            required: true,
            dynamic: true
        },
        key_value: {
            label: 'Key value',
            description: 'Value for the key field used to find the contact. E.g. the email address  ',
            type: 'string',
            required: true,
            dynamic: false
        }
    },
    dynamicFields: {
        contactlistid: async (request) => {
            return (0, emarsys_helper_1.getContactLists)(request);
        },
        key_field: async (request) => {
            return (0, emarsys_helper_1.getFields)(request);
        }
    },
    perform: async (request, data) => {
        data.payload.contactlistid = parseInt(data.payload.contactlistid.toString().replace(/[^0-9]/g, ''));
        if (data.payload.contactlistid > 0) {
            const payload = {
                contactlistid: data.payload.contactlistid,
                key_id: data.payload.key_field,
                external_ids: [data.payload.key_value]
            };
            const response = await request(`${emarsys_helper_1.API_BASE}contactlist/${data.payload.contactlistid}/delete`, {
                method: 'post',
                json: payload,
                throwHttpErrors: false
            });
            switch (response?.status) {
                case 200:
                    try {
                        const body = await response.json();
                        if (body.replyCode === 0)
                            return response;
                        else
                            throw new actions_core_1.APIError(`Something went wrong while removing from contact list: ${body?.replyText ?? 'UNKNOWN'}`, 500);
                    }
                    catch (err) {
                        throw new actions_core_1.APIError('Invalid JSON response', 400);
                    }
                case 400:
                    throw new actions_core_1.APIError('The contact could not be removed from the contact list', 400);
                case 429:
                    throw new actions_core_1.RetryableError('Rate limit reached.');
                default:
                    throw new actions_core_1.RetryableError('There seems to be an API issue.');
            }
        }
        else {
            throw new actions_core_1.PayloadValidationError('ContactlistId must be >0');
        }
    },
    performBatch: async (request, data) => {
        if (data && data.payload && Array.isArray(data.payload)) {
            const batches = {};
            data.payload.forEach((payload) => {
                if (!batches[`${payload.contactlistid}-${payload.key_field}`]) {
                    batches[`${payload.contactlistid}-${payload.key_field}`] = {
                        contactlistid: payload.contactlistid,
                        key_id: payload.key_field,
                        external_ids: []
                    };
                }
                batches[`${payload.contactlistid}-${payload.key_field}`].external_ids.push(payload.key_value);
            });
            const requests = [];
            for (const key in batches) {
                const batch = batches[key];
                const payload = {
                    key_id: batch.key_id,
                    external_ids: batch.external_ids
                };
                const response = request(`${emarsys_helper_1.API_BASE}contactlist/${batch.contactlistid}/delete`, {
                    method: 'post',
                    json: payload,
                    throwHttpErrors: false
                });
                requests.push(response);
            }
            if (requests.length > 0) {
                return Promise.all(requests);
            }
        }
        return 0;
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map