// src/actions/deplint/reporters.ts
import chalk from "chalk";
function pluralize(word, count) {
  return `${word}${count === 1 ? "" : "s"}`;
}
function formatWorkspaceList(workspaces) {
  const names = workspaces.map((workspace) => workspace.name);
  if (names.length <= 5) return names.join(", ");
  return `${names.slice(0, 5).join(", ")} +${names.length - 5} more`;
}
function groupedDiagnostics(diagnostics) {
  const workspaces = /* @__PURE__ */ new Map();
  for (const diagnostic of diagnostics) {
    const key = diagnostic.workspace.location;
    let workspaceGroup = workspaces.get(key);
    if (!workspaceGroup) {
      workspaceGroup = {
        diagnostics: 0,
        rules: /* @__PURE__ */ new Map(),
        workspace: diagnostic.workspace
      };
      workspaces.set(key, workspaceGroup);
    }
    workspaceGroup.diagnostics += 1;
    const rules = workspaceGroup.rules;
    const ruleDiagnostics = rules.get(diagnostic.id) ?? [];
    ruleDiagnostics.push(diagnostic);
    rules.set(diagnostic.id, ruleDiagnostics);
  }
  return [...workspaces.values()];
}
function createHumanDeplintProgressReporter({ verbose = false } = {}) {
  return (event) => renderHumanDeplintProgressEvent(event, verbose);
}
function renderHumanDeplintProgressEvent(event, verbose) {
  switch (event.type) {
    case "run-start": {
      renderRunStart(event);
      break;
    }
    case "workspace-start": {
      renderWorkspaceHeader(event);
      if (verbose) renderVerboseWorkspaceDetails(event);
      break;
    }
    case "fix-start": {
      renderFixStart(event);
      break;
    }
    case "fix-end": {
      renderFixEnd(event);
      break;
    }
    default: {
      if (verbose) renderVerboseProgressEvent(event);
    }
  }
}
function renderRunStart(event) {
  const categories = event.activeCategories.join(", ");
  const fixDetail = event.fix ? ` with --fix (max ${event.maxFixPasses} fix ${pluralize("pass", event.maxFixPasses)})` : "";
  console.info(`Deplint: checking ${event.workspaces.length} ${pluralize("workspace", event.workspaces.length)} [${categories}]${fixDetail}`);
  console.info(`Workspaces: ${formatWorkspaceList(event.workspaces)}`);
}
function renderFixStart(event) {
  console.info(
    `Deplint: fix pass ${event.fixPass} resolving ${event.diagnostics} ${pluralize("diagnostic", event.diagnostics)}`
  );
}
function renderFixEnd(event) {
  console.info(
    `Deplint: fix pass ${event.fixPass} applied ${event.appliedFixes} ${pluralize("fix", event.appliedFixes)}`
  );
}
function renderVerboseProgressEvent(event) {
  switch (event.type) {
    case "pass-start": {
      console.info(chalk.dim(`Deplint pass ${event.pass}: evaluating ${event.workspaces.length} ${pluralize("workspace", event.workspaces.length)}`));
      break;
    }
    case "pass-end": {
      console.info(chalk.dim(
        `Deplint pass ${event.pass}: ${event.diagnostics} ${pluralize("diagnostic", event.diagnostics)}, ${event.rulesEvaluated} ${pluralize("rule", event.rulesEvaluated)} evaluated, ${event.rulesSkipped} skipped`
      ));
      break;
    }
    case "workspace-end": {
      console.info(chalk.dim(
        `[${event.workspace.name}] pass ${event.pass}: ${event.diagnostics} ${pluralize("diagnostic", event.diagnostics)}`
      ));
      break;
    }
    case "rule-start": {
      console.info(chalk.dim(`  rule ${event.ruleIndex}/${event.ruleTotal}: ${event.rule.id}`));
      break;
    }
    case "rule-end": {
      console.info(chalk.dim(`    ${event.diagnostics} ${pluralize("finding", event.diagnostics)} in ${event.elapsedMs}ms`));
      break;
    }
    case "rule-skip": {
      console.info(chalk.dim(`  skip ${event.ruleIndex}/${event.ruleTotal}: ${event.rule.id} (${event.reason})`));
      break;
    }
  }
}
function renderWorkspaceHeader(event) {
  console.info(
    `[${chalk.blue(event.workspace.name)}] package ${event.workspaceIndex}/${event.workspaceTotal}: ${event.workspace.location} (${event.context.packageKind})`
  );
}
function renderVerboseWorkspaceDetails(event) {
  const forcedRefs = event.context.forceDeps + event.context.forceDevs + event.context.forcePeers + event.context.forcePeerOptional;
  console.info(chalk.dim(
    `  files: ${event.context.allFiles} all, ${event.context.distFiles} dist; deps: ${event.context.dependencies}, dev: ${event.context.devDependencies}, peer: ${event.context.peerDependencies}; exclude: ${event.context.excludeCount}, forced refs: ${forcedRefs}`
  ));
}
var humanDeplintReporter = {
  render(result) {
    for (const fix of result.appliedFixes) {
      console.log(`[${chalk.blue(fix.workspace.name)}] Fixed: ${fix.detail} ${chalk.green(fix.dependency)}`);
    }
    for (const workspaceGroup of groupedDiagnostics(result.diagnostics)) {
      console.log(`[${chalk.blue(workspaceGroup.workspace.name)}] ${workspaceGroup.diagnostics} ${pluralize("problem", workspaceGroup.diagnostics)}`);
      for (const [ruleId, diagnostics] of workspaceGroup.rules) {
        console.log(`  ${chalk.cyan(ruleId)} (${diagnostics.length})`);
        for (const diagnostic of diagnostics) {
          console.log(`    ${diagnostic.message}`);
          if (diagnostic.fixable) {
            console.log(`      ${chalk.dim("fixable with `--fix`")}`);
          }
          for (const line of diagnostic.evidence ?? []) {
            console.log(`      ${line}`);
          }
          console.log(`      ${chalk.yellow(diagnostic.file)}`);
        }
      }
      console.log("");
    }
    if (result.summary.problemCount > 0) {
      const summaryColor = result.summary.errorCount > 0 ? chalk.red : chalk.yellow;
      const summaryMessage2 = [
        `\u2716 ${result.summary.problemCount} ${pluralize("problem", result.summary.problemCount)}`,
        `(${result.summary.errorCount} ${pluralize("error", result.summary.errorCount)},`,
        `${result.summary.warningCount} ${pluralize("warning", result.summary.warningCount)})`
      ].join(" ");
      console.warn(summaryColor(
        summaryMessage2
      ));
      if (result.summary.fixableErrorCount > 0 || result.summary.fixableWarningCount > 0) {
        const fixableMessage = [
          `  ${result.summary.fixableErrorCount} ${pluralize("error", result.summary.fixableErrorCount)}`,
          `and ${result.summary.fixableWarningCount} ${pluralize("warning", result.summary.fixableWarningCount)}`,
          "potentially fixable with the `--fix` option."
        ].join(" ");
        console.warn(summaryColor(
          fixableMessage
        ));
      }
      if (result.summary.diagnosticsByRule !== void 0) {
        const byRule = Object.entries(result.summary.diagnosticsByRule).map(([rule, count]) => `${rule}: ${count}`).join(", ");
        if (byRule.length > 0) console.warn(summaryColor(`  By rule: ${byRule}`));
      }
      if (result.summary.passesRun !== void 0 && result.summary.rulesEvaluated !== void 0) {
        console.warn(chalk.dim(
          `  Checked ${result.summary.workspacesChecked} ${pluralize("workspace", result.summary.workspacesChecked)} across ${result.summary.passesRun} ${pluralize("pass", result.summary.passesRun)}; ${result.summary.rulesEvaluated} ${pluralize("rule", result.summary.rulesEvaluated)} evaluated, ${result.summary.rulesSkipped ?? 0} skipped.`
        ));
      }
      return;
    }
    const detail = result.summary.passesRun === void 0 ? "" : chalk.dim(
      ` Checked ${result.summary.workspacesChecked} ${pluralize("workspace", result.summary.workspacesChecked)} across ${result.summary.passesRun} ${pluralize("pass", result.summary.passesRun)}.`
    );
    const summaryMessage = `(${result.summary.errorCount} ${pluralize("error", result.summary.errorCount)}, ${result.summary.warningCount} ${pluralize("warning", result.summary.warningCount)})`;
    console.info(`Deplint: Found no dependency problems. ${chalk.green("\u2714")} ${summaryMessage}${detail}`);
  }
};
var jsonDeplintReporter = {
  render(result) {
    console.log(JSON.stringify(result, null, 2));
  }
};
function getDeplintReporter(format) {
  return format === "json" ? jsonDeplintReporter : humanDeplintReporter;
}
export {
  createHumanDeplintProgressReporter,
  getDeplintReporter,
  humanDeplintReporter,
  jsonDeplintReporter
};
//# sourceMappingURL=reporters.mjs.map
