"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var lint_exports = {};
__export(lint_exports, {
  lintFilter: () => lintFilter
});
module.exports = __toCommonJS(lint_exports);
const lintFilter = {
  name: "lint",
  matches(command) {
    return /\b(eslint|tsc|typescript|ruff|clippy|cargo\s+clippy|cargo\s+build|prettier|biome|golangci-lint|rubocop|next\s+build)\b/.test(command);
  },
  filter(command, rawOutput, level) {
    if (/tsc\b|typescript/.test(command)) return filterTsc(rawOutput, level);
    if (/eslint/.test(command)) return filterEslint(rawOutput, level);
    if (/ruff/.test(command)) return filterRuff(rawOutput, level);
    if (/cargo\s+clippy/.test(command)) return filterCargoClippy(rawOutput, level);
    if (/cargo\s+build/.test(command)) return filterCargoBuild(rawOutput, level);
    if (/prettier/.test(command)) return filterPrettier(rawOutput, level);
    if (/biome/.test(command)) return filterBiome(rawOutput, level);
    if (/golangci-lint/.test(command)) return filterGolangciLint(rawOutput, level);
    if (/rubocop/.test(command)) return filterRubocop(rawOutput, level);
    if (/next\s+build/.test(command)) return filterNextBuild(rawOutput, level);
    return { output: rawOutput, strategy: "lint:passthrough" };
  }
};
function filterTsc(raw, level) {
  const lines = raw.split("\n").filter((l) => l.trim());
  if (lines.length === 0 || raw.includes("0 errors")) {
    return { output: "ok (no errors)", strategy: "tsc:clean" };
  }
  const errorsByFile = /* @__PURE__ */ new Map();
  for (const line of lines) {
    const match = line.match(/^(.+?)\((\d+),(\d+)\):\s+error\s+(TS\d+):\s+(.+)/);
    if (match) {
      const [, file, , , code, msg] = match;
      if (!errorsByFile.has(file)) errorsByFile.set(file, []);
      errorsByFile.get(file).push(`${code}: ${msg}`);
      continue;
    }
    const altMatch = line.match(/^(.+?):(\d+):(\d+)\s+-\s+error\s+(TS\d+):\s+(.+)/);
    if (altMatch) {
      const [, file, , , code, msg] = altMatch;
      if (!errorsByFile.has(file)) errorsByFile.set(file, []);
      errorsByFile.get(file).push(`${code}: ${msg}`);
    }
  }
  if (errorsByFile.size === 0) {
    return { output: raw, strategy: "tsc:passthrough" };
  }
  const totalErrors = [...errorsByFile.values()].reduce((s, e) => s + e.length, 0);
  if (level === "ultra") {
    const summary = [...errorsByFile.entries()].map(([f, errs]) => `${f}: ${errs.length} errors`).join("\n");
    return { output: `${totalErrors} errors in ${errorsByFile.size} files
${summary}`, strategy: "tsc:ultra" };
  }
  const parts = [`${totalErrors} TypeScript errors in ${errorsByFile.size} files:
`];
  const maxFiles = level === "aggressive" ? 5 : 10;
  let shown = 0;
  for (const [file, errors] of errorsByFile) {
    if (shown >= maxFiles) break;
    parts.push(`${file} (${errors.length}):`);
    const maxErrs = level === "aggressive" ? 3 : 5;
    for (const err of errors.slice(0, maxErrs)) {
      parts.push(`  ${err}`);
    }
    if (errors.length > maxErrs) parts.push(`  \u2026+${errors.length - maxErrs} more`);
    shown++;
  }
  if (errorsByFile.size > maxFiles) {
    parts.push(`
\u2026+${errorsByFile.size - maxFiles} more files with errors`);
  }
  return { output: parts.join("\n"), strategy: "tsc:grouped" };
}
function filterEslint(raw, level) {
  const lines = raw.split("\n");
  if (lines.every((l) => !l.trim() || l.includes("0 problems"))) {
    return { output: "ok (no problems)", strategy: "eslint:clean" };
  }
  const ruleCount = /* @__PURE__ */ new Map();
  const errorsByFile = /* @__PURE__ */ new Map();
  let currentFile = "";
  for (const line of lines) {
    if (line.match(/^[/.]/)) {
      currentFile = line.trim();
      continue;
    }
    const match = line.match(/^\s+(\d+):(\d+)\s+(error|warning)\s+(.+?)\s{2,}(\S+)\s*$/);
    if (match) {
      const [, , , , msg, rule] = match;
      ruleCount.set(rule, (ruleCount.get(rule) || 0) + 1);
      if (currentFile) {
        if (!errorsByFile.has(currentFile)) errorsByFile.set(currentFile, []);
        errorsByFile.get(currentFile).push(`${rule}: ${msg}`);
      }
    }
  }
  const totalProblems = [...ruleCount.values()].reduce((s, c) => s + c, 0);
  if (level === "ultra") {
    const topRules = [...ruleCount.entries()].sort((a, b) => b[1] - a[1]).slice(0, 5).map(([rule, count]) => `${rule}: ${count}`).join(", ");
    return { output: `${totalProblems} problems: ${topRules}`, strategy: "eslint:ultra" };
  }
  if (level === "aggressive") {
    const byRule = [...ruleCount.entries()].sort((a, b) => b[1] - a[1]).map(([rule, count]) => `  ${rule}: ${count}`).join("\n");
    return { output: `${totalProblems} problems in ${errorsByFile.size} files:
${byRule}`, strategy: "eslint:byrule" };
  }
  const parts = [`${totalProblems} problems in ${errorsByFile.size} files:
`];
  let shown = 0;
  for (const [file, errors] of errorsByFile) {
    if (shown >= 10) break;
    parts.push(`${file} (${errors.length}):`);
    for (const err of errors.slice(0, 3)) parts.push(`  ${err}`);
    if (errors.length > 3) parts.push(`  \u2026+${errors.length - 3} more`);
    shown++;
  }
  if (errorsByFile.size > 10) parts.push(`
\u2026+${errorsByFile.size - 10} more files`);
  return { output: parts.join("\n"), strategy: "eslint:grouped" };
}
function filterRuff(raw, level) {
  const lines = raw.split("\n").filter((l) => l.trim());
  if (lines.length === 0 || raw.includes("All checks passed")) {
    return { output: "ok (no issues)", strategy: "ruff:clean" };
  }
  const ruleCount = /* @__PURE__ */ new Map();
  for (const line of lines) {
    const match = line.match(/\b([A-Z]\d{3,4})\b/);
    if (match) ruleCount.set(match[1], (ruleCount.get(match[1]) || 0) + 1);
  }
  const total = [...ruleCount.values()].reduce((s, c) => s + c, 0);
  if (level === "ultra") {
    const top = [...ruleCount.entries()].sort((a, b) => b[1] - a[1]).slice(0, 5).map(([r, c]) => `${r}:${c}`).join(" ");
    return { output: `${total} issues: ${top}`, strategy: "ruff:ultra" };
  }
  const byRule = [...ruleCount.entries()].sort((a, b) => b[1] - a[1]).map(([rule, count]) => `  ${rule}: ${count}`).join("\n");
  return { output: `${total} issues:
${byRule}`, strategy: "ruff:grouped" };
}
function filterCargoClippy(raw, level) {
  const lines = raw.split("\n");
  if (raw.includes("0 warnings") && !raw.includes("error")) {
    return { output: "ok (no warnings)", strategy: "clippy:clean" };
  }
  const issues = [];
  for (const line of lines) {
    if (line.startsWith("warning:") || line.startsWith("error")) {
      issues.push(line);
    }
  }
  const errors = issues.filter((l) => l.startsWith("error"));
  const warnings = issues.filter((l) => l.startsWith("warning:"));
  if (level === "ultra") {
    return { output: `${errors.length} errors, ${warnings.length} warnings`, strategy: "clippy:ultra" };
  }
  const maxIssues = level === "aggressive" ? 10 : 20;
  const shown = issues.slice(0, maxIssues).join("\n");
  const extra = issues.length > maxIssues ? `
\u2026+${issues.length - maxIssues} more` : "";
  return { output: `${errors.length} errors, ${warnings.length} warnings:

${shown}${extra}`, strategy: "clippy:filtered" };
}
function filterCargoBuild(raw, level) {
  const lines = raw.split("\n");
  if (raw.includes("Finished") && !raw.includes("error")) {
    const finishLine = lines.find((l) => l.includes("Finished"));
    return { output: finishLine || "ok", strategy: "cargo-build:success" };
  }
  const errors = lines.filter((l) => l.startsWith("error"));
  if (errors.length === 0) {
    return { output: raw, strategy: "cargo-build:passthrough" };
  }
  const maxErrors = level === "ultra" ? 5 : level === "aggressive" ? 10 : 20;
  const shown = errors.slice(0, maxErrors).join("\n");
  const extra = errors.length > maxErrors ? `
\u2026+${errors.length - maxErrors} more errors` : "";
  return { output: `${errors.length} errors:

${shown}${extra}`, strategy: "cargo-build:errors" };
}
function filterPrettier(raw, level) {
  const lines = raw.split("\n").filter((l) => l.trim());
  if (lines.length === 0 || raw.includes("All matched files use Prettier")) {
    return { output: "ok (all formatted)", strategy: "prettier:clean" };
  }
  const files = lines.filter((l) => !l.startsWith("[") && l.includes("."));
  if (level === "ultra") {
    return { output: `${files.length} files need formatting`, strategy: "prettier:ultra" };
  }
  const maxFiles = level === "aggressive" ? 10 : 20;
  const shown = files.slice(0, maxFiles).join("\n");
  const extra = files.length > maxFiles ? `
\u2026+${files.length - maxFiles} more` : "";
  return { output: `${files.length} files need formatting:
${shown}${extra}`, strategy: "prettier:files" };
}
function filterBiome(raw, level) {
  const lines = raw.split("\n").filter((l) => l.trim());
  if (lines.length === 0) return { output: "ok", strategy: "biome:clean" };
  const errorLines = lines.filter((l) => /error|warning/i.test(l));
  if (errorLines.length === 0) return { output: "ok", strategy: "biome:clean" };
  const maxLines = level === "ultra" ? 5 : level === "aggressive" ? 10 : 20;
  return { output: errorLines.slice(0, maxLines).join("\n"), strategy: "biome:filtered" };
}
function filterGolangciLint(raw, level) {
  const lines = raw.split("\n").filter((l) => l.trim());
  if (lines.length === 0) return { output: "ok (no issues)", strategy: "golangci:clean" };
  const byLinter = /* @__PURE__ */ new Map();
  for (const line of lines) {
    const match = line.match(/\((\w+)\)\s*$/);
    if (match) byLinter.set(match[1], (byLinter.get(match[1]) || 0) + 1);
  }
  const total = [...byLinter.values()].reduce((s, c) => s + c, 0) || lines.length;
  if (level === "ultra") {
    const top = [...byLinter.entries()].sort((a, b) => b[1] - a[1]).slice(0, 5).map(([l, c]) => `${l}:${c}`).join(" ");
    return { output: `${total} issues: ${top}`, strategy: "golangci:ultra" };
  }
  const grouped = [...byLinter.entries()].sort((a, b) => b[1] - a[1]).map(([linter, count]) => `  ${linter}: ${count}`).join("\n");
  return { output: `${total} issues:
${grouped}`, strategy: "golangci:grouped" };
}
function filterRubocop(raw, level) {
  const lines = raw.split("\n").filter((l) => l.trim());
  const summaryMatch = raw.match(/(\d+)\s+offenses?\s+detected/);
  if (!summaryMatch || summaryMatch[1] === "0") {
    return { output: "ok (no offenses)", strategy: "rubocop:clean" };
  }
  const total = parseInt(summaryMatch[1]);
  if (level === "ultra") {
    return { output: `${total} offenses`, strategy: "rubocop:ultra" };
  }
  const offenses = lines.filter((l) => l.includes(":") && /[CWE]:/.test(l));
  const maxLines = level === "aggressive" ? 10 : 20;
  const shown = offenses.slice(0, maxLines).join("\n");
  const extra = offenses.length > maxLines ? `
\u2026+${offenses.length - maxLines} more` : "";
  return { output: `${total} offenses:
${shown}${extra}`, strategy: "rubocop:filtered" };
}
function filterNextBuild(raw, level) {
  const lines = raw.split("\n");
  if (raw.includes("Compiled successfully") || raw.includes("\u2713 Compiled")) {
    const routeLines = lines.filter((l) => l.includes("\u25CB") || l.includes("\u25CF") || l.includes("\u03BB"));
    if (level === "ultra") {
      return { output: `ok (${routeLines.length} routes)`, strategy: "next-build:ultra" };
    }
    const maxRoutes = level === "aggressive" ? 10 : 20;
    const shown = routeLines.slice(0, maxRoutes).join("\n");
    return { output: `Build successful (${routeLines.length} routes):
${shown}`, strategy: "next-build:success" };
  }
  const errorLines = lines.filter((l) => /error/i.test(l) || l.includes("Failed"));
  const maxLines = level === "ultra" ? 5 : level === "aggressive" ? 10 : 20;
  return { output: errorLines.slice(0, maxLines).join("\n") || raw, strategy: "next-build:errors" };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  lintFilter
});
//# sourceMappingURL=lint.js.map
