"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../utils");
const functions_1 = require("./functions");
const shared_properties_1 = require("../shared-properties");
const getAllPetsWaitInterval = 60;
const createPetWaitInterval = 6000;
const action = {
    title: 'Send Audience as PET (Profile Extension Table)',
    description: 'Send Engage Audience to a separate, newly created Profile Extension Table in Responsys',
    defaultSubscription: 'type = "identify" or type = "track"',
    fields: {
        userData: shared_properties_1.recipient_data,
        folder_name: {
            label: 'Folder Name',
            description: 'The name of the folder where the new Profile Extension Table will be created. Overrides the default folder name in Settings.',
            type: 'string',
            required: false,
            default: 'Segment'
        },
        pet_name: {
            label: 'Profile Extension Table Name',
            description: 'The PET (Profile Extension Table) name. The default value is the audience key in Segment.',
            type: 'string',
            required: true,
            unsafe_hidden: true,
            default: { '@path': '$.context.personas.computation_key' }
        },
        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' }
                }
            }
        },
        timestamp: {
            label: 'Timestamp',
            description: 'The timestamp of when the event occurred.',
            type: 'datetime',
            required: false,
            unsafe_hidden: true,
            default: {
                '@path': '$.timestamp'
            }
        },
        retry: shared_properties_1.retry,
        default_permission_status: shared_properties_1.default_permission_status
    },
    perform: async (request, data) => {
        const { payload, settings, auth } = data;
        (0, utils_1.validateListMemberPayload)(payload.userData);
        await new Promise((resolve) => setTimeout(resolve, getAllPetsWaitInterval));
        const petAlreadyExists = await (0, functions_1.petExists)(request, settings, payload.pet_name);
        if (!petAlreadyExists) {
            await new Promise((resolve) => setTimeout(resolve, createPetWaitInterval));
            await (0, functions_1.createPet)(request, settings, payload);
        }
        return await (0, functions_1.updateProfileListAndPet)(request, auth, settings, [payload]);
    },
    performBatch: async (request, data) => {
        const { payload, settings, auth } = data;
        const validPayloads = [];
        for (const item of payload) {
            try {
                (0, utils_1.validateListMemberPayload)(item.userData);
                validPayloads.push(item);
            }
            catch (error) {
                console.error(error);
            }
        }
        await new Promise((resolve) => setTimeout(resolve, getAllPetsWaitInterval));
        const petAlreadyExists = await (0, functions_1.petExists)(request, settings, payload[0].pet_name);
        if (!petAlreadyExists) {
            await new Promise((resolve) => setTimeout(resolve, createPetWaitInterval));
            await (0, functions_1.createPet)(request, settings, payload[0]);
        }
        return await (0, functions_1.updateProfileListAndPet)(request, auth, settings, payload);
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map