"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatError = exports.mapHealthieToActivityError = exports.getHealthieErrorFromResponse = exports.HealthieError = void 0;
const lodash_1 = require("lodash");
const zod_1 = require("zod");
const zod_validation_error_1 = require("zod-validation-error");
class HealthieError extends Error {
    constructor(errors) {
        super('Error in Healthie: invalid object name or field, or a record is not found.');
        this.name = 'HealthieError';
        this.errors = errors;
    }
}
exports.HealthieError = HealthieError;
const getHealthieErrorFromResponse = (response) => {
    var _a;
    if (response instanceof Error) {
        return [{ message: response.message }];
    }
    const dataKeys = (0, lodash_1.keys)(response === null || response === void 0 ? void 0 : response.data);
    for (const key of dataKeys) {
        const messages = (_a = (response === null || response === void 0 ? void 0 : response.data)[key]) === null || _a === void 0 ? void 0 : _a.messages;
        if (!(0, lodash_1.isNil)(messages) && messages.length > 0) {
            return messages;
        }
    }
    return undefined;
};
exports.getHealthieErrorFromResponse = getHealthieErrorFromResponse;
const mapHealthieToActivityError = (errors) => {
    if ((0, lodash_1.isNil)(errors))
        return [];
    const nonNullErrors = errors.filter((value) => !(0, lodash_1.isNil)(value));
    return nonNullErrors.map((error) => ({
        date: new Date().toISOString(),
        text: { en: 'Healthie API reported an error' },
        error: {
            category: 'SERVER_ERROR',
            message: `${(0, lodash_1.isNil)(error.field) ? 'Message: ' : `Field "${error.field}": `}${error.message};`,
        },
    }));
};
exports.mapHealthieToActivityError = mapHealthieToActivityError;
const formatError = (error) => {
    var _a;
    switch (true) {
        case (error instanceof zod_1.ZodError): {
            const err = (0, zod_validation_error_1.fromZodError)(error);
            return {
                events: [
                    {
                        date: new Date().toISOString(),
                        text: { en: err.name },
                        error: {
                            category: 'WRONG_INPUT',
                            message: `${err.message}`,
                        },
                    },
                ],
            };
        }
        case (error instanceof HealthieError): {
            const errors = (0, exports.mapHealthieToActivityError)(error.errors);
            return {
                events: errors,
            };
        }
        default: {
            const err = error;
            const errMessage = (_a = err.message) !== null && _a !== void 0 ? _a : 'Unable to process the webhook';
            return {
                events: [
                    {
                        date: new Date().toISOString(),
                        text: { en: errMessage },
                        error: {
                            category: 'SERVER_ERROR',
                            message: errMessage,
                        },
                    }
                ]
            };
        }
    }
};
exports.formatError = formatError;
//# sourceMappingURL=errors.js.map