"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.searchLeadByEmail = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const validatePayloadAndCreateSdk_1 = require("../../lib/validatePayloadAndCreateSdk");
const config_1 = require("./config");
const lodash_1 = require("lodash");
const freshsalesMobilePhoneToE164_1 = require("../../lib/freshsalesMobilePhoneToE164/freshsalesMobilePhoneToE164");
const addEventLog_1 = require("../../../../src/lib/awell/addEventLog");
exports.searchLeadByEmail = {
    key: 'searchLeadByEmail',
    category: extensions_core_1.Category.CUSTOMER_SUPPORT,
    title: 'Search lead by email',
    description: 'Search a lead by email from Freshsales.',
    fields: config_1.fields,
    previewable: false,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError }) => {
        const { fields, freshsalesSdk } = await (0, validatePayloadAndCreateSdk_1.validatePayloadAndCreateSdk)({
            fieldsSchema: config_1.FieldsValidationSchema,
            payload,
        });
        const additionalLogs = [];
        const getMobileNumberInE164 = (mobileNumber) => {
            try {
                return (0, freshsalesMobilePhoneToE164_1.freshsalesMobilePhoneToE164)(mobileNumber);
            }
            catch (error) {
                additionalLogs.push((0, addEventLog_1.addActivityEventLog)({
                    message: "Could not convert the lead's phone number to the E164 format. It will not be set in the data point.",
                }));
                return undefined;
            }
        };
        const { data } = await freshsalesSdk.searchLeadByEmail(fields.email);
        if (data.meta.total === 0) {
            await onError({
                events: [
                    (0, addEventLog_1.addActivityEventLog)({
                        message: 'Lead not found',
                    }),
                ],
            });
            return;
        }
        if (data.meta.total > 1) {
            await onError({
                events: [
                    (0, addEventLog_1.addActivityEventLog)({
                        message: 'Multiple leads found for the given email',
                    }),
                ],
            });
            return;
        }
        const lead = data.leads[0];
        await onComplete({
            data_points: {
                leadData: JSON.stringify(lead),
                email: (0, lodash_1.isNil)(lead.email) ? undefined : lead.email,
                mobileNumber: getMobileNumberInE164(lead.mobile_number),
                city: (0, lodash_1.isNil)(lead.city) ? undefined : lead.city,
                country: (0, lodash_1.isNil)(lead.country) ? undefined : lead.country,
                displayName: (0, lodash_1.isNil)(lead.display_name) ? undefined : lead.display_name,
                firstName: (0, lodash_1.isNil)(lead.first_name) ? undefined : lead.first_name,
                lastName: (0, lodash_1.isNil)(lead.last_name) ? undefined : lead.last_name,
            },
            events: additionalLogs,
        });
    },
};
//# sourceMappingURL=searchLeadByEmail.js.map