"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.objectLookup = void 0;
const api_1 = require("../api");
const common_fields_1 = require("../common-fields");
const object = {
    type: 'string',
    label: 'Attio Object',
    description: "The type of Attio Object you'd like to create or update ('assert')",
    dynamic: true,
    required: true,
    default: 'person'
};
const matching_attribute = {
    type: 'string',
    label: 'Matching Attribute',
    description: 'The Attribute (ID or slug) on the Attio Object above, that uniquely identifies a Record (and is marked as unique in Attio). ' +
        'Events containing the same value for this attribute will update the original Record, rather than creating a new one. ' +
        'For example, to create or update a Person you might use the Attio attribute `email_addresses` here.',
    required: true,
    default: 'email_addresses'
};
const attributes = {
    type: 'object',
    label: 'Attributes',
    description: 'Attributes to either set or update on the Attio 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. ' +
        'The Matching Attribute must be included for assertion to work.',
    defaultObjectUI: 'keyvalue:only',
    additionalProperties: true,
    properties: {},
    default: {}
};
const objectLookup = async (request) => {
    try {
        const objects = await new api_1.AttioClient(request).listObjects();
        return {
            choices: objects.map(({ api_slug, singular_noun }) => ({
                value: api_slug,
                label: singular_noun
            }))
        };
    }
    catch (error) {
        const response_data = error?.response?.data ?? {};
        return {
            choices: [],
            error: {
                code: response_data.status_code ? String(response_data.status_code) : 'unknown',
                message: response_data.message ?? error?.message ?? 'Unknown error'
            }
        };
    }
};
exports.objectLookup = objectLookup;
const action = {
    title: 'Assert Record',
    description: 'Create or update a Record in Attio.',
    fields: {
        object,
        matching_attribute,
        attributes,
        ...common_fields_1.commonFields
    },
    dynamicFields: {
        object: exports.objectLookup
    },
    perform: async (request, { payload }) => {
        const client = new api_1.AttioClient(request);
        return await client.assertRecord({
            object: payload.object,
            matching_attribute: payload.matching_attribute,
            values: payload.attributes ?? {}
        });
    },
    performBatch: async (request, { payload }) => {
        const client = new api_1.AttioClient(request);
        return await client.batchAssert({
            assertions: payload.map((item) => ({
                object: item.object,
                mode: 'create-or-update',
                matching_attribute: item.matching_attribute,
                multiselect_values: 'append',
                values: item.attributes ?? {},
                received_at: item.received_at?.toString() ?? new Date().toISOString()
            }))
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map