"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("../constants");
const utils_1 = require("./utils");
const action = {
    title: 'Update User Profile',
    description: 'Sends user events or creates and updates user profiles in Batch.',
    defaultSubscription: 'type = "identify" or type = "track"',
    fields: {
        identifiers: {
            label: 'Identifiers',
            description: 'User identifiers',
            type: 'object',
            required: true,
            additionalProperties: true,
            properties: {
                custom_id: {
                    label: 'User ID',
                    description: 'The unique profile identifier',
                    type: 'string',
                    required: true
                }
            },
            default: {
                custom_id: { '@path': '$.userId' }
            }
        },
        enable_batching: {
            label: 'Enable Batching',
            description: `If enabled, the action will send multiple profiles in a single request. The maximum number of profiles in a single request is ${constants_1.MAX_BATCH_SIZE}.`,
            type: 'boolean',
            default: true,
            unsafe_hidden: true
        },
        batch_size: {
            label: 'Batch Size',
            description: 'Maximum number of attributes to include in each batch.',
            type: 'number',
            default: constants_1.MAX_BATCH_SIZE,
            readOnly: true,
            unsafe_hidden: true
        },
        profileAttributes: {
            label: 'Profile attributes',
            description: `Attributes for the user profile. Batch can accept up to ${constants_1.MAX_ATTRIBUTES_SIZE} attributes per user.`,
            type: 'object',
            additionalProperties: true,
            properties: {
                language: {
                    label: 'Language',
                    description: "The profile's language. This can be sent as a locale (e.g., 'en-US') or a language code (e.g., 'en').",
                    type: 'string',
                    allowNull: true
                },
                email_address: {
                    label: 'Email',
                    description: "The profile's email",
                    type: 'string',
                    allowNull: true,
                    format: 'email'
                },
                phone_number: {
                    label: 'Phone Number',
                    description: "The profile's phone number",
                    type: 'string',
                    allowNull: true
                },
                email_marketing: {
                    label: 'Email marketing subscribe',
                    description: "The profile's marketing emails subscription. Setting to 'reset' will reset the marketing emails subscription.",
                    type: 'string',
                    choices: [
                        { label: 'Subscribed', value: 'subscribed' },
                        { label: 'Unsubscribed', value: 'unsubscribed' },
                        { label: 'Reset', value: 'reset' }
                    ]
                },
                sms_marketing: {
                    label: 'SMS marketing subscribe',
                    description: "The profile's marketing SMS subscription. Setting to 'reset' will reset the marketing SMS subscription.",
                    type: 'string',
                    allowNull: true,
                    choices: [
                        { label: 'Subscribed', value: 'subscribed' },
                        { label: 'Unsubscribed', value: 'unsubscribed' },
                        { label: 'Reset', value: 'reset' }
                    ]
                },
                timezone: {
                    label: 'Timezone',
                    description: "The profile's time zone name from IANA Time Zone Database (e.g., “Europe/Paris”). Only valid time zone values will be set.",
                    type: 'string',
                    allowNull: true
                },
                region: {
                    label: 'Region',
                    description: "The profile's region. This can be sent as a locale (e.g., 'en-US') or a country code (e.g., 'US').",
                    type: 'string',
                    allowNull: true
                }
            },
            default: {
                language: { '@path': '$.context.locale' },
                email_address: {
                    '@if': {
                        exists: { '@path': '$.context.traits.email' },
                        then: { '@path': '$.context.traits.email' },
                        else: { '@path': '$.traits.email' }
                    }
                },
                phone_number: {
                    '@if': {
                        exists: { '@path': '$.context.traits.phone' },
                        then: { '@path': '$.context.traits.phone' },
                        else: { '@path': '$.traits.phone' }
                    }
                },
                email_marketing: {
                    '@if': {
                        exists: { '@path': '$.context.traits.email_marketing' },
                        then: { '@path': '$.context.traits.email_marketing' },
                        else: { '@path': '$.traits.email_marketing' }
                    }
                },
                sms_marketing: {
                    '@if': {
                        exists: { '@path': '$.context.traits.sms_marketing' },
                        then: { '@path': '$.context.traits.sms_marketing' },
                        else: { '@path': '$.traits.sms_marketing' }
                    }
                },
                timezone: { '@path': '$.context.timezone' },
                region: { '@path': '$.context.locale' }
            }
        },
        eventName: {
            label: 'Event Name',
            description: 'The name of the event.',
            type: 'string',
            default: {
                '@path': '$.event'
            }
        },
        eventAttributes: {
            label: 'Event Attributes',
            description: "An object containining the event's attributes",
            type: 'object',
            default: {
                '@path': '$.properties'
            },
            additionalProperties: true
        }
    },
    perform: (request, { payload }) => {
        return send(request, [payload]);
    },
    performBatch: (request, { payload }) => {
        return send(request, payload);
    }
};
async function send(request, payload) {
    const json = payload.map(utils_1.mapPayload);
    const response = await request(`https://api.batch.com/${constants_1.API_VERSION}/profiles/update`, {
        method: 'post',
        json
    });
    return (0, utils_1.handleMultiStatusResponse)(response.data, payload, json);
}
exports.default = action;
//# sourceMappingURL=index.js.map