"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTicket = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const validatePayloadAndCreateSdk_1 = require("../../lib/validatePayloadAndCreateSdk");
const config_1 = require("./config");
const atoms_1 = require("../../lib/api/schema/atoms");
const axios_1 = require("axios");
const addEventLog_1 = require("../../../../src/lib/awell/addEventLog");
exports.getTicket = {
    key: 'getTicket',
    category: extensions_core_1.Category.CUSTOMER_SUPPORT,
    title: 'Get ticket',
    description: 'Get a ticket from Freshdesk.',
    fields: config_1.fields,
    previewable: false,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError }) => {
        var _a, _b, _c, _d;
        const { fields, freshdeskSdk } = await (0, validatePayloadAndCreateSdk_1.validatePayloadAndCreateSdk)({
            fieldsSchema: config_1.FieldsValidationSchema,
            payload,
        });
        try {
            const { data } = await freshdeskSdk.getTicket(fields.ticketId);
            const priorityLabel = (_a = Object.entries(atoms_1.TicketPriority).find(([, value]) => value === data.priority)) === null || _a === void 0 ? void 0 : _a[0];
            const statusLabel = (_b = Object.entries(atoms_1.TicketStatus).find(([, value]) => value === data.status)) === null || _b === void 0 ? void 0 : _b[0];
            const sourceLabel = (_c = Object.entries(atoms_1.TicketSource).find(([, value]) => value === data.source)) === null || _c === void 0 ? void 0 : _c[0];
            await onComplete({
                data_points: {
                    ticketData: JSON.stringify(data),
                    requesterId: String(data.requester_id),
                    subject: data.subject,
                    type: data.type,
                    priorityValue: String(data.priority),
                    priorityLabel,
                    statusValue: String(data.status),
                    statusLabel,
                    sourceValue: String(data.source),
                    sourceLabel,
                    descriptionText: data.description_text,
                    descriptionHtml: data.description,
                    customFields: JSON.stringify(data.custom_fields),
                    tags: 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 (((_d = err.response) === null || _d === void 0 ? void 0 : _d.status) === 404)
                    await onError({
                        events: [
                            (0, addEventLog_1.addActivityEventLog)({
                                message: 'Ticket not found (404)',
                            }),
                        ],
                    });
                return;
            }
            // Throw all other errors
            throw error;
        }
    },
};
//# sourceMappingURL=getTicket.js.map