"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
const constants_1 = require("../utils/constants");
const helpers_1 = require("../utils/helpers");
const dayjs_1 = __importDefault(require("../../../lib/dayjs"));
const action = {
    title: 'Identify User',
    description: 'Set the user ID to recognise other evens by the same user',
    defaultSubscription: 'type = "identify"',
    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'
            }
        },
        user_id: {
            label: 'User ID',
            type: 'string',
            allowNull: true,
            description: 'A distinct ID of an identified (logged in) user.',
            default: {
                '@path': '$.userId'
            }
        },
        anonymous_id: {
            label: 'Anonymous ID',
            type: 'string',
            allowNull: true,
            description: 'A distinct ID of an unidentified (logged out) user. Device id is used if available',
            default: {
                '@path': '$.anonymousId'
            }
        },
        first_name: {
            description: "User's first name",
            label: 'First Name',
            type: 'string',
            required: false,
            default: {
                '@if': {
                    exists: { '@path': '$.traits.first_name' },
                    then: { '@path': '$.traits.first_name' },
                    else: { '@path': '$.traits.firstName' }
                }
            }
        },
        last_name: {
            description: "User's last name",
            label: 'Last Name',
            type: 'string',
            required: false,
            default: {
                '@if': {
                    exists: { '@path': '$.traits.last_name' },
                    then: { '@path': '$.traits.last_name' },
                    else: { '@path': '$.traits.lastName' }
                }
            }
        },
        name: {
            description: "User's full name",
            label: 'Name',
            type: 'string',
            required: false,
            default: {
                '@path': '$.traits.name'
            }
        },
        email: {
            description: "User's email address",
            label: 'Email',
            type: 'string',
            required: false,
            default: {
                '@path': '$.traits.email'
            }
        },
        phone: {
            description: "User's phone number",
            label: 'Phone',
            type: 'string',
            required: false,
            default: {
                '@path': '$.traits.phone'
            }
        },
        created_at: {
            description: 'The timestamp for when the user profile was first created',
            label: 'User Creation Time',
            type: 'datetime',
            required: false,
            default: {
                '@if': {
                    exists: { '@path': '$.traits.createdAt' },
                    then: { '@path': '$.traits.createdAt' },
                    else: { '@path': '$.traits.created_at' }
                }
            }
        },
        traits: {
            label: 'User Properties',
            type: 'object',
            description: 'Properties to set on the user profile',
            default: {
                '@path': '$.traits'
            }
        }
    },
    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();
        const name = (0, helpers_1.formatName)(payload.first_name, payload.last_name, payload.name);
        return await request(`${constants_1.API_URL}/user/identify`, {
            method: 'post',
            json: {
                companyId: settings.companyId,
                key: settings.segmentKey,
                user: {
                    time,
                    user_id: payload.user_id,
                    anonymous_id: payload.anonymous_id,
                    created_at: payload.created_at,
                    email: payload.email,
                    first_name: payload.first_name,
                    last_name: payload.last_name,
                    name,
                    phone: payload.phone,
                    traits: (0, actions_core_1.omit)(payload.traits, [
                        'created',
                        'email',
                        'firstName',
                        'first_name',
                        'lastName',
                        'last_name',
                        'name',
                        'username',
                        'phone'
                    ])
                }
            }
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map