import { chineseMessages } from './eslint-chinese-messages.js';

// ESLint 中文格式化器
export default function chineseFormatter(results) {
  let output = '';
  let errorCount = 0;
  let warningCount = 0;
  let fixableErrorCount = 0;
  let fixableWarningCount = 0;

  results.forEach(result => {
    const messages = result.messages;

    if (messages.length === 0) {
      return;
    }

    errorCount += result.errorCount;
    warningCount += result.warningCount;
    fixableErrorCount += result.fixableErrorCount;
    fixableWarningCount += result.fixableWarningCount;

    output += `\n${result.filePath}\n`;

    messages.forEach(message => {
      const severity = message.severity === 2 ? '错误' : '警告';
      const ruleId = message.ruleId || '';
      const line = message.line || 0;
      const column = message.column || 0;

      // 使用中文消息映射
      const chineseMessage = chineseMessages[ruleId] || message.message;

      output += `  ${line}:${column}  ${severity}  ${chineseMessage}`;

      if (ruleId) {
        output += `  ${ruleId}`;
      }

      output += '\n';
    });
  });

  if (errorCount || warningCount) {
    output += '\n';

    if (errorCount > 0) {
      output += `✖ 发现 ${errorCount} 个错误`;
      if (fixableErrorCount > 0) {
        output += `，其中 ${fixableErrorCount} 个可自动修复`;
      }
      output += '\n';
    }

    if (warningCount > 0) {
      output += `⚠ 发现 ${warningCount} 个警告`;
      if (fixableWarningCount > 0) {
        output += `，其中 ${fixableWarningCount} 个可自动修复`;
      }
      output += '\n';
    }

    if (fixableErrorCount > 0 || fixableWarningCount > 0) {
      output += '\n运行 `pnpm lint` 来自动修复这些问题。\n';
    }
  } else {
    output = '✅ 代码检查通过！没有发现问题。\n';
  }

  return output;
}
