// src/actions/lintTierAnalysis.ts
import { readFileSync } from "node:fs";
import { createRequire } from "node:module";
import path from "node:path";
import { pathToFileURL } from "node:url";
import chalk from "chalk";
import { findUpSync } from "find-up";
var tierExportPattern = /config(?:React)?Tier\d+[A-Za-z]*/g;
function normalizeRuleLevel(setting) {
  let current = setting;
  while (Array.isArray(current) && current.length > 0) {
    current = current[0];
  }
  if (current === void 0) return "off";
  if (current === "off" || current === 0) return "off";
  if (current === "warn" || current === 1) return "warn";
  if (current === "error" || current === 2) return "error";
  return "off";
}
function countMessagesForTier(messages, rules) {
  let errors = 0;
  let warnings = 0;
  for (const message of messages) {
    if (message.ruleId === null) {
      errors++;
      continue;
    }
    const level = normalizeRuleLevel(rules[message.ruleId]);
    if (level === "off") continue;
    if (level === "error") errors++;
    else warnings++;
  }
  return { errors, warnings };
}
function getLintTierDefinitions(isReact, isTypeChecked) {
  if (isReact) {
    return [
      {
        id: 0,
        label: "Correctness",
        exportName: isTypeChecked ? "configReactTier0CorrectnessTypeChecked" : "configReactTier0Correctness"
      },
      {
        id: 1,
        label: "Consistency",
        exportName: isTypeChecked ? "configReactTier1ConsistencyTypeChecked" : "configReactTier1Consistency"
      },
      {
        id: 2,
        label: "Best Practices",
        exportName: isTypeChecked ? "configReactTier2BestPracticesTypeChecked" : "configReactTier2BestPractices"
      },
      {
        id: 3,
        label: "Opinionated",
        exportName: "configReactTier3OpinionatedTypeChecked"
      },
      {
        id: 4,
        label: "Experimental",
        exportName: isTypeChecked ? "configReactTier4ExperimentalTypeChecked" : "configReactTier4Experimental"
      }
    ];
  }
  return [
    {
      id: 0,
      label: "Correctness",
      exportName: isTypeChecked ? "configTier0CorrectnessTypeChecked" : "configTier0Correctness"
    },
    {
      id: 1,
      label: "Consistency",
      exportName: isTypeChecked ? "configTier1ConsistencyTypeChecked" : "configTier1Consistency"
    },
    {
      id: 2,
      label: "Best Practices",
      exportName: isTypeChecked ? "configTier2BestPracticesTypeChecked" : "configTier2BestPractices"
    },
    {
      id: 3,
      label: "Opinionated",
      exportName: isTypeChecked ? "configTier3OpinionatedTypeChecked" : "configTier3Opinionated"
    },
    {
      id: 4,
      label: "Experimental",
      exportName: isTypeChecked ? "configTier4ExperimentalTypeChecked" : "configTier4Experimental"
    }
  ];
}
function detectLintTierContext(cwd) {
  const configPath = findUpSync("eslint.config.ts", { cwd });
  if (configPath === void 0) {
    return {
      hasXylabsConfig: false,
      isReact: false,
      isTypeChecked: true
    };
  }
  const source = readFileSync(configPath, "utf8");
  const hasXylabsConfig = source.includes("@xylabs/eslint-config-flat") || source.includes("@xylabs/eslint-config-react-flat");
  const isReact = source.includes("@xylabs/eslint-config-react-flat");
  const isTypeChecked = source.includes("TypeChecked");
  const tierMatches = source.match(tierExportPattern) ?? [];
  const currentTierExport = tierMatches.at(-1);
  return {
    currentTierExport,
    hasXylabsConfig,
    isReact,
    isTypeChecked
  };
}
async function loadXylabsEslintConfigPackage(cwd, isReact) {
  const packageName = isReact ? "@xylabs/eslint-config-react-flat" : "@xylabs/eslint-config-flat";
  const requireFromCwd = createRequire(path.join(cwd, "package.json"));
  const resolved = requireFromCwd.resolve(packageName);
  return import(pathToFileURL(resolved).href);
}
function resolveTierConfigExport(configPackage, exportName) {
  const configs = configPackage[exportName];
  if (!Array.isArray(configs)) {
    throw new TypeError(`Missing ESLint tier export "${exportName}" from @xylabs/eslint-config`);
  }
  return configs;
}
function mergeTierRules(mergeConfigRules, configs) {
  return mergeConfigRules(configs);
}
async function loadMergeConfigRules(cwd) {
  const flatPackage = await loadXylabsEslintConfigPackage(cwd, false);
  const mergeConfigRules = flatPackage.mergeConfigRules;
  if (typeof mergeConfigRules !== "function") {
    throw new TypeError("mergeConfigRules is missing from @xylabs/eslint-config-flat");
  }
  return mergeConfigRules;
}
async function buildLintTierRuleMaps(cwd, context) {
  const configPackage = await loadXylabsEslintConfigPackage(cwd, context.isReact);
  const mergeConfigRules = await loadMergeConfigRules(cwd);
  const definitions = getLintTierDefinitions(context.isReact, context.isTypeChecked);
  const maps = /* @__PURE__ */ new Map();
  for (const definition of definitions) {
    const configs = resolveTierConfigExport(configPackage, definition.exportName);
    maps.set(definition.id, mergeTierRules(mergeConfigRules, configs));
  }
  return maps;
}
function resolveMaxTierConfigExport(context) {
  return getLintTierDefinitions(context.isReact, context.isTypeChecked).at(-1)?.exportName ?? "configTier4ExperimentalTypeChecked";
}
function projectLintTierCounts(messages, tierRuleMaps, definitions) {
  return definitions.map((definition) => {
    const rules = tierRuleMaps.get(definition.id) ?? {};
    const { errors, warnings } = countMessagesForTier(messages, rules);
    return {
      errors,
      label: definition.label,
      tier: definition.id,
      warnings
    };
  });
}
function formatLintTierAnalysisReport({
  context,
  counts,
  currentTierExport,
  files,
  timeMs
}) {
  const profile = [
    context.isReact ? "react" : "flat",
    context.isTypeChecked ? "type-checked" : "untyped"
  ].join(", ");
  const labelWidth = Math.max(...counts.map((entry) => entry.label.length), "Experimental".length);
  const lines = [
    chalk.cyan("Lint tier analysis"),
    chalk.gray(`XY preset projection (${profile}); based on your eslint.config.ts with no extra ESLint passes`),
    "",
    ` ${"Tier".padEnd(4)} ${"Preset".padEnd(labelWidth)}  ${"Errors".padStart(6)}  ${"Warnings".padStart(8)}`,
    ` ${"-".repeat(4)} ${"-".repeat(labelWidth)}  ${"-".repeat(6)}  ${"-".repeat(8)}`
  ];
  for (const entry of counts) {
    const definition = getLintTierDefinitions(context.isReact, context.isTypeChecked).find((tier) => tier.id === entry.tier);
    const isCurrent = definition?.exportName === currentTierExport || currentTierExport === void 0 && entry.tier === 3;
    const marker = isCurrent ? chalk.gray("  <- current") : "";
    lines.push(
      ` ${String(entry.tier).padEnd(4)} ${entry.label.padEnd(labelWidth)}  ${String(entry.errors).padStart(6)}  ${String(entry.warnings).padStart(8)}${marker}`
    );
  }
  const currentTierId = getLintTierDefinitions(context.isReact, context.isTypeChecked).find((tier) => tier.exportName === currentTierExport)?.id;
  if (currentTierId !== void 0 && currentTierId < 4) {
    lines.push(
      "",
      chalk.yellow("Note: eslint.config.ts is below tier 4. Counts for higher tiers only include violations ESLint reported with your current config and may be understated.")
    );
  }
  lines.push(
    "",
    chalk.gray(`Analyzed ${files} files in ${timeMs < 1e3 ? `${timeMs}ms` : `${(timeMs / 1e3).toFixed(2)}s`}`)
  );
  return lines.join("\n");
}
export {
  buildLintTierRuleMaps,
  countMessagesForTier,
  detectLintTierContext,
  formatLintTierAnalysisReport,
  getLintTierDefinitions,
  loadMergeConfigRules,
  loadXylabsEslintConfigPackage,
  mergeTierRules,
  normalizeRuleLevel,
  projectLintTierCounts,
  resolveMaxTierConfigExport,
  resolveTierConfigExport
};
//# sourceMappingURL=lintTierAnalysis.mjs.map
