"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createMessageThread = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const client_1 = require("../../client");
const messageThread_zod_1 = require("../../validation/messageThread.zod");
const lodash_1 = require("lodash");
const config_1 = require("./config");
exports.createMessageThread = {
    key: 'createMessageThread',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: 'Create Message Thread',
    description: "Create a message thread in Elation's messaging system.",
    fields: config_1.fields,
    previewable: true,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError }) => {
        const { patientId, senderId, practiceId, documentDate, chartDate, messageBody, isUrgent, recipientId, groupId, } = config_1.FieldsValidationSchema.parse(payload.fields);
        const threadMembers = [
            // Individual recipient
            ...(!(0, lodash_1.isNil)(recipientId)
                ? [{ user: recipientId, status: 'Requiring Action' }]
                : []),
            // Group recipient
            ...(!(0, lodash_1.isNil)(groupId)
                ? [{ group: groupId, status: 'Requiring Action' }]
                : []),
            // Sender
            {
                user: senderId,
                status: 'Addressed',
            },
        ];
        const messageThread = messageThread_zod_1.messageThreadSchema.parse({
            patient: patientId,
            sender: senderId,
            practice: practiceId,
            document_date: documentDate,
            chart_date: chartDate,
            is_urgent: isUrgent,
            messages: [
                {
                    body: messageBody,
                    send_date: new Date().toISOString(),
                    sender: senderId,
                },
            ],
            members: threadMembers,
        });
        const api = (0, client_1.makeAPIClient)(payload.settings);
        const data = await api.createMessageThread(messageThread);
        await onComplete({
            data_points: {
                messageThreadId: String(data.id),
            },
        });
    },
};
//# sourceMappingURL=createMessageThread.js.map