// 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 resolveExpectedPeerVersion(location, dep, currentRange) {
  const depPkgPath = findDepPackageJson(location, dep);
  if (depPkgPath) {
    try {
      const depPkg = JSON.parse(fs2.readFileSync(depPkgPath, "utf8"));
      const version = depPkg.version;
      if (version) {
        const match = /^(\d+)\.(\d+)/.exec(version);
        if (match) {
          if (currentRange?.startsWith("^")) {
            return `^${match[1]}`;
          }
          return `~${match[1]}.${match[2]}`;
        }
        return `~${version}`;
      }
    } catch {
    }
  }
  return "*";
}

// src/actions/deplint/versionMismatchFixes.ts
function computeBumpedRange(installedVersion, currentRange) {
  const match = /^(\d+)\.(\d+)/.exec(installedVersion);
  if (!match) return void 0;
  if (currentRange.startsWith("^")) return `^${match[1]}`;
  return `~${match[1]}.${match[2]}`;
}
function sectionLookupsFor(context) {
  const {
    dependencies,
    devDependencies,
    peerDependencies,
    dependencyVersions,
    devDependencyVersions,
    peerDependencyVersions
  } = context.packageParams;
  return [
    ["dependencies", dependencies, dependencyVersions],
    ["devDependencies", devDependencies, devDependencyVersions],
    ["peerDependencies", peerDependencies, peerDependencyVersions]
  ];
}
function addSourceBumpFixes(context, finding, fixes) {
  if (!finding.installedVersion || !finding.mismatchedSources) return;
  const sectionLookups = sectionLookupsFor(context);
  const seen = /* @__PURE__ */ new Set();
  for (const source of finding.mismatchedSources) {
    const dep = source.requiredBy;
    if (seen.has(dep) || dep === context.workspace.name) continue;
    seen.add(dep);
    for (const [section, names, versions] of sectionLookups) {
      if (!names.includes(dep)) continue;
      const current = versions[dep];
      if (!current || current.startsWith("workspace:")) continue;
      const bumped = computeBumpedRange(finding.installedVersion, current);
      if (!bumped || bumped === current) continue;
      fixes.push({
        action: "set-version",
        dependency: dep,
        detail: `bumped ${section} range to ${bumped} so a newer version is installed that accepts peer ${finding.peer}@${finding.installedVersion}`,
        section,
        version: bumped
      });
    }
  }
}
function generateVersionMismatchFixes(context, finding) {
  const fixes = [];
  const expectedVersion = resolveExpectedPeerVersion(
    context.workspace.location,
    finding.peer,
    finding.versionRange
  );
  if (expectedVersion !== "*") {
    if (context.packageParams.peerDependencies.includes(finding.peer)) {
      fixes.push({
        action: "set-version",
        dependency: finding.peer,
        detail: `updated peerDependency version to match installed (${finding.installedVersion})`,
        section: "peerDependencies",
        version: expectedVersion
      });
    }
    if (context.packageParams.devDependencies.includes(finding.peer)) {
      const devVersion = context.packageParams.devDependencyVersions[finding.peer];
      if (devVersion && !devVersion.startsWith("workspace:")) {
        fixes.push({
          action: "set-version",
          dependency: finding.peer,
          detail: `updated devDependency version to match installed (${finding.installedVersion})`,
          section: "devDependencies",
          version: expectedVersion
        });
      }
    }
  }
  addSourceBumpFixes(context, finding, fixes);
  return fixes;
}
export {
  generateVersionMismatchFixes
};
//# sourceMappingURL=versionMismatchFixes.mjs.map