// src/actions/deplint/packageJsonEditor.ts
import fs2 from "fs";
import chalk from "chalk";
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 resolveDepVersion(location, dep) {
  const depPkgPath = findDepPackageJson(location, dep);
  if (depPkgPath) {
    try {
      const depPkg = JSON.parse(fs2.readFileSync(depPkgPath, "utf8"));
      const version = depPkg.version;
      if (version) return `~${version}`;
    } catch {
    }
  }
  return "*";
}

// 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 diagnostic(context, id, message, dependency, evidence, fixes) {
  return {
    dependency,
    evidence,
    file: context.packageJsonPath,
    fixes,
    id,
    message,
    severity: "error",
    workspace: context.workspace
  };
}
function resolvedAddFix(context, dependency, section, detail) {
  return {
    action: "add",
    dependency,
    detail,
    section,
    version: resolveDepVersion(context.workspace.location, dependency)
  };
}

// src/actions/deplint/rulesMisplaced.ts
var misplacedDependenciesRule = {
  category: "dependencies",
  id: "deplint.dependencies.misplaced",
  evaluate(context) {
    const diagnostics = [];
    for (const dep of context.packageParams.dependencies) {
      if (context.forceDevs.has(dep)) {
        diagnostics.push(diagnostic(
          context,
          "deplint.dependencies.misplaced",
          `dependency ${dep} should be a devDependency \u2014 configured as refType "dev"`,
          dep,
          void 0,
          [{
            action: "move",
            dependency: dep,
            detail: "moved from dependencies to devDependencies",
            fromSection: "dependencies",
            toSection: "devDependencies"
          }]
        ));
        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 fixes = [{
          action: "move",
          dependency: dep,
          detail: "moved from dependencies to peerDependencies",
          fromSection: "dependencies",
          toSection: "peerDependencies"
        }];
        if (!context.packageParams.devDependencies.includes(dep)) {
          fixes.push(resolvedAddFix(context, dep, "devDependencies", "added to devDependencies"));
        }
        const reason = context.forcePeers.has(dep) ? 'configured as refType "peer"' : "external runtime dep \u2014 consumers should control the version";
        diagnostics.push(diagnostic(
          context,
          "deplint.dependencies.misplaced",
          `dependency ${dep} should be a peerDependency \u2014 ${reason}`,
          dep,
          void 0,
          fixes
        ));
      }
    }
    return diagnostics;
  }
};
var misplacedPeerDependenciesRule = {
  category: "peerDependencies",
  id: "deplint.peerDependencies.misplaced",
  evaluate(context) {
    const diagnostics = [];
    for (const dep of context.packageParams.peerDependencies) {
      if (context.forceDeps.has(dep)) {
        diagnostics.push(diagnostic(
          context,
          "deplint.peerDependencies.misplaced",
          `peerDependency ${dep} should be a dependency \u2014 configured as refType "dep"`,
          dep,
          void 0,
          [{
            action: "move",
            dependency: dep,
            detail: "moved from peerDependencies to dependencies",
            fromSection: "peerDependencies",
            toSection: "dependencies",
            version: context.packageParams.peerDependencyVersions[dep] ?? "*"
          }]
        ));
        continue;
      }
      if (context.forceDevs.has(dep)) {
        diagnostics.push(diagnostic(
          context,
          "deplint.peerDependencies.misplaced",
          `peerDependency ${dep} should be a devDependency \u2014 configured as refType "dev"`,
          dep,
          void 0,
          [{
            action: "move",
            dependency: dep,
            detail: "moved from peerDependencies to devDependencies",
            fromSection: "peerDependencies",
            toSection: "devDependencies",
            version: context.packageParams.peerDependencyVersions[dep] ?? "*"
          }]
        ));
      }
    }
    return diagnostics;
  }
};
export {
  misplacedDependenciesRule,
  misplacedPeerDependenciesRule
};
//# sourceMappingURL=rulesMisplaced.mjs.map