"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
const dynamic_functions_1 = require("../dynamic-functions");
const constants_1 = require("../constants");
const action = {
    title: 'Upsert Lead',
    description: 'Send a Lead to Qualified, or update an existing Lead',
    defaultSubscription: 'type = "track" and event = "Lead Updated"',
    fields: {
        email: {
            label: 'Email',
            description: 'The email address of the lead to upsert.',
            type: 'string',
            format: 'email',
            required: true,
            default: {
                '@if': {
                    exists: { '@path': '$.context.traits.email' },
                    then: { '@path': '$.context.traits.email' },
                    else: { '@path': '$.properties.email' }
                }
            }
        },
        phone: {
            label: 'Phone',
            description: 'The phone number of the lead to upsert.',
            type: 'string',
            required: false,
            default: {
                '@if': {
                    exists: { '@path': '$.context.traits.phone' },
                    then: { '@path': '$.context.traits.phone' },
                    else: { '@path': '$.properties.phone' }
                }
            }
        },
        company: {
            label: 'Company',
            description: 'The company name of the lead to upsert.',
            type: 'string',
            required: false,
            default: {
                '@if': {
                    exists: { '@path': '$.context.traits.company' },
                    then: { '@path': '$.context.traits.company' },
                    else: { '@path': '$.properties.company' }
                }
            }
        },
        name: {
            label: 'Name',
            description: 'The name of the lead to upsert.',
            type: 'string',
            required: false,
            default: {
                '@if': {
                    exists: { '@path': '$.context.traits.name' },
                    then: { '@path': '$.context.traits.name' },
                    else: { '@path': '$.properties.name' }
                }
            }
        },
        string_fields: {
            label: 'String Fields',
            description: 'Additional string, text, picklist fields to set on the Lead.',
            type: 'object',
            defaultObjectUI: 'keyvalue',
            dynamic: true
        },
        boolean_fields: {
            label: 'Boolean Fields',
            description: 'Additional boolean / checkbox fields to set on the Lead.',
            type: 'object',
            defaultObjectUI: 'keyvalue',
            dynamic: true
        },
        number_fields: {
            label: 'Number Fields',
            description: 'Additional numeric / decimal fields to set on the Lead.',
            type: 'object',
            defaultObjectUI: 'keyvalue',
            dynamic: true
        }
    },
    dynamicFields: {
        string_fields: {
            __keys__: async (request) => {
                return await (0, dynamic_functions_1.dynamicReadFields)(request, ['string', 'picklist', 'text']);
            }
        },
        boolean_fields: {
            __keys__: async (request) => {
                return await (0, dynamic_functions_1.dynamicReadFields)(request, ['boolean']);
            }
        },
        number_fields: {
            __keys__: async (request) => {
                return await (0, dynamic_functions_1.dynamicReadFields)(request, ['decimal']);
            }
        }
    },
    perform: async (request, { payload }) => {
        const { email, phone, company, name, string_fields, boolean_fields, number_fields } = payload;
        Object.entries(string_fields || {}).forEach(([key, value]) => {
            if (typeof value !== 'string') {
                throw new actions_core_1.PayloadValidationError(`Invalid field value for: ${key}. Should be a string.`);
            }
        });
        Object.entries(boolean_fields || {}).forEach(([key, value]) => {
            if (typeof value !== 'boolean') {
                throw new actions_core_1.PayloadValidationError(`Invalid field value for: ${key}. Should be a boolean.`);
            }
        });
        Object.entries(number_fields || {}).forEach(([key, value]) => {
            if (typeof value !== 'number') {
                throw new actions_core_1.PayloadValidationError(`Invalid field value for: ${key}. Should be a number.`);
            }
        });
        const json = {
            email,
            fields: {
                ...(phone || phone === '' ? { phone } : {}),
                ...(company || company === '' ? { company } : {}),
                ...(name || name === '' ? { name } : {}),
                ...string_fields,
                ...boolean_fields,
                ...number_fields
            }
        };
        return request(constants_1.base_url + 'leads', {
            method: 'post',
            json
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map