// src/actions/deplint/checkPackage/getUnusedPeerDependencies.ts
import chalk2 from "chalk";

// 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}
`);
}
function removeDep(pkg, dep, section) {
  const sectionObj = pkg[section];
  if (!sectionObj || !Object.hasOwn(sectionObj, dep)) return;
  delete sectionObj[dep];
  if (Object.keys(sectionObj).length === 0) {
    delete pkg[section];
  }
  if (section === "peerDependencies") {
    removePeerMeta(pkg, dep);
  }
}
function removePeerMeta(pkg, dep) {
  const meta = pkg.peerDependenciesMeta;
  if (!meta || !Object.hasOwn(meta, dep)) return;
  delete meta[dep];
  if (Object.keys(meta).length === 0) {
    delete pkg.peerDependenciesMeta;
  }
}
function logFix(workspace, action, dep, detail) {
  const detailStr = detail != null && detail.length > 0 ? ` (${detail})` : "";
  console.log(`[${chalk.blue(workspace.name)}] Fixed: ${action} ${chalk.green(dep)}${detailStr}`);
}

// src/actions/deplint/checkPackage/getUnusedPeerDependencies.ts
function isPeerUsed(dep, externalDistImports, externalDistTypeImports) {
  const baseName = dep.replace(/^@types\//, "");
  return externalDistImports.includes(dep) || externalDistImports.includes(baseName) || externalDistTypeImports.includes(dep) || externalDistTypeImports.includes(baseName);
}
function findUnusedPeerDeps(peerDependencies, externalDistImports, externalDistTypeImports, exclude) {
  const unused = [];
  for (const dep of peerDependencies) {
    if (exclude?.has(dep) ?? false) continue;
    if (!isPeerUsed(dep, externalDistImports, externalDistTypeImports)) {
      unused.push(dep);
    }
  }
  return unused;
}
function getUnusedPeerDependencies({ name, location }, { peerDependencies, dependencies }, { externalDistImports, externalDistTypeImports }, exclude) {
  const unused = findUnusedPeerDeps(peerDependencies, externalDistImports, externalDistTypeImports, exclude);
  for (const dep of unused) {
    if (dependencies.includes(dep)) {
      console.log(`[${chalk2.blue(name)}] Unused peerDependency [already a dependency] in package.json: ${chalk2.red(dep)}`);
    } else {
      console.log(`[${chalk2.blue(name)}] Unused peerDependency in package.json: ${chalk2.red(dep)}`);
    }
  }
  if (unused.length > 0) {
    console.log(`  ${chalk2.yellow(`${location}/package.json`)}
`);
  }
  return unused.length;
}
function fixUnusedPeerDependencies(workspace, { peerDependencies }, { externalDistImports, externalDistTypeImports }, exclude) {
  const unused = findUnusedPeerDeps(peerDependencies, externalDistImports, externalDistTypeImports, exclude);
  if (unused.length === 0) return 0;
  const pkg = readPackageJson(workspace.location);
  for (const dep of unused) {
    removeDep(pkg, dep, "peerDependencies");
    logFix(workspace, "removed from peerDependencies", dep);
  }
  writePackageJson(workspace.location, pkg);
  return unused.length;
}
export {
  findUnusedPeerDeps,
  fixUnusedPeerDependencies,
  getUnusedPeerDependencies
};
//# sourceMappingURL=getUnusedPeerDependencies.mjs.map
