"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ortto_client_1 = __importDefault(require("../ortto-client"));
const action = {
    title: 'Sync Audience',
    description: 'Sync contacts to Ortto',
    defaultSubscription: 'type = "identify" or type = "track"',
    fields: {
        segment_computation_action: {
            label: 'Segment Computation Action',
            description: "Hidden field used to verify that the payload is generated by an Audience. Payloads not containing computation_class = 'audience' or 'journey_step' will be dropped before the perform() fuction call.",
            type: 'string',
            unsafe_hidden: true,
            required: true,
            default: {
                '@path': '$.context.personas.computation_class'
            },
            choices: [
                { label: 'audience', value: 'audience' },
                { label: 'journey_step', value: 'journey_step' }
            ]
        },
        computation_key: {
            label: 'Audience Computation Key',
            description: "The name of a boolean trait/property in Segment that indicates a user's membership in a specific audience. This name matches Segment's friendly name for the audience exactly.",
            type: 'string',
            required: true,
            default: {
                '@path': '$.context.personas.computation_key'
            }
        },
        external_audience_id: {
            type: 'string',
            label: 'Ortto Audience ID',
            description: 'Unique Audience Identifier returned by the createAudience() function call.',
            required: true,
            unsafe_hidden: false,
            default: {
                '@path': '$.context.personas.external_audience_id'
            }
        },
        message_id: {
            label: 'Message ID',
            description: 'Message ID',
            type: 'string',
            readOnly: true,
            required: false,
            unsafe_hidden: true,
            default: {
                '@path': '$.messageId'
            }
        },
        enable_batching: {
            type: 'boolean',
            label: 'Batch data',
            description: 'When enabled, events will be sent to Ortto in batches for improved efficiency.',
            default: true
        },
        batch_size: {
            label: 'Batch Size',
            description: 'Maximum number of events to include in each batch.',
            type: 'number',
            required: false,
            default: 500,
            readOnly: true,
            unsafe_hidden: true
        },
        user_id: {
            label: 'User ID',
            description: 'The unique user identifier',
            type: 'string',
            required: {
                conditions: [
                    {
                        fieldKey: 'anonymous_id',
                        operator: 'is',
                        value: undefined
                    }
                ]
            },
            default: {
                '@path': '$.userId'
            }
        },
        anonymous_id: {
            label: 'Anonymous ID',
            description: 'Anonymous user identifier',
            type: 'string',
            required: {
                conditions: [
                    {
                        fieldKey: 'user_id',
                        operator: 'is',
                        value: undefined
                    }
                ]
            },
            default: {
                '@path': '$.anonymousId'
            }
        },
        ip: {
            label: 'IP Address',
            description: "The contact's IP address",
            placeholder: '180.1.12.125',
            type: 'string',
            format: 'ipv4',
            default: {
                '@if': {
                    exists: { '@path': '$.traits.ip' },
                    then: { '@path': '$.traits.ip' },
                    else: { '@path': '$.properties.ip' }
                }
            },
            allowNull: true
        },
        location: {
            label: 'Location',
            description: "The contact's location. Will take priority over the IP address.",
            type: 'object',
            defaultObjectUI: 'keyvalue:only',
            additionalProperties: false,
            allowNull: true,
            properties: {
                country: {
                    label: 'Country',
                    type: 'string',
                    allowNull: true
                },
                state: {
                    label: 'State',
                    type: 'string',
                    allowNull: true
                },
                city: {
                    label: 'City',
                    type: 'string',
                    allowNull: true
                },
                post_code: {
                    label: 'Postcode',
                    type: 'string',
                    allowNull: true
                }
            },
            default: {
                country: {
                    '@if': {
                        exists: { '@path': '$.traits.country' },
                        then: { '@path': '$.traits.country' },
                        else: { '@path': '$.properties.country' }
                    }
                },
                state: {
                    '@if': {
                        exists: { '@path': '$.traits.state' },
                        then: { '@path': '$.traits.state' },
                        else: { '@path': '$.properties.state' }
                    }
                },
                city: {
                    '@if': {
                        exists: { '@path': '$.traits.city' },
                        then: { '@path': '$.traits.city' },
                        else: { '@path': '$.properties.city' }
                    }
                },
                post_code: {
                    '@if': {
                        exists: { '@path': '$.traits.postal_code' },
                        then: { '@path': '$.traits.postal_code' },
                        else: { '@path': '$.properties.postal_code' }
                    }
                }
            }
        },
        traits: {
            label: 'Traits or properties object',
            description: 'A computed object for track and identify events. This field should not need to be edited.',
            type: 'object',
            required: true,
            unsafe_hidden: true,
            default: {
                '@if': {
                    exists: { '@path': '$.properties' },
                    then: { '@path': '$.properties' },
                    else: { '@path': '$.traits' }
                }
            }
        }
    },
    perform: async (request, { settings, payload }) => {
        const client = new ortto_client_1.default(request);
        return await client.syncAudience(settings, [payload]);
    },
    performBatch: async (request, { settings, payload }) => {
        const client = new ortto_client_1.default(request);
        return await client.syncAudience(settings, payload);
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map