"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTask = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const sdk_1 = require("../../lib/sdk/graphql-codegen/generated/sdk");
const graphqlClient_1 = require("../../lib/sdk/graphql-codegen/graphqlClient");
const createTask_zod_1 = require("../../lib/validation/createTask.zod");
const errors_1 = require("../../lib/sdk/graphql-codegen/errors");
const config_1 = require("./config");
exports.createTask = {
    key: 'createTask',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Create task',
    description: 'Create a new task in Healthie.',
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        var _a, _b;
        const { fields, settings } = payload;
        try {
            const { patientId, assignToUserId, content, dueDate, reminder } = createTask_zod_1.createTaskSchema.parse(fields);
            const client = (0, graphqlClient_1.initialiseClient)(settings);
            if (client !== undefined) {
                const sdk = (0, sdk_1.getSdk)(client);
                const { data } = await sdk.createTask({
                    client_id: patientId,
                    user_id: assignToUserId,
                    content,
                    due_date: dueDate,
                    reminder,
                });
                await onComplete({
                    data_points: {
                        taskId: (_b = (_a = data.createTask) === null || _a === void 0 ? void 0 : _a.task) === null || _b === void 0 ? void 0 : _b.id,
                    },
                });
            }
            else {
                throw new Error('API Client is missing settings (api url or api key)');
            }
        }
        catch (err) {
            if (err instanceof errors_1.HealthieError) {
                const errors = (0, errors_1.mapHealthieToActivityError)(err.errors);
                await onError({
                    events: errors,
                });
            }
            else {
                throw err;
            }
        }
    },
};
//# sourceMappingURL=createTask.js.map