// src/actions/deplint/packageJsonEditor.ts
import fs2 from "fs";
import chalk from "chalk";
import semver from "semver";
import sortPackageJson from "sort-package-json";

// src/actions/deplint/getRequiredPeerDependencies.ts
import fs from "fs";
import path from "path";
function findDepPackageJson(location, dep) {
  let dir = location;
  while (true) {
    const candidate = path.join(dir, "node_modules", dep, "package.json");
    if (fs.existsSync(candidate)) return candidate;
    const parent = path.dirname(dir);
    if (parent === dir) return void 0;
    dir = parent;
  }
}

// src/actions/deplint/packageJsonEditor.ts
function formatPeerRange(version) {
  const parsed = semver.parse(version);
  if (!parsed) return `~${version}`;
  return `>=${parsed.version} <${parsed.major + 1}`;
}
function readInstalledVersion(location, dep) {
  const depPkgPath = findDepPackageJson(location, dep);
  if (!depPkgPath) return void 0;
  try {
    const depPkg = JSON.parse(fs2.readFileSync(depPkgPath, "utf8"));
    return depPkg.version;
  } catch {
    return void 0;
  }
}
function resolvePeerLowerBound(location, dep, devRange) {
  if (devRange && devRange !== "*" && !devRange.startsWith("workspace:") && !devRange.startsWith("catalog:") && !devRange.startsWith("npm:")) {
    try {
      const min = semver.minVersion(devRange);
      if (min) return min.version;
    } catch {
    }
  }
  return readInstalledVersion(location, dep);
}
function resolveDepVersion(location, dep) {
  const version = readInstalledVersion(location, dep);
  return version ? `~${version}` : "*";
}
function resolvePeerDepVersion(location, dep, devRange) {
  const lower = resolvePeerLowerBound(location, dep, devRange);
  return lower ? formatPeerRange(lower) : "*";
}

// src/actions/deplint/checkPackage/getUnlistedDependencies.ts
import { builtinModules } from "module";
import chalk2 from "chalk";
function shouldBePeerDependency(imp, _location, _currentPeers, _distImports, workspaceNames) {
  if (workspaceNames?.has(imp)) return false;
  if (imp.startsWith("@types/")) return false;
  return true;
}

// src/actions/deplint/rulesDiagnostic.ts
function resolvedAddFix(context, dependency, section, detail) {
  const devRange = context.packageParams.devDependencyVersions[dependency];
  const version = section === "peerDependencies" ? resolvePeerDepVersion(context.workspace.location, dependency, devRange) : resolveDepVersion(context.workspace.location, dependency);
  return {
    action: "add",
    dependency,
    detail,
    section,
    version
  };
}

// src/actions/deplint/rulesMisplaced.ts
var misplacedDependenciesRule = {
  category: "dependencies",
  id: "deplint.dependencies.misplaced",
  meta: {
    description: "Dependencies declared in the wrong package.json section",
    fixable: ["manifest"],
    severity: "error"
  },
  evaluate(context) {
    for (const dep of context.packageParams.dependencies) {
      if (context.forceDevs.has(dep)) {
        context.report({
          data: { dep, mode: "to-dev" },
          dependency: dep,
          message: `dependency ${dep} should be a devDependency \u2014 configured as refType "dev"`
        });
        continue;
      }
      if (context.isTerminalPackage) continue;
      if (context.forceDeps.has(dep)) continue;
      if (context.packageParams.dependencyVersions[dep]?.startsWith("workspace:")) continue;
      if (context.forcePeers.has(dep) || shouldBePeerDependency(dep, context.workspace.location, context.packageParams.peerDependencies, context.sourceParams.externalDistImports, context.workspaceNames)) {
        const reason = context.forcePeers.has(dep) ? 'configured as refType "peer"' : "external runtime dep \u2014 consumers should control the version";
        context.report({
          data: {
            dep,
            mode: "to-peer",
            needsDevCompanion: !context.packageParams.devDependencies.includes(dep)
          },
          dependency: dep,
          message: `dependency ${dep} should be a peerDependency \u2014 ${reason}`
        });
      }
    }
  },
  resolveFixes(diagnostic, context) {
    const data = diagnostic.data;
    if (!data) return [];
    if (data.mode === "to-dev") {
      return [{
        action: "move",
        dependency: data.dep,
        detail: "moved from dependencies to devDependencies",
        fromSection: "dependencies",
        toSection: "devDependencies"
      }];
    }
    const fixes = [{
      action: "move",
      dependency: data.dep,
      detail: "moved from dependencies to peerDependencies",
      fromSection: "dependencies",
      toSection: "peerDependencies"
    }];
    if (data.needsDevCompanion) {
      fixes.push(resolvedAddFix(context, data.dep, "devDependencies", "added to devDependencies"));
    }
    return fixes;
  }
};
var misplacedPeerDependenciesRule = {
  category: "peerDependencies",
  id: "deplint.peerDependencies.misplaced",
  meta: {
    description: "Peer dependencies declared in the wrong package.json section",
    fixable: ["manifest"],
    severity: "error"
  },
  evaluate(context) {
    for (const dep of context.packageParams.peerDependencies) {
      const version = context.packageParams.peerDependencyVersions[dep] ?? (context.workspaceNames?.has(dep) ? "workspace:~" : "*");
      if (context.forceDeps.has(dep)) {
        context.report({
          data: {
            dep,
            mode: "to-dep",
            version
          },
          dependency: dep,
          message: `peerDependency ${dep} should be a dependency \u2014 configured as refType "dep"`
        });
        continue;
      }
      if (context.forceDevs.has(dep)) {
        context.report({
          data: {
            dep,
            mode: "to-dev",
            version
          },
          dependency: dep,
          message: `peerDependency ${dep} should be a devDependency \u2014 configured as refType "dev"`
        });
        continue;
      }
      if (context.workspaceNames?.has(dep) && !context.forcePeers.has(dep)) {
        context.report({
          data: {
            dep,
            mode: "to-dep",
            version
          },
          dependency: dep,
          message: `peerDependency ${dep} should be a dependency \u2014 sibling workspace packages ship in lockstep; set refType "peer" in xy.config.ts to override`
        });
      }
    }
  },
  resolveFixes(diagnostic) {
    const data = diagnostic.data;
    if (!data) return [];
    return [{
      action: "move",
      dependency: data.dep,
      detail: data.mode === "to-dep" ? "moved from peerDependencies to dependencies" : "moved from peerDependencies to devDependencies",
      fromSection: "peerDependencies",
      toSection: data.mode === "to-dep" ? "dependencies" : "devDependencies",
      version: data.version
    }];
  }
};
export {
  misplacedDependenciesRule,
  misplacedPeerDependenciesRule
};
//# sourceMappingURL=rulesMisplaced.mjs.map