import { ZodError } from 'zod';
import logger from '../logger.js';
import { AggregatedValidationError } from './AggregatedValidationError.js';

class ErrorHandler {
    error;
    developerMessage;
    userMessage;
    moreInfo;
    stack;
    status;
    constructor(error) {
        this.error = error;
        logger.error(`Error: ${JSON.stringify(error)}`);
    }
    formatError = () => {
        return this.handleError();
    };
    handleError = () => {
        // Zod error
        if (this.error instanceof ZodError) {
            const issues = this.error.issues
                .map((issue) => {
                return issue.message;
            })
                .join('. ');
            this.userMessage = `Schema validation error: ${issues}`;
            this.status = 500;
        }
        // AggregatedValidationError
        else if (this.error instanceof AggregatedValidationError) {
            this.userMessage = [
                this.error.message,
                ...this.error.details.map((detail) => {
                    const issues = detail.issues
                        .map((issue) => {
                        return issue.message;
                    })
                        .join('. ');
                    let message = '';
                    if (detail.type)
                        message = `Type: '${detail.type}'. `;
                    if (detail.id)
                        message += `ID: '${detail.id}'. `;
                    return `${message}Issues: ${issues}`;
                }),
            ];
            this.status = 500;
        }
        // status: FAILED
        else if (typeof this.error === 'string') {
            this.developerMessage = this.error;
        }
        // Error response
        else if (this.error.data) {
            const error = this.error.data;
            this.developerMessage = error.developerMessage;
            this.userMessage = error.userMessage;
            this.moreInfo = error.moreInfo;
            this.status = error.status;
        }
        // client side error
        else if (Object.prototype.hasOwnProperty.call(this.error, 'message')) {
            const error = this.error;
            this.userMessage = error.message;
            this.stack = error.stack;
            this.status = 500;
        }
        // Server response error
        else if (Object.prototype.hasOwnProperty.call(this.error, 'developerMessage')) {
            const error = this.error;
            this.developerMessage = error.developerMessage;
            this.userMessage = error.userMessage;
            this.moreInfo = error.moreInfo;
            this.status = error.status;
        }
        const formattedError = {
            ...(this.developerMessage && { developerMessage: this.developerMessage }),
            ...(this.userMessage && { userMessage: this.userMessage }),
            ...(this.moreInfo && { moreInfo: this.moreInfo }),
            ...(this.stack && { stack: this.stack }),
            ...(this.status && { status: this.status }),
        };
        logger.error(JSON.stringify(formattedError, null, 2));
        return formattedError;
    };
}

export { ErrorHandler, ErrorHandler as default };
//# sourceMappingURL=ErrorHandler.js.map
