"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getContact = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const validatePayloadAndCreateSdk_1 = require("../../lib/validatePayloadAndCreateSdk");
const config_1 = require("./config");
const axios_1 = require("axios");
const addEventLog_1 = require("../../../../src/lib/awell/addEventLog");
const lodash_1 = require("lodash");
exports.getContact = {
    key: 'getContact',
    category: extensions_core_1.Category.CUSTOMER_SUPPORT,
    title: 'Get contact',
    description: 'Get a contact from Freshdesk.',
    fields: config_1.fields,
    previewable: true,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        var _a, _b, _c;
        const { fields, freshdeskSdk } = await (0, validatePayloadAndCreateSdk_1.validatePayloadAndCreateSdk)({
            fieldsSchema: config_1.FieldsValidationSchema,
            payload,
        });
        try {
            const { data } = await freshdeskSdk.getContact(fields.contactId);
            helpers.log({ data }, 'data');
            await onComplete({
                data_points: {
                    contactData: JSON.stringify(data),
                    name: data.name,
                    email: (0, lodash_1.isNil)(data.email) ? undefined : data.email,
                    customFields: (0, lodash_1.isNil)(data.custom_fields)
                        ? undefined
                        : JSON.stringify(data.custom_fields),
                    tags: (0, lodash_1.isNil)(data.tags) ? undefined : JSON.stringify(data.tags),
                },
            });
        }
        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: 'Contact not found (404)',
                            }),
                        ],
                    });
                    return;
                }
                if (((_b = err.response) === null || _b === void 0 ? void 0 : _b.status) === 400) {
                    await onError({
                        events: [
                            (0, addEventLog_1.addActivityEventLog)({
                                message: `Bad request (400): ${JSON.stringify((_c = err.response) === null || _c === void 0 ? void 0 : _c.data, null, 2)}`,
                            }),
                        ],
                    });
                    return;
                }
                // Throw other Axios errors
                throw error;
            }
            // Throw all other errors
            throw error;
        }
    },
};
//# sourceMappingURL=getContact.js.map