"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateCoverage = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const extensions_core_2 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const utils_1 = require("../../utils");
const zod_1 = require("zod");
const client_1 = require("../../client");
exports.updateCoverage = {
    key: 'updateCoverage',
    category: extensions_core_2.Category.EHR_INTEGRATIONS,
    title: 'Update coverage',
    description: 'Update a coverage',
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        try {
            const { fields: { id, order, status, type, subscriberId, subscriber: subscriberValue, beneficiary: beneficiaryValue, relationship, periodStart, periodEnd, payor, classCoverage, }, } = (0, extensions_core_1.validate)({
                schema: zod_1.z.object({
                    fields: config_1.fieldsValidationSchema,
                }),
                payload,
            });
            const api = (0, client_1.makeAPIClient)(payload.settings);
            const coverageId = await api.updateCoverage({
                id,
                resourceType: 'Coverage',
                order,
                status,
                type,
                subscriber: {
                    reference: subscriberValue,
                },
                subscriberId,
                beneficiary: {
                    reference: beneficiaryValue,
                },
                relationship,
                period: {
                    start: periodStart,
                    end: periodEnd,
                },
                payor,
                class: classCoverage,
            });
            await onComplete({
                data_points: {
                    coverageId,
                },
            });
        }
        catch (error) {
            helpers.log({ error }, 'error', 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=updateCoverage.js.map