// src/actions/dead/reporters.ts
import chalk from "chalk";

// src/actions/dead/ruleNames.ts
function deadRuleShortName(ruleId) {
  const parts = ruleId.split(".");
  return parts.slice(2).join(".");
}

// src/actions/dead/reporters.ts
function groupBy(items, keyFn) {
  const out = /* @__PURE__ */ new Map();
  for (const item of items) {
    const key = keyFn(item);
    let bucket = out.get(key);
    if (!bucket) {
      bucket = [];
      out.set(key, bucket);
    }
    bucket.push(item);
  }
  return out;
}
function renderFindingLabel(finding) {
  return `${finding.scope}/${deadRuleShortName(finding.rule)}`;
}
function renderDeprecatedColumn(finding) {
  return finding.deprecated === true ? chalk.gray("(deprecated)") : "";
}
function renderFinding(finding) {
  const loc = `${finding.file}:${finding.line}:${finding.column}`;
  const label = chalk.yellow(renderFindingLabel(finding).padEnd(28));
  const symbolCol = finding.symbol.padEnd(28);
  const deprecatedCol = renderDeprecatedColumn(finding).padEnd(14);
  const detail = finding.detail != null && finding.detail.length > 0 ? chalk.gray(`  (${finding.detail})`) : "";
  console.log(`  ${loc.padEnd(40)} ${label} ${symbolCol}${deprecatedCol}${detail}`);
}
function renderFindingsByPackage(findings) {
  const byPkg = groupBy(findings, (f) => f.pkg);
  for (const [pkg, packageFindings] of byPkg) {
    console.log(chalk.green(`Dead Code [${pkg}]`));
    packageFindings.sort((a, b) => a.file.localeCompare(b.file) || a.line - b.line || a.column - b.column);
    for (const finding of packageFindings) renderFinding(finding);
  }
}
function renderDisabledWorkspaceRules(disabledWorkspaceRules) {
  if (disabledWorkspaceRules.length === 0) return;
  console.log(chalk.gray(`  workspace rules disabled (no code-workspace): ${disabledWorkspaceRules.join(", ")}`));
}
function renderSummaryFooter(result) {
  const { summary } = result;
  const byScope = groupBy(result.findings, (f) => f.scope);
  console.log(chalk.gray(`
  scanned ${summary.scannedFiles} files in ${summary.scannedPackages} package${summary.scannedPackages === 1 ? "" : "s"}`));
  const parts = [...byScope].map(([scope, scopedFindings]) => `${scopedFindings.length} ${scope}`);
  if (parts.length > 0) console.log(chalk.yellow(`  ${parts.join(", ")}`));
  if (summary.errorCount > 0 || summary.warningCount > 0) {
    console.log(chalk.gray(`  ${summary.errorCount} error${summary.errorCount === 1 ? "" : "s"}, ${summary.warningCount} warning${summary.warningCount === 1 ? "" : "s"}`));
  }
  if ((summary.fixApplied ?? 0) > 0) {
    const mode = summary.fixMode === "remove" ? "removed" : "deprecated";
    console.log(chalk.green(`  ${mode} ${summary.fixApplied} symbol${summary.fixApplied === 1 ? "" : "s"}`));
  }
  if (summary.workspaceFile != null && summary.workspaceFile.length > 0) {
    console.log(chalk.gray(`  workspace: ${summary.workspaceFile}`));
  }
  renderDisabledWorkspaceRules(summary.disabledWorkspaceRules);
}
function renderHuman(result) {
  if (result.findings.length === 0) {
    console.log(chalk.green(`Dead Code: no findings (${result.summary.scannedFiles} files in ${result.summary.scannedPackages} package${result.summary.scannedPackages === 1 ? "" : "s"})`));
    renderDisabledWorkspaceRules(result.summary.disabledWorkspaceRules);
    return;
  }
  renderFindingsByPackage(result.findings);
  renderSummaryFooter(result);
}
function getDeadReporter(format) {
  if (format === "json") {
    return {
      render(result) {
        console.log(JSON.stringify(result, null, 2));
      }
    };
  }
  return { render: renderHuman };
}
export {
  getDeadReporter
};
//# sourceMappingURL=reporters.mjs.map
