"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.messageDeleted = void 0;
const lodash_1 = require("lodash");
const types_1 = require("../lib/types");
const errors_1 = require("../lib/sdk/graphql-codegen/errors");
const createSdk_1 = require("../lib/sdk/graphql-codegen/createSdk");
const helpers_1 = require("../lib/helpers");
const dataPoints = {
    deletedMessageId: {
        key: 'deletedMessageId',
        valueType: 'string',
    },
};
exports.messageDeleted = {
    key: 'messageDeleted',
    dataPoints,
    onWebhookReceived: async ({ payload, settings }, onSuccess, onError) => {
        var _a, _b, _c, _d;
        try {
            const { sdk } = await (0, createSdk_1.createSdk)({ settings });
            const validatedPayload = helpers_1.webhookPayloadSchema.parse(payload);
            const deletedMessageId = validatedPayload.resource_id.toString();
            const messageResponse = await sdk.getMessage({ id: deletedMessageId });
            const conversationResponse = await sdk.getConversation({
                id: (_b = (_a = messageResponse === null || messageResponse === void 0 ? void 0 : messageResponse.data) === null || _a === void 0 ? void 0 : _a.note) === null || _b === void 0 ? void 0 : _b.conversation_id,
            });
            const healthiePatientId = (_d = (_c = conversationResponse === null || conversationResponse === void 0 ? void 0 : conversationResponse.data) === null || _c === void 0 ? void 0 : _c.conversation) === null || _d === void 0 ? void 0 : _d.patient_id;
            await onSuccess({
                data_points: {
                    deletedMessageId,
                },
                ...(!(0, lodash_1.isNil)(healthiePatientId) && {
                    patient_identifier: {
                        system: types_1.HEALTHIE_IDENTIFIER,
                        value: healthiePatientId,
                    },
                }),
            });
        }
        catch (error) {
            await onError((0, errors_1.formatError)(error));
        }
    },
};
//# sourceMappingURL=messageDeleted.js.map