"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../utils");
const customerio_properties_1 = require("../customerio-properties");
const action = {
    title: 'Create or Update Person',
    description: 'Create a person in Customer.io or update them if they exist.',
    defaultSubscription: 'type = "identify"',
    fields: {
        id: {
            label: 'Person ID',
            description: 'The ID used to uniquely identify a person in Customer.io. [Learn more](https://customer.io/docs/identifying-people/#identifiers).',
            type: 'string',
            default: {
                '@if': {
                    exists: { '@path': '$.userId' },
                    then: { '@path': '$.userId' },
                    else: { '@path': '$.traits.email' }
                }
            }
        },
        anonymous_id: {
            label: 'Anonymous ID',
            description: 'An optional anonymous ID. This is used to tie anonymous events to this person. [Learn more](https://customer.io/docs/anonymous-events/).',
            type: 'string',
            default: {
                '@path': '$.anonymousId'
            }
        },
        email: {
            label: 'Email Address',
            description: "The person's email address.",
            type: 'string',
            default: {
                '@template': '{{traits.email}}'
            }
        },
        created_at: {
            label: 'Created At',
            description: 'A timestamp of when the person was created.',
            type: 'string',
            default: {
                '@if': {
                    exists: { '@path': '$.traits.created_at' },
                    then: { '@path': '$.traits.created_at' },
                    else: { '@path': '$.traits.createdAt' }
                }
            }
        },
        group_id: {
            label: 'Object ID',
            description: 'The ID used to uniquely identify an object in Customer.io. [Learn more](https://customer.io/docs/object-relationships).',
            type: 'string',
            default: {
                '@path': '$.traits.objectId'
            }
        },
        custom_attributes: {
            label: 'Person Attributes',
            description: 'Optional attributes for the person. When updating a person, attributes are added or updated, not removed.',
            type: 'object',
            default: {
                '@path': '$.traits'
            }
        },
        relationship_attributes: {
            label: 'Relationship Attributes',
            description: 'Optional attributes for the relationship between the object and the user. When updating an object, attributes are added or updated, not removed.',
            type: 'object',
            default: {
                '@path': '$.traits.relationshipAttributes'
            }
        },
        convert_timestamp: {
            label: 'Convert Timestamps',
            description: 'Convert dates to Unix timestamps (seconds since Epoch).',
            type: 'boolean',
            default: true
        },
        object_type_id: {
            label: 'Object Type Id',
            description: 'The ID used to uniquely identify a custom object type in Customer.io. [Learn more](https://customer.io/docs/object-relationships).',
            type: 'string',
            default: {
                '@if': {
                    exists: { '@path': '$.traits.object_type_id' },
                    then: { '@path': '$.traits.object_type_id' },
                    else: { '@path': '$.traits.objectTypeId' }
                }
            }
        },
        ...customerio_properties_1.eventProperties
    },
    performBatch: (request, { payload: payloads, settings }) => {
        return (0, utils_1.sendBatch)(request, payloads.map((payload) => ({ action: 'identify', payload: mapPayload(payload), settings, type: 'person' })));
    },
    perform: (request, { payload, settings }) => {
        return (0, utils_1.sendSingle)(request, { action: 'identify', payload: mapPayload(payload), settings, type: 'person' });
    }
};
function mapPayload(payload) {
    const { id, custom_attributes = {}, relationship_attributes, created_at, group_id, object_type_id, ...rest } = payload;
    delete custom_attributes.createdAt;
    delete custom_attributes.created_at;
    delete custom_attributes?.object_type_id;
    delete custom_attributes?.relationshipAttributes;
    delete custom_attributes?.objectTypeId;
    if (created_at) {
        custom_attributes.created_at = created_at;
    }
    if (payload.email) {
        custom_attributes.email = payload.email;
    }
    const body = {
        ...rest,
        person_id: id,
        attributes: custom_attributes
    };
    if (group_id) {
        const relationship = {
            identifiers: { object_type_id: object_type_id ?? '1', object_id: group_id }
        };
        if (relationship_attributes) {
            relationship.relationship_attributes = relationship_attributes;
        }
        body.cio_relationships = [relationship];
    }
    return body;
}
exports.default = action;
//# sourceMappingURL=index.js.map