"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
const fields_1 = require("../fields");
const action = {
    title: 'Upsert User Profile',
    description: 'Send user profile updates to Aampe.',
    defaultSubscription: 'type = "identify" or type = "track" or type = "page" or type = "screen"',
    fields: {
        contact_id: fields_1.contact_id,
        event_name: {
            ...fields_1.event_name,
            default: 'User Profile Updated'
        },
        timestamp: fields_1.timestamp,
        metadata: fields_1.metadata,
        event_id: fields_1.event_id,
        user_properties: fields_1.user_properties
    },
    perform: (request, { payload, settings }) => {
        const { contact_id, event_name, timestamp, metadata, event_id, user_properties } = payload;
        if (!user_properties || Object.keys(user_properties).length === 0) {
            throw new actions_core_1.PayloadValidationError('Upsert User Profile action requires at least one user property to be set');
        }
        const json = {
            contact_id,
            event_name,
            timestamp: new Date(timestamp).getTime() / 1000,
            metadata,
            event_id,
            user_properties
        };
        return request(`${settings.region}properties`, {
            method: 'post',
            json
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map