// src/actions/deplint/checkPackage/getMismatchedPeerDevVersions.ts
import chalk2 from "chalk";
import semver2 from "semver";

// src/actions/deplint/packageJsonEditor.ts
import fs from "node:fs";
import chalk from "chalk";
import semver from "semver";
import sortPackageJson from "sort-package-json";
function readPackageJson(location) {
  return JSON.parse(fs.readFileSync(`${location}/package.json`, "utf8"));
}
function writePackageJson(location, pkg) {
  const sorted = sortPackageJson(JSON.stringify(pkg, null, 2));
  fs.writeFileSync(`${location}/package.json`, `${sorted}
`);
}

// src/actions/deplint/checkPackage/getMismatchedPeerDevVersions.ts
function findMismatchedPeerDevVersions({
  peerDependencies,
  peerDependencyVersions,
  devDependencyVersions
}, exclude) {
  const mismatches = [];
  for (const dep of peerDependencies) {
    if (exclude?.has(dep)) continue;
    const peerVersion = peerDependencyVersions[dep];
    const devVersion = devDependencyVersions[dep];
    const hasWorkspaceProtocol = devVersion?.startsWith("workspace:") || peerVersion?.startsWith("workspace:");
    if (devVersion && peerVersion !== devVersion && !hasWorkspaceProtocol && !semver2.subset(devVersion, peerVersion)) {
      mismatches.push({
        dep,
        devVersion,
        peerVersion
      });
    }
  }
  return mismatches;
}
function getMismatchedPeerDevVersions({ name, location }, packageParams, exclude) {
  const mismatches = findMismatchedPeerDevVersions(packageParams, exclude);
  for (const {
    dep,
    peerVersion,
    devVersion
  } of mismatches) {
    console.log(
      `[${chalk2.blue(name)}] Peer/dev version mismatch ${chalk2.red(dep)} \u2014 peer: ${chalk2.yellow(peerVersion)}, dev: ${chalk2.cyan(devVersion)}`
    );
  }
  if (mismatches.length > 0) {
    console.log(`  ${chalk2.yellow(`${location}/package.json`)}
`);
  }
  return mismatches.length;
}
function fixMismatchedPeerDevVersions(workspace, packageParams, exclude) {
  const mismatches = findMismatchedPeerDevVersions(packageParams, exclude);
  if (mismatches.length === 0) return 0;
  const pkg = readPackageJson(workspace.location);
  for (const { dep, devVersion } of mismatches) {
    if (pkg.peerDependencies?.[dep]) {
      pkg.peerDependencies[dep] = devVersion;
      console.log(
        `[${chalk2.blue(workspace.name)}] Fixed: synced peerDependency ${chalk2.green(dep)} to devDependency version ${chalk2.cyan(devVersion)}`
      );
    }
  }
  writePackageJson(workspace.location, pkg);
  return mismatches.length;
}
export {
  findMismatchedPeerDevVersions,
  fixMismatchedPeerDevVersions,
  getMismatchedPeerDevVersions
};
//# sourceMappingURL=getMismatchedPeerDevVersions.mjs.map
