import { IViolations } from '../violations/IViolations';
import { IErrorHandler } from '../index';
import { GenericError } from '../generic';
import { getFirstErrorCode } from '../utils/IValidationExtractor';

/**
 * Unified Error Handler for GenericError
 * Handles the new unified error class that uses error catalog
 */
export class GenericErrorHandler implements IErrorHandler {
	canHandle(error: unknown): boolean {
		return error instanceof GenericError;
	}

	handle(error: unknown): IViolations {
		const e = error as GenericError;

		const firstErrorCode = getFirstErrorCode(e.additionalData);

		return [
			{
				messages: e.messages,
				type: e.type,
				code: firstErrorCode || e.code,
				additionalData: e.additionalData,
			},
		];
	}
}
