import { logger } from "../../logger/index.mjs";
import { HttpStatusCodes } from "../httpStatusCodes.mjs";
import { formatPaginatedResponse, formatResponse } from "../responseData.mjs";
import { errorData } from "./errorCodes.mjs";
import { Locales } from "@intlayer/types";
import { t } from "fastify-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 = 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 ?? HttpStatusCodes.INTERNAL_SERVER_ERROR_500, isPaginatedResponse);
			return;
		}
		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 = t(title, Locales.ENGLISH);
		const errorMessage = t(message, Locales.ENGLISH);
		logger.error(errorMessage, messageDetails);
		const status = statusCode ?? HttpStatusCodes.INTERNAL_SERVER_ERROR_500;
		if (isPaginatedResponse) {
			const responseData = formatPaginatedResponse({
				error: {
					code: errorKey,
					title: errorTitle,
					message: errorMessage
				},
				status
			});
			if ("status" in res && "json" in res) res.status(status).json(responseData);
			else res.code(status).send(responseData);
			return;
		}
		const responseData = formatResponse({
			error: {
				code: errorKey,
				title: errorTitle,
				message: errorMessage,
				...messageDetails
			},
			status
		});
		if ("status" in res && "json" in res) res.status(status).json(responseData);
		else res.code(status).send(responseData);
	}
	/**
	* Formats a generic error response without sending it (useful for Fastify plugins).
	* @param errorKey - A key representing the specific error.
	* @param errorDetails - Optional error details to include.
	* @param statusCode - Optional HTTP status code.
	* @returns Formatted error response object.
	*/
	static formatGenericErrorResponse(errorKey, errorDetails, statusCode) {
		const error = errorData[errorKey];
		const status = statusCode ?? error.statusCode;
		const errorTitle = t(error.title, Locales.ENGLISH);
		const errorMessage = t(error.message, Locales.ENGLISH);
		logger.error(errorMessage, errorDetails);
		return formatResponse({
			error: {
				code: errorKey,
				title: errorTitle,
				message: errorMessage,
				...errorDetails
			},
			status
		});
	}
};

//#endregion
export { ErrorHandler };
//# sourceMappingURL=ErrorHandler.mjs.map