"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.errorHandlerMiddleware = void 0;
const zod_1 = require("zod");
const domain_error_1 = require("../../domain/errors/domain-error");
const validation_error_1 = require("../../domain/errors/validation-error");
const errorHandlerMiddleware = (logger) => {
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    return (error, _req, res, _next) => {
        if (error instanceof domain_error_1.DomainError) {
            const response = {
                success: false,
                code: error.code,
                message: error.message,
            };
            if (error instanceof validation_error_1.ValidationError && error.details) {
                response.details = error.details;
            }
            res.status(error.statusCode).json(response);
            return;
        }
        if (error instanceof zod_1.ZodError) {
            const issues = error.issues.map((issue) => ({
                path: issue.path.join('.'),
                message: issue.message,
            }));
            res.status(400).json({
                success: false,
                code: 'VALIDATION_ERROR',
                message: 'Invalid request data',
                details: issues,
            });
            return;
        }
        logger.error('Unhandled error', {
            error: error.message,
            stack: error.stack,
            requestId: res.locals.requestId,
        });
        res.status(500).json({
            success: false,
            code: 'INTERNAL_ERROR',
            message: 'Internal server error',
        });
    };
};
exports.errorHandlerMiddleware = errorHandlerMiddleware;
//# sourceMappingURL=error-handler.middleware.js.map