"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const shared_properties_1 = require("../shared-properties");
const utils_1 = require("../utils");
const action = {
    title: 'Send Audience',
    description: 'Send Engage Audience to a Profile Extension Table in Responsys',
    defaultSubscription: 'type = "identify" or type = "track"',
    fields: {
        userData: {
            label: 'Recipient Data',
            description: 'Record data that represents field names and corresponding values for each profile.',
            type: 'object',
            defaultObjectUI: 'keyvalue',
            required: true,
            additionalProperties: true,
            properties: {
                EMAIL_ADDRESS_: {
                    label: 'Email address',
                    description: "The user's email address",
                    type: 'string',
                    format: 'email',
                    required: false
                },
                CUSTOMER_ID_: {
                    label: 'Customer ID',
                    description: 'Responsys Customer ID.',
                    type: 'string',
                    required: false
                }
            },
            default: {
                EMAIL_ADDRESS_: {
                    '@if': {
                        exists: { '@path': '$.traits.email' },
                        then: { '@path': '$.traits.email' },
                        else: { '@path': '$.context.traits.email' }
                    }
                },
                CUSTOMER_ID_: { '@path': '$.userId' }
            }
        },
        computation_key: {
            label: 'Segment Audience Key',
            description: 'A unique identifier assigned to a specific audience in Segment.',
            type: 'string',
            required: true,
            unsafe_hidden: true,
            default: { '@path': '$.context.personas.computation_key' }
        },
        traits_or_props: {
            label: 'Traits or Properties',
            description: 'Hidden field used to access traits or properties objects from Engage payloads.',
            type: 'object',
            required: true,
            unsafe_hidden: true,
            default: {
                '@if': {
                    exists: { '@path': '$.traits' },
                    then: { '@path': '$.traits' },
                    else: { '@path': '$.properties' }
                }
            }
        },
        computation_class: {
            label: 'Segment Audience Computation Class',
            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',
            required: true,
            unsafe_hidden: true,
            default: { '@path': '$.context.personas.computation_class' },
            choices: [{ label: 'Audience', value: 'audience' }, { label: 'journey_step', value: 'journey_step' }]
        },
        enable_batching: shared_properties_1.use_responsys_async_api,
        batch_size: shared_properties_1.batch_size,
        stringify: {
            label: 'Stringify Recipient Data',
            description: 'If true, all Recipient data will be converted to strings before being sent to Responsys.',
            type: 'boolean',
            required: true,
            default: false
        },
        timestamp: {
            label: 'Timestamp',
            description: 'The timestamp of when the event occurred.',
            type: 'datetime',
            required: true,
            unsafe_hidden: true,
            default: {
                '@path': '$.timestamp'
            }
        },
        retry: {
            label: 'Delay (seconds)',
            description: `A delay of the selected seconds will be added before retrying a failed request.
                    Max delay allowed is 600 secs (10 mins). The default is 0 seconds.`,
            type: 'number',
            choices: [
                { label: '0 secs', value: 0 },
                { label: '30 secs', value: 30 },
                { label: '120 secs', value: 120 },
                { label: '300 secs', value: 300 },
                { label: '480 secs', value: 480 },
                { label: '600 secs', value: 600 }
            ],
            required: false,
            unsafe_hidden: true,
            default: 0
        }
    },
    perform: async (request, data) => {
        const { payload, settings, statsContext } = data;
        const userDataFieldNames = (0, utils_1.getUserDataFieldNames)(data);
        (0, utils_1.testConditionsToRetry)({
            profileExtensionTable: settings.profileExtensionTable,
            timestamp: payload.timestamp,
            statsContext: statsContext,
            retry: payload.retry
        });
        (0, utils_1.validateListMemberPayload)(payload.userData);
        return (0, utils_1.sendCustomTraits)(request, [payload], data.settings, userDataFieldNames, true);
    },
    performBatch: async (request, data) => {
        const { payload, settings, statsContext } = data;
        const userDataFieldNames = (0, utils_1.getUserDataFieldNames)(data);
        (0, utils_1.testConditionsToRetry)({
            profileExtensionTable: settings.profileExtensionTable,
            timestamp: payload[0].timestamp,
            statsContext: statsContext,
            retry: payload[0].retry
        });
        return (0, utils_1.sendCustomTraits)(request, data.payload, data.settings, userDataFieldNames, true);
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map