"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.requestFormCompletionUpdated = 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 = {
    updatedFormCompletionId: {
        key: 'updatedFormCompletionId',
        valueType: 'string',
    },
};
exports.requestFormCompletionUpdated = {
    key: 'requestFormCompletionUpdated',
    dataPoints,
    onWebhookReceived: async ({ payload, settings }, onSuccess, onError) => {
        var _a, _b;
        try {
            const { sdk } = await (0, createSdk_1.createSdk)({ settings });
            const validatedPayload = helpers_1.webhookPayloadSchema.parse(payload);
            const updatedFormCompletionId = validatedPayload.resource_id.toString();
            const response = await sdk.getRequestedFormCompletion({
                id: updatedFormCompletionId,
            });
            const healthiePatientId = (_b = (_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.requestedFormCompletion) === null || _b === void 0 ? void 0 : _b.recipient_id;
            await onSuccess({
                data_points: {
                    updatedFormCompletionId,
                },
                ...(!(0, lodash_1.isNil)(healthiePatientId) && {
                    patient_identifier: {
                        system: types_1.HEALTHIE_IDENTIFIER,
                        value: healthiePatientId,
                    },
                }),
            });
        }
        catch (error) {
            await onError((0, errors_1.formatError)(error));
        }
    },
};
//# sourceMappingURL=requestedFormCompletionUpdated.js.map