"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const custom_audience_operations_1 = require("../../launchdarkly-audiences/syncAudience/custom-audience-operations");
const constants_1 = require("../constants");
const types_1 = require("../types");
const action = {
    title: 'Sync Audience',
    description: 'Sync Engage Audiences to LaunchDarkly segments',
    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' }]
        },
        context_kind: {
            label: 'Context kind',
            description: "The event's context kind. To learn more about context kinds and where you can find a list of context kinds LaunchDarkly has observed, read [Context kinds](https://docs.launchdarkly.com/home/contexts/context-kinds).",
            type: 'string',
            required: true,
            default: 'user'
        },
        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' }
        },
        user_email: {
            label: 'Email address',
            description: "The user's email address",
            type: 'string',
            unsafe_hidden: true,
            required: false,
            default: {
                '@if': {
                    exists: { '@path': '$.traits.email' },
                    then: { '@path': '$.traits.email' },
                    else: { '@path': '$.context.traits.email' }
                }
            }
        },
        context_key: {
            label: 'Context key',
            description: 'The unique LaunchDarkly context key. In most cases the Segment UserId should be used.',
            type: 'string',
            required: true,
            default: types_1.Priority.UserIdOnly,
            choices: [
                { value: types_1.Priority.UserIdThenEmail, label: 'Use Segment UserId or email' },
                { value: types_1.Priority.UserIdThenAnonymousId, label: 'Use Segment UserId or Segment AnonymousId' },
                { value: types_1.Priority.UserIdThenEmailThenAnonymousId, label: 'Use Segment UserId or email or Segment AnonymousId' },
                { value: types_1.Priority.UserIdOnly, label: 'Use Segment UserId only' },
                { value: types_1.Priority.EmailOnly, label: 'Use email only' }
            ]
        },
        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' }
                }
            }
        },
        enable_batching: {
            type: 'boolean',
            label: 'Batch events',
            description: 'When enabled, the action will batch events before sending them to LaunchDarkly. In most cases, batching should be enabled.',
            required: false,
            default: true
        },
        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: constants_1.CONSTANTS.ADD, value: constants_1.CONSTANTS.ADD },
                { label: constants_1.CONSTANTS.REMOVE, value: constants_1.CONSTANTS.REMOVE }
            ]
        }
    },
    perform: (request, { payload, settings }) => {
        payload.audience_action = payload.traits_or_props[payload.segment_audience_key] ? constants_1.CONSTANTS.ADD : constants_1.CONSTANTS.REMOVE;
        return (0, custom_audience_operations_1.processPayload)(request, settings, [payload]);
    },
    performBatch: (request, { payload, settings }) => {
        payload.forEach((event) => (event.audience_action = event.traits_or_props[event.segment_audience_key] ? constants_1.CONSTANTS.ADD : constants_1.CONSTANTS.REMOVE));
        return (0, custom_audience_operations_1.processPayload)(request, settings, payload);
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map