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

// src/actions/deplint/packageJsonEditor.ts
import fs2 from "node:fs";
import chalk from "chalk";
import semver from "semver";
import sortPackageJson from "sort-package-json";

// src/actions/deplint/getRequiredPeerDependencies.ts
import fs from "node:fs";
import path from "node: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 readInstalledVersion(location, dep) {
  const depPkgPath = findDepPackageJson(location, dep);
  if (!(depPkgPath != null && depPkgPath.length > 0)) return void 0;
  try {
    const depPkg = JSON.parse(fs2.readFileSync(depPkgPath, "utf8"));
    return depPkg.version;
  } catch {
    return void 0;
  }
}
function resolveDepVersion(location, dep) {
  const version = readInstalledVersion(location, dep);
  return version != null && version.length > 0 ? `~${version}` : "*";
}
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/getWorkspaceVersionProblems.ts
function checkSection(deps, versions, section, workspaceNames, problems, expectedVersion) {
  for (const dep of deps) {
    if (!workspaceNames.has(dep)) continue;
    const version = versions[dep];
    const isInvalidVersion = expectedVersion === void 0 ? version !== void 0 && !version.startsWith("workspace:") : version !== expectedVersion;
    if (version !== void 0 && isInvalidVersion) {
      problems.push({
        dep,
        expectedVersion,
        section,
        version
      });
    }
  }
}
function findWorkspaceVersionProblems(packageParams, workspaceNames, expectedVersion) {
  const problems = [];
  checkSection(packageParams.dependencies, packageParams.dependencyVersions, "dependencies", workspaceNames, problems, expectedVersion);
  checkSection(packageParams.devDependencies, packageParams.devDependencyVersions, "devDependencies", workspaceNames, problems, expectedVersion);
  checkSection(packageParams.peerDependencies, packageParams.peerDependencyVersions, "peerDependencies", workspaceNames, problems, expectedVersion);
  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 (!semver2.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, expectedVersion = "workspace:~") {
  const problems = findWorkspaceVersionProblems(packageParams, workspaceNames, expectedVersion);
  if (problems.length === 0) return 0;
  const pkg = readPackageJson(workspace.location);
  for (const { dep, section } of problems) {
    addDep(pkg, dep, expectedVersion, section);
    logFix(workspace, `set ${expectedVersion} for ${section}`, dep);
  }
  writePackageJson(workspace.location, pkg);
  return problems.length;
}
export {
  findWorkspacePeerVersionProblems,
  findWorkspaceVersionProblems,
  fixWorkspaceVersionProblems,
  getWorkspaceVersionProblems
};
//# sourceMappingURL=getWorkspaceVersionProblems.mjs.map
