"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const api_1 = require("../api");
const common_fields_1 = require("../common-fields");
const email_address = {
    type: 'string',
    label: 'Email address',
    description: 'The email address of the person to link the user to',
    format: 'email',
    required: true,
    default: {
        '@if': {
            exists: { '@path': '$.traits.email' },
            then: { '@path': '$.traits.email' },
            else: { '@path': '$.email' }
        }
    }
};
const user_id = {
    type: 'string',
    label: 'ID',
    description: 'The ID of the User',
    format: 'text',
    required: true,
    default: { '@path': '$.userId' }
};
const user_attributes = {
    type: 'object',
    label: 'Additional User attributes',
    description: 'Additional attributes to either set or update on the Attio User Record. The values on the left should be ' +
        'Segment attributes or custom text, and the values on the right are Attio Attribute IDs or Slugs. ' +
        'For example: traits.name → name',
    defaultObjectUI: 'keyvalue:only',
    additionalProperties: true,
    properties: {},
    default: {}
};
const person_attributes = {
    type: 'object',
    label: 'Additional Person attributes',
    description: 'Additional attributes to either set or update on the Attio Person Record. The values on the left should be ' +
        'Segment attributes or custom text, and the values on the right are Attio Attribute IDs or Slugs. ' +
        'For example: traits.name → name',
    defaultObjectUI: 'keyvalue:only',
    default: {}
};
const action = {
    title: 'Identify User',
    description: 'Create or update an Attio User and link it to a Person based on a shared email address.',
    fields: {
        email_address,
        user_id,
        user_attributes,
        person_attributes,
        ...common_fields_1.commonFields
    },
    perform: async (request, { payload }) => {
        const client = new api_1.AttioClient(request);
        await client.assertRecord({
            object: 'people',
            matching_attribute: 'email_addresses',
            values: {
                email_addresses: payload.email_address,
                ...(payload.person_attributes ?? {})
            }
        });
        return await client.assertRecord({
            object: 'users',
            matching_attribute: 'user_id',
            values: {
                primary_email_address: payload.email_address,
                person: payload.email_address,
                user_id: payload.user_id,
                ...(payload.user_attributes ?? {})
            }
        });
    },
    performBatch: async (request, { payload }) => {
        const client = new api_1.AttioClient(request);
        return await client.batchAssert({
            assertions: payload.map((item) => ({
                object: 'users',
                mode: 'create-or-update',
                matching_attribute: 'user_id',
                multiselect_values: 'append',
                values: {
                    primary_email_address: item.email_address,
                    user_id: item.user_id,
                    ...(item.user_attributes ?? {}),
                    person: {
                        object: 'people',
                        mode: 'create-or-update',
                        matching_attribute: 'email_addresses',
                        multiselect_values: 'append',
                        values: {
                            email_addresses: item.email_address,
                            ...(item.person_attributes ?? {})
                        },
                        received_at: item.received_at?.toString() ?? new Date().toISOString()
                    }
                },
                received_at: item.received_at?.toString() ?? new Date().toISOString()
            }))
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map