"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLead = 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 axios_1 = require("axios");
const addEventLog_1 = require("../../../../src/lib/awell/addEventLog");
exports.getLead = {
    key: 'getLead',
    category: extensions_core_1.Category.CUSTOMER_SUPPORT,
    title: 'Get lead',
    description: 'Get a lead from Freshsales.',
    fields: config_1.fields,
    previewable: false,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError }) => {
        var _a;
        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;
            }
        };
        try {
            const { data } = await freshsalesSdk.getLead(fields.leadId);
            await onComplete({
                data_points: {
                    leadData: JSON.stringify(data),
                    email: (0, lodash_1.isNil)(data.lead.email) ? undefined : data.lead.email,
                    mobileNumber: getMobileNumberInE164(data.lead.mobile_number),
                    address: (0, lodash_1.isNil)(data.lead.address) ? undefined : data.lead.address,
                    city: (0, lodash_1.isNil)(data.lead.city) ? undefined : data.lead.city,
                    state: (0, lodash_1.isNil)(data.lead.state) ? undefined : data.lead.state,
                    zipcode: (0, lodash_1.isNil)(data.lead.zipcode) ? undefined : data.lead.zipcode,
                    country: (0, lodash_1.isNil)(data.lead.country) ? undefined : data.lead.country,
                    timeZone: (0, lodash_1.isNil)(data.lead.time_zone)
                        ? undefined
                        : data.lead.time_zone,
                    displayName: (0, lodash_1.isNil)(data.lead.display_name)
                        ? undefined
                        : data.lead.display_name,
                    firstName: (0, lodash_1.isNil)(data.lead.first_name)
                        ? undefined
                        : data.lead.first_name,
                    lastName: (0, lodash_1.isNil)(data.lead.last_name)
                        ? undefined
                        : data.lead.last_name,
                },
                events: additionalLogs,
            });
        }
        catch (error) {
            // Some errors we want to handle explicitly for more human-readable logging
            if (error instanceof axios_1.AxiosError) {
                const err = error;
                if (((_a = err.response) === null || _a === void 0 ? void 0 : _a.status) === 404)
                    await onError({
                        events: [
                            (0, addEventLog_1.addActivityEventLog)({
                                message: 'Lead not found (404)',
                            }),
                        ],
                    });
                return;
            }
            // Throw all other errors
            throw error;
        }
    },
};
//# sourceMappingURL=getLead.js.map