"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const omit_1 = __importDefault(require("lodash/omit"));
const pick_1 = __importDefault(require("lodash/pick"));
const utils_1 = require("../utils");
const common_definitions_1 = require("../common-definitions");
const action = {
    title: 'Identify User',
    description: 'Identify a user in Encharge. This will create a new user if the user does not exist in Encharge.',
    fields: {
        user: {
            type: 'object',
            label: 'User Fields',
            description: 'Fields to associate with the user in Encharge. Any unexisting fields will be automatically created in Encharge.',
            required: false,
            default: { '@path': '$.traits' }
        },
        email: {
            type: 'string',
            required: false,
            description: 'The email address of the user.',
            label: 'Email',
            default: {
                '@if': {
                    exists: { '@path': '$.traits.email' },
                    then: { '@path': '$.traits.email' },
                    else: { '@path': '$.context.traits.email' }
                }
            }
        },
        ...(0, omit_1.default)(common_definitions_1.commonFields, 'email')
    },
    defaultSubscription: 'type = "identify"',
    perform: (request, data) => {
        const payload = {
            ...(0, omit_1.default)(data.payload, ['ip', 'userAgent', 'campaign', 'page', 'location', 'user', 'groupId']),
            context: (0, pick_1.default)(data.payload, ['ip', 'userAgent', 'campaign', 'page', 'location', 'groupId']),
            user: {
                email: data.payload.email,
                userId: data.payload.userId,
                segmentAnonymousId: data.payload.segmentAnonymousId,
                ...(data.payload.user || {})
            }
        };
        return request(utils_1.enchargeIngestAPIURL, {
            method: 'post',
            json: payload
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map