"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.patientDischarged = void 0;
const lodash_1 = require("lodash");
const types_1 = require("./types");
const client_1 = require("../client");
const validation_1 = require("../validation");
const utils_1 = require("../utils");
const dataPoints = {
    resourceId: {
        key: 'resourceId',
        valueType: 'string',
    },
    UPID: {
        key: 'UPID',
        valueType: 'string',
    },
    ownerId: {
        key: 'ownerId',
        valueType: 'string',
    },
    eventType: {
        key: 'eventType',
        valueType: 'string',
    },
    encounterStatus: {
        key: 'encounterStatus',
        valueType: 'string',
    },
    encounterStart: {
        key: 'encounterStart',
        valueType: 'date',
    },
    encounterEnd: {
        key: 'encounterEnd',
        valueType: 'date',
    },
    HL7EventType: {
        key: 'HL7EventType',
        valueType: 'string',
    },
    HL7EventTypeDescription: {
        key: 'HL7EventTypeDescription',
        valueType: 'string',
    },
    HL7EventTypeFull: {
        key: 'HL7EventTypeFull',
        valueType: 'string',
    },
};
exports.patientDischarged = {
    key: 'patientDischarged',
    dataPoints,
    onWebhookReceived: async ({ payload, settings }, onSuccess, onError) => {
        try {
            let { resourceId } = payload;
            if ((0, lodash_1.isNil)(resourceId)) {
                await onError({});
            }
            resourceId = validation_1.startsWithEncounter.parse(resourceId);
            const api = (0, client_1.makeAPIClient)(settings);
            const resource = await api.getResource(resourceId);
            if (resource.status === types_1.EncounterStatus.Discharged &&
                !(0, lodash_1.isNil)(resource.period.end)) {
                await onSuccess({
                    data_points: {
                        resourceId,
                        UPID: payload.resource.UPID,
                        ownerId: payload.ownerId,
                        eventType: types_1.EncounterStatus.Discharged,
                        encounterStatus: resource.status,
                        encounterStart: resource.period.start,
                        encounterEnd: resource.period.end,
                        HL7EventType: types_1.HL7EventType.A03.code,
                        HL7EventTypeFull: types_1.HL7EventType.A03.fullCode,
                        HL7EventTypeDescription: types_1.HL7EventType.A03.description,
                    },
                });
            }
        }
        catch (error) {
            let parsedError;
            if ((0, utils_1.isZodError)(error)) {
                parsedError = (0, utils_1.parseZodError)(error);
            }
            else if ((0, utils_1.isAxiosError)(error)) {
                parsedError = (0, utils_1.parseAxiosError)(error);
            }
            else {
                parsedError = (0, utils_1.parseUnknowError)(error);
            }
            await onError({
                events: [parsedError],
            });
        }
    },
};
//# sourceMappingURL=patientDischarged.js.map