"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
const flat_1 = require("../flat");
const heapUtils_1 = require("../heapUtils");
const action = {
    title: 'Identify User',
    description: 'Set the user ID for a particular device ID or update user properties.',
    defaultSubscription: 'type = "identify"',
    fields: {
        user_id: {
            label: 'Identity',
            type: 'string',
            allowNull: true,
            description: 'REQUIRED: A string that uniquely identifies a user, such as an email, handle, or username. This means no two users in one environment may share the same identity. More on identify: https://developers.heap.io/docs/using-identify',
            default: {
                '@path': '$.userId'
            }
        },
        anonymous_id: {
            label: 'Anonymous ID',
            type: 'string',
            allowNull: true,
            description: 'The generated anonymous ID for the user.',
            default: {
                '@path': '$.anonymousId'
            }
        },
        traits: {
            label: 'User Properties',
            type: 'object',
            description: 'An object with key-value properties you want associated with the user. Each key and property must either be a number or string with fewer than 1024 characters.',
            default: {
                '@path': '$.traits'
            }
        }
    },
    perform: async (request, { payload, settings }) => {
        if (!payload.user_id || !(0, heapUtils_1.isDefined)(payload.user_id)) {
            throw new actions_core_1.IntegrationError('Missing identity, cannot add user properties without identity', 'Missing required field', 400);
        }
        const addUserPropertiesPayload = {
            identity: payload.user_id,
            app_id: settings.appId,
            properties: {
                ...(payload.anonymous_id && { anonymous_id: payload.anonymous_id })
            }
        };
        if (payload.traits && Object.keys(payload.traits).length > 0) {
            const flatten = (0, flat_1.flat)(payload.traits);
            addUserPropertiesPayload.properties = {
                ...addUserPropertiesPayload.properties,
                ...flatten
            };
        }
        return request('https://heapanalytics.com/api/add_user_properties', {
            method: 'post',
            json: addUserPropertiesPayload
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map