"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.searchContactByEmail = 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.searchContactByEmail = {
    key: 'searchContactByEmail',
    category: extensions_core_1.Category.CUSTOMER_SUPPORT,
    title: 'Search contact by email',
    description: 'Search a contact 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.searchContactByEmail(fields.email);
        if (data.meta.total === 0) {
            await onError({
                events: [
                    (0, addEventLog_1.addActivityEventLog)({
                        message: 'Contact not found',
                    }),
                ],
            });
            return;
        }
        if (data.meta.total > 1) {
            await onError({
                events: [
                    (0, addEventLog_1.addActivityEventLog)({
                        message: 'Multiple contacts found for the given email',
                    }),
                ],
            });
            return;
        }
        const contact = data.contacts[0];
        await onComplete({
            data_points: {
                contactData: JSON.stringify(contact),
                email: (0, lodash_1.isNil)(contact.email) ? undefined : contact.email,
                mobileNumber: getMobileNumberInE164(contact.mobile_number),
                city: (0, lodash_1.isNil)(contact.city) ? undefined : contact.city,
                country: (0, lodash_1.isNil)(contact.country) ? undefined : contact.country,
                displayName: (0, lodash_1.isNil)(contact.display_name)
                    ? undefined
                    : contact.display_name,
                firstName: (0, lodash_1.isNil)(contact.first_name) ? undefined : contact.first_name,
                lastName: (0, lodash_1.isNil)(contact.last_name) ? undefined : contact.last_name,
            },
            events: additionalLogs,
        });
    },
};
//# sourceMappingURL=searchContactByEmail.js.map