"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ocrDocumentCompleted = void 0;
const lib_1 = require("./lib");
const dataPoints = {
    webhookData: {
        key: 'webhookData',
        valueType: 'json',
    },
    extractedInfo: {
        key: 'extractedInfo',
        valueType: 'json',
    },
};
exports.ocrDocumentCompleted = {
    key: 'ocrDocumentCompleted',
    dataPoints,
    onEvent: async ({ payload: { payload }, onSuccess, onError, helpers: { getOpenAIConfig }, }) => {
        var _a, _b;
        const ocrText = (_b = (_a = payload.ocrData) === null || _a === void 0 ? void 0 : _a.text) !== null && _b !== void 0 ? _b : '';
        // Get OpenAI API key from configuration
        const { apiKey } = getOpenAIConfig();
        // Extract patient information from OCR text using LLM
        let extractedInfo;
        try {
            extractedInfo = await (0, lib_1.extractPatientInfoFromOCR)({
                apiKey,
                ocrText,
            });
        }
        catch (error) {
            await onError({
                response: {
                    statusCode: 500,
                    message: error instanceof Error
                        ? error.message
                        : 'Failed to extract patient information from OCR text',
                },
            });
            return;
        }
        await onSuccess({
            data_points: {
                webhookData: JSON.stringify(payload),
                extractedInfo: JSON.stringify(extractedInfo),
            },
            // Use the patient hash as the patient identifier for matching
            ...(extractedInfo.patient_identifier_hash != null
                ? {
                    patient_identifier: {
                        system: 'documo-ocr-hash',
                        value: extractedInfo.patient_identifier_hash,
                    },
                }
                : {}),
        });
    },
};
//# sourceMappingURL=ocrDocumentCompleted.js.map