/**
 * Error Formatter
 *
 * Formats validation issues for beautiful console output.
 * Provides colored, contextualized error messages with suggestions.
 *
 * @module registry/formatters/error-formatter
 * @version 2.0.0
 */
/**
 * ANSI color codes for terminal output
 */
const colors = {
    reset: '\x1b[0m',
    bold: '\x1b[1m',
    dim: '\x1b[2m',
    // Severity colors
    error: '\x1b[31m', // Red
    warning: '\x1b[33m', // Yellow
    info: '\x1b[36m', // Cyan
    success: '\x1b[32m', // Green
    // Element colors
    title: '\x1b[1m\x1b[37m', // Bold White
    file: '\x1b[35m', // Magenta
    code: '\x1b[36m', // Cyan
    suggestion: '\x1b[2m\x1b[37m' // Dim White
};
/**
 * Error Formatter
 *
 * Formats validation issues for console output.
 */
export class ErrorFormatter {
    options;
    constructor(options = {}) {
        this.options = {
            colors: options.colors ?? true,
            showSuggestions: options.showSuggestions ?? true,
            showFix: options.showFix ?? true,
            showLearnMore: options.showLearnMore ?? true,
            compact: options.compact ?? false
        };
    }
    /**
     * Format a complete validation report
     *
     * @param report - Validation report to format
     * @returns Formatted string ready for console output
     */
    formatReport(report) {
        const lines = [];
        // Header
        lines.push(this.formatHeader(report));
        lines.push('');
        // Summary
        if (!this.options.compact) {
            lines.push(this.formatSummary(report));
            lines.push('');
        }
        // Issues
        if (report.result.errors.length > 0) {
            lines.push(this.color(colors.bold, '━━━ Errors ━━━'));
            lines.push('');
            report.result.errors.forEach(error => {
                lines.push(this.formatIssue(error));
                lines.push('');
            });
        }
        if (report.result.warnings.length > 0 && !this.options.compact) {
            lines.push(this.color(colors.bold, '━━━ Warnings ━━━'));
            lines.push('');
            report.result.warnings.forEach(warning => {
                lines.push(this.formatIssue(warning));
                lines.push('');
            });
        }
        if (report.result.info.length > 0 && !this.options.compact) {
            lines.push(this.color(colors.bold, '━━━ Info ━━━'));
            lines.push('');
            report.result.info.forEach(info => {
                lines.push(this.formatIssue(info));
                lines.push('');
            });
        }
        // Footer
        lines.push(this.formatFooter(report));
        return lines.join('\n');
    }
    /**
     * Format validation result
     *
     * @param result - Validation result to format
     * @returns Formatted string
     */
    formatResult(result) {
        const lines = [];
        // Errors
        if (result.errors.length > 0) {
            lines.push(this.color(colors.bold, '━━━ Errors ━━━'));
            lines.push('');
            result.errors.forEach(error => {
                lines.push(this.formatIssue(error));
                lines.push('');
            });
        }
        // Warnings
        if (result.warnings.length > 0) {
            lines.push(this.color(colors.bold, '━━━ Warnings ━━━'));
            lines.push('');
            result.warnings.forEach(warning => {
                lines.push(this.formatIssue(warning));
                lines.push('');
            });
        }
        // Info
        if (result.info.length > 0) {
            lines.push(this.color(colors.bold, '━━━ Info ━━━'));
            lines.push('');
            result.info.forEach(info => {
                lines.push(this.formatIssue(info));
                lines.push('');
            });
        }
        return lines.join('\n');
    }
    /**
     * Format a single validation issue
     *
     * @param issue - Issue to format
     * @returns Formatted string
     */
    formatIssue(issue) {
        const lines = [];
        // Icon and title
        const icon = this.getIcon(issue.severity);
        const severityColor = this.getSeverityColor(issue.severity);
        lines.push(`${this.color(severityColor, icon)} ` +
            `${this.color(colors.title, issue.title)} ` +
            `${this.color(colors.dim, `[${issue.code}]`)}`);
        // File location
        if (issue.file) {
            const location = issue.line > 0
                ? `${issue.file}:${issue.line}:${issue.column}`
                : issue.file;
            lines.push(`  ${this.color(colors.file, location)}`);
        }
        // Message
        lines.push(`  ${issue.message}`);
        // Suggestions
        if (this.options.showSuggestions && issue.suggestions && issue.suggestions.length > 0) {
            lines.push('');
            lines.push(this.color(colors.dim, '  Suggestions:'));
            issue.suggestions.forEach(suggestion => {
                lines.push(`  ${this.color(colors.suggestion, '• ' + suggestion)}`);
            });
        }
        // Fix command
        if (this.options.showFix && issue.fix) {
            lines.push('');
            lines.push(this.color(colors.dim, '  Fix:'));
            lines.push(`  ${this.color(colors.code, '$ ' + issue.fix)}`);
        }
        // Learn more
        if (this.options.showLearnMore && issue.learnMoreUrl) {
            lines.push('');
            lines.push(`  ${this.color(colors.dim, 'Learn more:')} ${this.color(colors.info, issue.learnMoreUrl)}`);
        }
        return lines.join('\n');
    }
    /**
     * Format header
     *
     * @param report - Validation report
     * @returns Formatted header
     */
    formatHeader(report) {
        const lines = [];
        if (report.result.valid) {
            lines.push(this.color(colors.success, '✓ Validation Passed'));
        }
        else {
            lines.push(this.color(colors.error, '✗ Validation Failed'));
        }
        return lines.join('\n');
    }
    /**
     * Format summary
     *
     * @param report - Validation report
     * @returns Formatted summary
     */
    formatSummary(report) {
        const lines = [];
        const { summary } = report;
        lines.push(this.color(colors.bold, 'Summary:'));
        // Validations
        if (summary.validationsPassed.length > 0) {
            lines.push(`  ${this.color(colors.success, '✓')} Passed: ${summary.validationsPassed.join(', ')}`);
        }
        if (summary.validationsFailed.length > 0) {
            lines.push(`  ${this.color(colors.error, '✗')} Failed: ${summary.validationsFailed.join(', ')}`);
        }
        // Issue counts
        lines.push('');
        lines.push(this.color(colors.bold, 'Issues:'));
        if (summary.errorCount > 0) {
            lines.push(`  ${this.color(colors.error, '✗')} Errors: ${summary.errorCount}`);
        }
        if (summary.warningCount > 0) {
            lines.push(`  ${this.color(colors.warning, '⚠')} Warnings: ${summary.warningCount}`);
        }
        if (summary.infoCount > 0) {
            lines.push(`  ${this.color(colors.info, 'ℹ')} Info: ${summary.infoCount}`);
        }
        if (summary.totalIssues === 0) {
            lines.push(`  ${this.color(colors.success, 'No issues found')}`);
        }
        return lines.join('\n');
    }
    /**
     * Format footer
     *
     * @param report - Validation report
     * @returns Formatted footer
     */
    formatFooter(report) {
        const lines = [];
        if (report.result.valid) {
            lines.push(this.color(colors.success, '✓ All validations passed successfully'));
        }
        else {
            lines.push(this.color(colors.error, `✗ Validation failed with ${report.summary.errorCount} error(s)`));
            if (!this.options.compact) {
                lines.push('');
                lines.push(this.color(colors.dim, 'Run with --help for more information'));
            }
        }
        return lines.join('\n');
    }
    /**
     * Get icon for severity
     *
     * @param severity - Issue severity
     * @returns Icon character
     */
    getIcon(severity) {
        switch (severity) {
            case 'error':
                return '✗';
            case 'warning':
                return '⚠';
            case 'info':
                return 'ℹ';
            default:
                return '•';
        }
    }
    /**
     * Get color for severity
     *
     * @param severity - Issue severity
     * @returns ANSI color code
     */
    getSeverityColor(severity) {
        switch (severity) {
            case 'error':
                return colors.error;
            case 'warning':
                return colors.warning;
            case 'info':
                return colors.info;
            default:
                return colors.reset;
        }
    }
    /**
     * Apply color to text
     *
     * @param colorCode - ANSI color code
     * @param text - Text to color
     * @returns Colored text (or plain text if colors disabled)
     */
    color(colorCode, text) {
        if (!this.options.colors) {
            return text;
        }
        return `${colorCode}${text}${colors.reset}`;
    }
}
//# sourceMappingURL=error-formatter.js.map