const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
const require_logger_index = require('../../logger/index.cjs');
const require_utils_httpStatusCodes = require('../httpStatusCodes.cjs');
const require_utils_responseData = require('../responseData.cjs');
const require_utils_errors_errorCodes = require('./errorCodes.cjs');
let __intlayer_types = require("@intlayer/types");
__intlayer_types = require_rolldown_runtime.__toESM(__intlayer_types);
let express_intlayer = require("express-intlayer");
express_intlayer = require_rolldown_runtime.__toESM(express_intlayer);

//#region src/utils/errors/ErrorHandler.ts
var ErrorHandler = class ErrorHandler {
	/**
	* Handles generic error responses by formatting and sending a JSON response.
	* @param res - The response object provided by Express.js.
	* @param errorKey - A key representing the specific error.
	* @param statusCode - (Optional) A specific HTTP status code to use for the response.
	* @param isPaginatedResponse - Flag to determine if the response should be paginated.
	*/
	static handleGenericErrorResponse(res, errorKey, errorDetails, statusCode, isPaginatedResponse = false) {
		const error = require_utils_errors_errorCodes.errorData[errorKey];
		const status = statusCode ?? error.statusCode;
		ErrorHandler.handleCustomErrorResponse(res, errorKey, error.title, error.message, errorDetails, status, isPaginatedResponse);
	}
	/**
	* Handles application-specific error responses by formatting and sending a JSON response.
	* @param res - The response object provided by Express.js.
	* @param error - The error object.
	* @param messageDetails - (Optional) Additional message details to include in the response.
	* @param isPaginatedResponse - (Optional) Flag to determine if the response should be paginated.
	*/
	static handleAppErrorResponse(res, error, messageDetails, isPaginatedResponse = false) {
		if (!error.isAppError) ErrorHandler.handleCustomErrorResponse(res, error.errorKey ?? "UNKNOWN_ERROR", "Error", error.message ?? JSON.stringify(error), void 0, error.httpStatusCode ?? require_utils_httpStatusCodes.HttpStatusCodes.INTERNAL_SERVER_ERROR_500, isPaginatedResponse);
		const isMultilingual = error.isMultilingual ?? false;
		ErrorHandler.handleCustomErrorResponse(res, error.errorKey, isMultilingual ? error.multilingualTitle : error.title, isMultilingual ? error.multilingualMessage : error.message, error.messageDetails ?? messageDetails, error.httpStatusCode, isPaginatedResponse);
	}
	/**
	* Handles more customizable error responses with detailed error messages and codes.
	* @param res - The response object.
	* @param errorKey - Error code key used to fetch the corresponding message and default status.
	* @param message - The localized error message object.
	* @param messageDetails - (Optional) Additional message details to include in the response.
	* @param statusCode - (Optional) HTTP status code, defaults to 500 if not specified.
	* @param isPaginatedResponse - Determines if the error should be part of a paginated response.
	*/
	static handleCustomErrorResponse(res, errorKey, title, message, messageDetails, statusCode, isPaginatedResponse = false) {
		const errorTitle = (0, express_intlayer.t)(title, __intlayer_types.Locales.ENGLISH);
		const errorMessage = (0, express_intlayer.t)(message, __intlayer_types.Locales.ENGLISH);
		require_logger_index.logger.error(errorMessage, messageDetails);
		const status = statusCode ?? require_utils_httpStatusCodes.HttpStatusCodes.INTERNAL_SERVER_ERROR_500;
		if (isPaginatedResponse) {
			const responseData$1 = require_utils_responseData.formatPaginatedResponse({
				error: {
					code: errorKey,
					title: errorTitle,
					message: errorMessage
				},
				status
			});
			res.status(status).json(responseData$1);
			return;
		}
		const responseData = require_utils_responseData.formatResponse({
			error: {
				code: errorKey,
				title: errorTitle,
				message: errorMessage,
				...messageDetails
			},
			status
		});
		res.status(status).json(responseData);
	}
};

//#endregion
exports.ErrorHandler = ErrorHandler;
//# sourceMappingURL=ErrorHandler.cjs.map