"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
const shared_properties_1 = require("../shared-properties");
const utils_1 = require("../utils");
const functions_1 = require("./functions");
const action = {
    title: 'Send to PET (Profile Extension Table)',
    description: 'Send values to a Profile Extension Table in Responsys.',
    fields: {
        userData: { ...shared_properties_1.recipient_data, additionalProperties: true },
        folder_name: shared_properties_1.folder_name,
        pet_name: {
            label: 'Profile Extension Table Name',
            description: 'The PET (Profile Extension Table) name. Overrides the default Profile Extension Table name in Settings.',
            type: 'string',
            required: false
        },
        timestamp: {
            label: 'Timestamp',
            description: 'The timestamp of when the event occurred.',
            type: 'datetime',
            required: false,
            unsafe_hidden: true,
            default: {
                '@path': '$.timestamp'
            }
        },
        use_responsys_async_api: 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: false,
            default: false
        },
        default_permission_status: shared_properties_1.default_permission_status
    },
    perform: async (request, data) => {
        const { payload, settings, auth } = data;
        const userDataFieldNames = (0, utils_1.getUserDataFieldNames)(data);
        const profileExtensionTable = String(payload.pet_name || settings.profileExtensionTable);
        if (!(typeof profileExtensionTable !== 'undefined' &&
            profileExtensionTable !== null &&
            profileExtensionTable.trim().length > 0)) {
            throw new actions_core_1.IntegrationError('Send Custom Traits Action requires "PET Name" setting field to be populated', 'PET_NAME_SETTING_MISSING', 400);
        }
        (0, utils_1.validateListMemberPayload)(payload.userData);
        return (0, functions_1.sendToPet)(request, auth, [payload], settings, userDataFieldNames);
    },
    performBatch: async (request, data) => {
        const { payload, settings, auth } = data;
        const userDataFieldNames = (0, utils_1.getUserDataFieldNames)(data);
        const validatedPayloads = [];
        for (const item of payload) {
            const profileExtensionTable = String(item.pet_name || settings.profileExtensionTable);
            if (!profileExtensionTable || profileExtensionTable.trim().length === 0) {
                continue;
            }
            if (item.userData.EMAIL_ADDRESS_ || item.userData.RIID_ || item.userData.CUSTOMER_ID_) {
                validatedPayloads.push(item);
            }
        }
        return (0, functions_1.sendToPet)(request, auth, validatedPayloads, settings, userDataFieldNames);
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map