"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.startNetworkQuery = 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 dataPoints_1 = require("./dataPoints");
const zod_1 = require("zod");
const startNetworkQuerySchema = zod_1.z.object({
    patientId: zod_1.z
        .string({ errorMap: () => ({ message: 'Missing patientId' }) })
        .min(1),
});
exports.startNetworkQuery = {
    key: 'startNetworkQuery',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Start Network Query',
    description: 'Triggers an asynchronous query for the specified Patient across available health data networks (HIEs, pharmacies, labs). Results are sent via webhooks.',
    fields: fields_1.startNetworkQueryFields,
    previewable: false,
    dataPoints: dataPoints_1.networkQueryDataPoints,
    onActivityCreated: async (payload, onComplete, onError) => {
        try {
            const { patientId } = startNetworkQuerySchema.parse(payload.fields);
            const api = (0, client_1.createMetriportApi)(payload.settings);
            const resp = await api.startNetworkQuery({
                patientId,
                sources: ['hie'],
            });
            await onComplete({
                data_points: {
                    requestId: resp.requestId,
                    status: resp.status,
                },
            });
        }
        catch (err) {
            await (0, errorHandler_1.handleErrorMessage)(err, onError);
        }
    },
};
//# sourceMappingURL=startNetworkQuery.js.map