"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPatientByIdentifier = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const zod_1 = require("zod");
const awellSdk_1 = __importDefault(require("../../sdk/awellSdk"));
const lodash_1 = require("lodash");
const addEventLog_1 = require("../../../../../src/lib/awell/addEventLog");
exports.getPatientByIdentifier = {
    key: 'getPatientByIdentifier',
    category: extensions_core_1.Category.WORKFLOW,
    title: 'Get patient by identifier',
    description: 'Retrieve a patient based on the provided identifier system and value',
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    supports_automated_retries: true,
    onEvent: async ({ payload, onComplete, helpers: { awellSdk }, }) => {
        const { fields: { system, value }, } = (0, extensions_core_1.validate)({
            schema: zod_1.z.object({
                fields: config_1.FieldsValidationSchema,
            }),
            payload,
        });
        const { apiKey, apiUrl } = await awellSdk();
        const sdk = new awellSdk_1.default({ apiKey, apiUrl: apiUrl });
        const patient = await sdk.getPatientByIdentifier({
            system,
            value,
        });
        if ((0, lodash_1.isNil)(patient)) {
            await onComplete({
                data_points: {
                    patientAlreadyExists: String(false),
                    patientId: undefined,
                },
                events: [
                    (0, addEventLog_1.addActivityEventLog)({
                        message: `No patient with identifier system ${system} and value ${value} exists.`,
                    }),
                ],
            });
            return;
        }
        await onComplete({
            data_points: {
                patientAlreadyExists: String(true),
                patientId: patient.id,
            },
            events: [
                (0, addEventLog_1.addActivityEventLog)({
                    message: `Patient with identifier system ${system} and value ${value} was found. The Awell ID of the patient is ${patient.id}.`,
                }),
            ],
        });
    },
};
//# sourceMappingURL=getPatientByIdentifier.js.map