"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CONSTANTS = void 0;
const __1 = require("..");
exports.CONSTANTS = {
    ADD: 'ADD',
    REMOVE: 'REMOVE'
};
const action = {
    title: 'Sync Audience',
    description: 'Sync Engage Audiences to Courier lists',
    defaultSubscription: 'type = "identify" or type = "track"',
    fields: {
        segment_audience_id: {
            label: 'Audience ID',
            description: 'Segment Audience ID to which user identifier should be added or removed',
            type: 'string',
            unsafe_hidden: true,
            required: true,
            default: {
                '@path': '$.context.personas.computation_id'
            }
        },
        segment_audience_key: {
            label: 'Audience Key',
            description: 'Segment Audience key to which user identifier should be added or removed',
            type: 'string',
            unsafe_hidden: true,
            required: true,
            default: {
                '@path': '$.context.personas.computation_key'
            }
        },
        segment_computation_action: {
            label: 'Segment Computation Action',
            description: "Hidden field used to verify that the payload is generated by an Audience. Payloads not containing computation_class = 'audience' or 'journey_step' will be dropped before the perform() fuction call.",
            type: 'string',
            unsafe_hidden: true,
            required: true,
            default: {
                '@path': '$.context.personas.computation_class'
            },
            choices: [{ label: 'audience', value: 'audience' }, { label: 'journey_step', value: 'journey_step' }]
        },
        segment_user_id: {
            label: 'Segment User ID',
            description: 'The Segment userId value.',
            type: 'string',
            unsafe_hidden: true,
            required: false,
            default: { '@path': '$.userId' }
        },
        segment_anonymous_id: {
            label: 'Segment Anonymous ID',
            description: 'The Segment anonymousId value.',
            type: 'string',
            unsafe_hidden: true,
            required: false,
            default: { '@path': '$.anonymousId' }
        },
        traits_or_props: {
            label: 'Traits or properties object',
            description: 'A computed object for track and identify events. This field should not need to be edited.',
            type: 'object',
            required: true,
            unsafe_hidden: true,
            default: {
                '@if': {
                    exists: { '@path': '$.properties' },
                    then: { '@path': '$.properties' },
                    else: { '@path': '$.traits' }
                }
            }
        },
        audience_action: {
            label: 'Audience Action',
            description: 'Indicates if the user will be added or removed from the Audience',
            type: 'string',
            unsafe_hidden: true,
            choices: [
                { label: exports.CONSTANTS.ADD, value: exports.CONSTANTS.ADD },
                { label: exports.CONSTANTS.REMOVE, value: exports.CONSTANTS.REMOVE }
            ]
        }
    },
    perform: (request, { payload, settings }) => {
        return processPayload(request, payload, settings);
    }
};
const processPayload = (request, payload, settings) => {
    payload.audience_action = payload.traits_or_props[payload.segment_audience_key] ? exports.CONSTANTS.ADD : exports.CONSTANTS.REMOVE;
    const domain = (0, __1.getDomain)(settings.region);
    const list_id = `${payload.segment_audience_key}-${payload.segment_audience_id}`;
    const user_id = payload.segment_user_id ?? payload.segment_anonymous_id;
    if (payload.audience_action === exports.CONSTANTS.ADD) {
        return request(`${domain}/lists/${list_id}/subscriptions/${user_id}`, {
            method: 'PUT'
        });
    }
    else {
        return request(`${domain}/lists/${list_id}/subscriptions/${user_id}`, {
            method: 'DELETE'
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map