"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("../utils/constants");
const dayjs_1 = __importDefault(require("../../../lib/dayjs"));
const action = {
    title: 'Alias',
    description: 'Create an alias to a user id. Used when a single user has been initially identified as several users.',
    fields: {
        time: {
            label: 'Timestamp',
            type: 'datetime',
            description: 'The timestamp of the event. Could be any date string/number value compatible with JavaScript Date constructor: e.g. milliseconds epoch or an ISO datetime. If time is not sent with the event, it will be set to the request time.',
            default: {
                '@path': '$.timestamp'
            }
        },
        new_id: {
            label: 'New ID',
            type: 'string',
            allowNull: true,
            description: 'A new user id to be merged with the original distinct id. Each alias can only map to one distinct id.',
            default: {
                '@if': {
                    exists: { '@path': '$.userId' },
                    then: { '@path': '$.userId' },
                    else: { '@path': '$.anonymousId' }
                }
            }
        },
        previous_id: {
            label: 'Previous ID',
            type: 'string',
            description: 'A previous user id to be merged with the alias.',
            default: {
                '@path': '$.previousId'
            }
        }
    },
    perform: async (request, { payload, settings }) => {
        const datetime = payload.time;
        const time = datetime && dayjs_1.default.utc(datetime).isValid() ? dayjs_1.default.utc(datetime).valueOf() : Date.now();
        return await request(`${constants_1.API_URL}/user/alias`, {
            method: 'post',
            json: {
                companyId: settings.companyId,
                key: settings.segmentKey,
                alias: {
                    time,
                    new_id: payload.new_id,
                    previous_id: payload.previous_id
                }
            }
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map