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

// 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 readPackageJson(location) {
  return JSON.parse(fs2.readFileSync(`${location}/package.json`, "utf8"));
}
function writePackageJson(location, pkg) {
  const sorted = sortPackageJson(JSON.stringify(pkg, null, 2));
  fs2.writeFileSync(`${location}/package.json`, `${sorted}
`);
}
function addDep(pkg, dep, version, section) {
  const sectionObj = pkg[section] ?? {};
  sectionObj[dep] = version;
  pkg[section] = sectionObj;
}
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 "*";
}
function logFix(workspace, action, dep, detail) {
  const detailStr = detail ? ` (${detail})` : "";
  console.log(`[${chalk.blue(workspace.name)}] Fixed: ${action} ${chalk.green(dep)}${detailStr}`);
}

// src/actions/deplint/checkPackage/getWorkspaceVersionProblems.ts
function checkSection(deps, versions, section, workspaceNames, problems) {
  for (const dep of deps) {
    if (!workspaceNames.has(dep)) continue;
    const version = versions[dep];
    if (version && !version.startsWith("workspace:")) {
      problems.push({
        dep,
        section,
        version
      });
    }
  }
}
function findWorkspaceVersionProblems(packageParams, workspaceNames) {
  const problems = [];
  checkSection(packageParams.dependencies, packageParams.dependencyVersions, "dependencies", workspaceNames, problems);
  checkSection(packageParams.devDependencies, packageParams.devDependencyVersions, "devDependencies", workspaceNames, problems);
  return problems;
}
function findWorkspacePeerVersionProblems(packageParams, workspaceNames, location) {
  const problems = [];
  for (const dep of packageParams.peerDependencies) {
    if (!workspaceNames.has(dep)) continue;
    const declaredRange = packageParams.peerDependencyVersions[dep];
    if (!declaredRange || declaredRange.startsWith("workspace:")) continue;
    const resolved = resolveDepVersion(location, dep);
    const actualVersion = resolved.startsWith("~") ? resolved.slice(1) : resolved;
    if (actualVersion === "*") continue;
    if (!semver.satisfies(actualVersion, declaredRange)) {
      problems.push({
        actualVersion,
        declaredRange,
        dep
      });
    }
  }
  return problems;
}
function getWorkspaceVersionProblems({ name, location }, packageParams, workspaceNames) {
  const problems = findWorkspaceVersionProblems(packageParams, workspaceNames);
  for (const {
    dep,
    section,
    version
  } of problems) {
    console.log(
      `[${chalk2.blue(name)}] Workspace dependency ${chalk2.red(dep)} in ${chalk2.cyan(section)} should use workspace: protocol \u2014 found ${chalk2.yellow(version)}`
    );
  }
  if (problems.length > 0) {
    console.log(`  ${chalk2.yellow(`${location}/package.json`)}
`);
  }
  return problems.length;
}
function fixWorkspaceVersionProblems(workspace, packageParams, workspaceNames) {
  const problems = findWorkspaceVersionProblems(packageParams, workspaceNames);
  if (problems.length === 0) return 0;
  const pkg = readPackageJson(workspace.location);
  for (const { dep, section } of problems) {
    addDep(pkg, dep, "workspace:~", section);
    logFix(workspace, `set workspace:~ for ${section}`, dep);
  }
  writePackageJson(workspace.location, pkg);
  return problems.length;
}
export {
  findWorkspacePeerVersionProblems,
  findWorkspaceVersionProblems,
  fixWorkspaceVersionProblems,
  getWorkspaceVersionProblems
};
//# sourceMappingURL=getWorkspaceVersionProblems.mjs.map