"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../utils");
const action = {
    title: 'Create Rev User',
    defaultSubscription: 'type = "identify"',
    description: 'Creates a RevUser, unless one already exists with the same email address. Posts the comment to the RevUser object if it is created or if it already exists.',
    fields: {
        email: {
            label: 'Email',
            description: "The user's email address.",
            type: 'string',
            required: true,
            default: {
                '@if': {
                    exists: { '@path': '$.traits.email' },
                    then: { '@path': '$.traits.email' },
                    else: { '@path': '$.properties.email' }
                }
            }
        },
        fullName: {
            label: 'Full Name',
            description: "The user's full name.",
            type: 'string',
            required: false,
            default: {
                '@if': {
                    exists: { '@path': '$.traits.name' },
                    then: { '@path': '$.traits.name' },
                    else: { '@path': '$.properties.name' }
                }
            }
        },
        firstName: {
            label: 'First Name',
            description: "The user's first name.",
            type: 'string',
            required: false,
            default: {
                '@if': {
                    exists: { '@path': '$.traits.first_name' },
                    then: { '@path': '$.traits.first_name' },
                    else: { '@path': '$.properties.first_name' }
                }
            }
        },
        lastName: {
            label: 'Last Name',
            description: "The user's last name.",
            type: 'string',
            required: false,
            default: {
                '@if': {
                    exists: { '@path': '$.traits.last_name' },
                    then: { '@path': '$.traits.last_name' },
                    else: { '@path': '$.properties.last_name' }
                }
            }
        },
        comment: {
            label: 'Comment',
            description: 'A comment to post to the RevUser. If empty, no comment will be posted on the RevUser, otherwise the comment will be posted to the RevUser. The comment will be posted even if the RevUser already exists on the RevDev platform',
            type: 'string',
            required: false
        },
        tag: {
            label: 'Tag',
            description: 'A tag to apply.',
            type: 'string',
            dynamic: true,
            required: false
        }
    },
    dynamicFields: {
        tag: async (request, { settings }) => {
            try {
                const result = await request(`${(0, utils_1.getBaseUrl)(settings)}${utils_1.devrevApiPaths.tagsList}`, {
                    method: 'get',
                    skipResponseCloning: true
                });
                const choices = result.data.tags.map((tag) => {
                    return { value: tag.id, label: tag.name };
                });
                return {
                    choices,
                    nextPage: result.data.next_cursor
                };
            }
            catch (err) {
                return {
                    choices: [],
                    nextPage: '',
                    error: {
                        message: err.message ?? 'Unknown error',
                        code: err.status + '' ?? 'Unknown error'
                    }
                };
            }
        }
    },
    perform: async (request, { settings, payload }) => {
        const email = payload.email.toLowerCase();
        const domain = (0, utils_1.getDomain)(settings, email);
        const name = (0, utils_1.getName)(payload);
        const existingUsers = await request(`${(0, utils_1.getBaseUrl)(settings)}${utils_1.devrevApiPaths.revUsersList}?email="${email}"`);
        let contact = undefined;
        if (existingUsers.data.rev_users.length == 0) {
            const createUserPayload = {
                email,
                display_name: name,
                external_ref: email
            };
            let requestUrl = '';
            if (domain !== email)
                requestUrl = `${(0, utils_1.getBaseUrl)(settings)}${utils_1.devrevApiPaths.accountsList}?domains="${domain}"`;
            else
                requestUrl = `${(0, utils_1.getBaseUrl)(settings)}${utils_1.devrevApiPaths.accountsList}?external_refs="${domain}"`;
            const existingAccount = await request(requestUrl);
            let accountId = '';
            if (existingAccount.data.accounts.length > 0) {
                accountId = existingAccount.data.accounts[0].id;
            }
            else if (domain !== email) {
                const createAccountPayload = {
                    display_name: domain,
                    external_refs: [domain],
                    domains: [domain]
                };
                if (payload.tag)
                    createAccountPayload.tags = [{ id: payload.tag }];
                const createAccount = await request(`${(0, utils_1.getBaseUrl)(settings)}${utils_1.devrevApiPaths.accountCreate}`, {
                    method: 'POST',
                    json: createAccountPayload
                });
                accountId = createAccount.data.account.id;
            }
            if (accountId) {
                const accountRevorgs = await request(`${(0, utils_1.getBaseUrl)(settings)}${utils_1.devrevApiPaths.revOrgsList}?account=${accountId}`);
                const filtered = accountRevorgs.data.rev_orgs.filter((revorg) => revorg.external_ref_issuer == 'devrev:platform:revorg:account');
                if (filtered.length > 0)
                    createUserPayload.org_id = filtered[0].id;
            }
            if (payload.tag)
                createUserPayload.tags = [{ id: payload.tag }];
            const createRevUser = await request(`${(0, utils_1.getBaseUrl)(settings)}${utils_1.devrevApiPaths.revUsersCreate}`, {
                method: 'post',
                json: createUserPayload
            });
            contact = createRevUser.data.rev_user;
        }
        else if (existingUsers.data.rev_users.length == 1) {
            contact = existingUsers.data.rev_users[0];
        }
        else {
            const sorted = existingUsers.data.rev_users.sort((a, b) => {
                return new Date(a.created_date).getTime() - new Date(b.created_date).getTime();
            });
            contact = sorted[0];
        }
        if (payload.tag && contact) {
            const tags = contact.tags?.map((tag) => tag.tag.id) ?? [];
            if (tags.indexOf(payload.tag) === -1) {
                tags.push(payload.tag);
                const formattedTags = tags.map((tag) => ({ id: tag }));
                await request(`${(0, utils_1.getBaseUrl)(settings)}${utils_1.devrevApiPaths.revUsersUpdate}`, {
                    method: 'post',
                    json: {
                        id: contact.id,
                        tags: formattedTags
                    }
                });
            }
        }
        if (payload.comment && contact) {
            await request(`${(0, utils_1.getBaseUrl)(settings)}${utils_1.devrevApiPaths.timelineEntriesCreate}`, {
                method: 'post',
                json: {
                    object: contact.id,
                    type: 'timeline_comment',
                    body_type: 'text',
                    body: payload.comment
                }
            });
        }
        return;
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map