"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
const client_1 = require("./client");
const action = {
    title: 'Sync Audience',
    description: 'Sync users to Taboola.',
    defaultSubscription: 'type = "track"',
    fields: {
        external_audience_id: {
            label: 'External Audience ID',
            description: 'The Audience ID from Taboola.',
            type: 'string',
            required: true,
            unsafe_hidden: true,
            default: {
                '@path': '$.context.personas.external_audience_id'
            }
        },
        segment_computation_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' }
            ]
        },
        user_email: {
            label: 'Email address',
            description: "The user's email address",
            type: 'string',
            unsafe_hidden: false,
            required: false,
            default: {
                '@if': {
                    exists: { '@path': '$.traits.email' },
                    then: { '@path': '$.traits.email' },
                    else: { '@path': '$.context.traits.email' }
                }
            }
        },
        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, events will be batched before being sent to Taboola. In most cases, batching should be enabled.',
            required: true,
            default: true
        },
        device_id: {
            label: 'Mobile Device ID',
            description: "To send iOS and Android Device IDs include them as 'ios_id' and 'android_id' via the 'Customized Setup' option when connecting your Audience.",
            type: 'string',
            required: false,
            unsafe_hidden: false,
            default: {
                '@if': {
                    exists: { '@path': '$.properties.ios_id' },
                    then: { '@path': '$.properties.ios_id' },
                    else: { '@path': '$.properties.android_id' }
                }
            }
        },
        batch_size: {
            label: 'Batch Size',
            description: 'Max Batch size to send to Taboola.',
            type: 'integer',
            default: 1000,
            required: true,
            unsafe_hidden: true
        },
        action: {
            label: 'Action',
            description: 'Action to perform on the audience.',
            type: 'string',
            required: false,
            unsafe_hidden: true,
            choices: [
                { label: 'Add', value: 'ADD' },
                { label: 'Remove', value: 'REMOVE' }
            ]
        }
    },
    perform: (request, { payload, audienceSettings }) => {
        if (!payload.external_audience_id) {
            throw new actions_core_1.IntegrationError('Bad Request: payload.external_audience_id missing.', 'INVALID_REQUEST_DATA', 400);
        }
        if (!payload.user_email && !payload.device_id) {
            throw new actions_core_1.IntegrationError("Bad Request: Either 'Email address' or 'Mobile Device ID' must be provided in the payload.", 'INVALID_REQUEST_DATA', 400);
        }
        const taboolaClient = new client_1.TaboolaClient(request, [payload], audienceSettings);
        return taboolaClient.sendToTaboola();
    },
    performBatch: (request, { payload: payloads, audienceSettings }) => {
        const taboolaClient = new client_1.TaboolaClient(request, payloads, audienceSettings);
        return taboolaClient.sendToTaboola();
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map