"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTask = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const zod_1 = require("zod");
const settings_1 = require("../../settings");
const config_1 = require("./config");
exports.createTask = {
    key: 'createTask',
    title: 'Creëer taak in HiX',
    description: 'Creëert een taak op de werklijst van de zorgverlener in HiX. De taak verschijnt op "Mijn werklijst" en kan vanuit HiX gelanceerd worden.',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        var _a, _b, _c;
        try {
            const { fields: { patientId, patientName, title, description, requester, launchUrl, }, settings: { apiUrl, tasksIngestToken }, } = (0, extensions_core_1.validate)({
                schema: zod_1.z.object({
                    fields: config_1.FieldsValidationSchema,
                    settings: settings_1.SettingsValidationSchema,
                }),
                payload,
            });
            const response = await fetch(apiUrl, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    Authorization: `Bearer ${tasksIngestToken}`,
                },
                body: JSON.stringify({
                    patient_id: patientId,
                    patient_name: patientName,
                    title,
                    description,
                    requester: requester !== null && requester !== void 0 ? requester : 'ZTOP',
                    launch_url: launchUrl,
                }),
            });
            if (!response.ok) {
                const responseText = await response.text();
                throw new Error(`HiX task endpoint returned ${response.status} ${response.statusText}: ${responseText}`);
            }
            const responseBody = (await response.json().catch(() => ({})));
            await onComplete({
                data_points: {
                    taskId: (_a = responseBody.id) !== null && _a !== void 0 ? _a : '',
                    statusCode: String(response.status),
                },
            });
        }
        catch (error) {
            const parsedError = error;
            await onError({
                events: [
                    {
                        date: new Date().toISOString(),
                        text: {
                            en: (_b = parsedError.message) !== null && _b !== void 0 ? _b : 'Unexpected error',
                        },
                        error: {
                            category: 'SERVER_ERROR',
                            message: (_c = parsedError.message) !== null && _c !== void 0 ? _c : 'Unexpected error',
                        },
                    },
                ],
            });
        }
    },
};
//# sourceMappingURL=createTask.js.map