"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.queryDocs = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const client_1 = require("../../client");
const errorHandler_1 = require("../../shared/errorHandler");
const fields_1 = require("./fields");
const validation_1 = require("./validation");
const dataPoints_1 = require("./dataPoints");
exports.queryDocs = {
    key: 'queryDocs',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Start Document Query',
    description: 'Triggers a document query for the specified patient across HIEs.',
    fields: fields_1.queryFields,
    previewable: true,
    dataPoints: dataPoints_1.documentQueryDataPoints,
    onActivityCreated: async (payload, onComplete, onError) => {
        var _a, _b, _c, _d, _e, _f, _g;
        try {
            const { patientId, facilityId } = validation_1.startQuerySchema.parse(payload.fields);
            const api = (0, client_1.createMetriportApi)(payload.settings);
            const resp = await api.startDocumentQuery(patientId, facilityId);
            await onComplete({
                data_points: {
                    requestId: (_a = resp.requestId) !== null && _a !== void 0 ? _a : '',
                    downloadStatus: (_c = (_b = resp.download) === null || _b === void 0 ? void 0 : _b.status) !== null && _c !== void 0 ? _c : '',
                    downloadTotal: String((_e = (_d = resp.download) === null || _d === void 0 ? void 0 : _d.total) !== null && _e !== void 0 ? _e : 0),
                    downloadSuccessful: String((_g = (_f = resp.download) === null || _f === void 0 ? void 0 : _f.successful) !== null && _g !== void 0 ? _g : 0),
                },
            });
        }
        catch (err) {
            await (0, errorHandler_1.handleErrorMessage)(err, onError);
        }
    },
};
//# sourceMappingURL=query.js.map