"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFaxDocument = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const dataPoints_1 = require("./config/dataPoints");
const fields_1 = require("./config/fields");
const addEventLog_1 = require("../../../../src/lib/awell/addEventLog");
const validatePayloadAndCreateSdk_1 = require("../../lib/validatePayloadAndCreateSdk");
exports.getFaxDocument = {
    key: 'getFaxDocument',
    category: extensions_core_1.Category.COMMUNICATION,
    title: 'Get fax document',
    description: 'Get fax document with WestFax.',
    fields: config_1.fields,
    dataPoints: dataPoints_1.dataPoints,
    previewable: false,
    onActivityCreated: async (payload, onComplete, onError) => {
        const { fields, westFaxSdk } = await (0, validatePayloadAndCreateSdk_1.validatePayloadAndCreateSdk)({
            fieldsSchema: fields_1.FieldsValidationSchema,
            payload,
        });
        const activityEventLog = [];
        const response = await westFaxSdk.getFaxDocument({
            Cookies: false,
            FaxIds1: {
                Id: fields.faxId,
            },
            format: 'pdf',
        });
        if (!response.data.Success) {
            await onError({
                events: [
                    (0, addEventLog_1.addActivityEventLog)({
                        message: `Error getting fax document:\n${JSON.stringify(response.data, null, 2)}`,
                    }),
                ],
            });
            return;
        }
        if (response.data.Result.length === 0) {
            await onError({
                events: [
                    (0, addEventLog_1.addActivityEventLog)({
                        message: 'No fax document found',
                    }),
                ],
            });
            return;
        }
        if (response.data.Result.length > 1) {
            await onError({
                events: [
                    (0, addEventLog_1.addActivityEventLog)({
                        message: 'More than one fax document found',
                    }),
                ],
            });
            return;
        }
        const faxDocument = response.data.Result[0];
        if (faxDocument.FaxFiles[0].FileContents.length === 0) {
            activityEventLog.push((0, addEventLog_1.addActivityEventLog)({
                message: 'No fax file was found',
            }));
        }
        if (faxDocument.FaxFiles[0].FileContents.length > 1) {
            activityEventLog.push((0, addEventLog_1.addActivityEventLog)({
                message: 'More than 1 fax file was found, only storing the first one',
            }));
        }
        await onComplete({
            data_points: {
                direction: faxDocument.Direction,
                date: faxDocument.Date,
                status: faxDocument.Status,
                format: faxDocument.Format,
                pageCount: String(faxDocument.PageCount),
            },
        });
    },
};
//# sourceMappingURL=getFaxDocument.js.map