"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../utils");
const convertPayloadToLDEvent = (payload) => {
    return {
        kind: 'alias',
        key: payload.user_key,
        previousKey: payload.previous_key,
        contextKind: 'user',
        previousContextKind: 'anonymousUser',
        creationDate: (0, utils_1.parseTimestamp)(payload.timestamp)
    };
};
const action = {
    title: 'Alias User',
    description: 'Alias an anonymous user with an identified user key.',
    defaultSubscription: 'type = "identify" or type = "alias"',
    fields: {
        user_key: {
            label: 'User key',
            type: 'string',
            required: true,
            description: "The user's unique key.",
            default: {
                '@path': '$.userId'
            }
        },
        previous_key: {
            label: 'Anonymous ID',
            type: 'string',
            required: true,
            description: "The user's unauthenticated identifier.",
            default: {
                '@if': {
                    exists: { '@path': '$.previousId' },
                    then: { '@path': '$.previousId' },
                    else: { '@path': '$.anonymousId' }
                }
            }
        },
        timestamp: {
            label: 'Event Timestamp',
            description: 'The time when the event happened. Defaults to the current time.',
            type: 'datetime',
            default: {
                '@path': '$.timestamp'
            }
        }
    },
    perform: (request, { payload, settings }) => {
        const event = convertPayloadToLDEvent(payload);
        return request((0, utils_1.getEventsUrl)(settings), {
            method: 'post',
            json: [event]
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map