"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../utils");
const getCustomObjects_1 = require("./getCustomObjects");
const action = {
    title: 'Create or Update Object',
    description: 'Create or Update a Custom Object (including Companies) in Encharge. If an email or a user ID is provided, an Encharge user will be associated with the object. A new user will be created if the email or user ID does not exist in Encharge.',
    defaultSubscription: 'type = "group"',
    fields: {
        objectType: {
            label: 'Object Type',
            description: 'The type of Encharge object to create or update.',
            type: 'string',
            required: true,
            dynamic: true,
            default: 'company'
        },
        externalId: {
            type: 'string',
            required: false,
            label: 'External Object ID',
            description: 'An ID from your app/database that is used to uniquely identify the object in Encharge.',
            default: { '@path': '$.groupId' }
        },
        id: {
            type: 'string',
            required: false,
            description: 'The Encharge ID of the object. Usually, you want to omit this and use External ID.',
            label: 'Object ID'
        },
        objectData: {
            type: 'object',
            label: 'Object Fields',
            description: 'Data for the object fields in Encharge. Any unexisting fields will be ignored.',
            required: false,
            default: { '@path': '$.traits' }
        },
        userId: {
            type: 'string',
            description: 'The User ID of the user to associate with the object. If no email or user ID is provided, no user will be created and associated.',
            label: 'User ID',
            default: { '@path': '$.userId' }
        },
        email: {
            type: 'string',
            description: 'The email of the user to associate with the object. If no email or user ID is provided, no user will be created and associated.',
            label: 'Email',
            default: { '@path': '$.email' }
        }
    },
    dynamicFields: {
        objectType: getCustomObjects_1.getCustomObjects
    },
    perform: (request, data) => {
        const payload = {
            type: 'group',
            objectType: data.payload.objectType,
            properties: {
                ...(data.payload.objectData || {}),
                externalId: data.payload.externalId,
                id: data.payload.id
            },
            user: {
                email: data.payload.email,
                userId: data.payload.userId
            }
        };
        return request(utils_1.enchargeIngestAPIURL, {
            method: 'post',
            json: payload
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map