// src/lib/updo/collectWorkspaceDeps.ts
import { readFileSync } from "fs";
import PATH from "path";
var DEP_FIELDS = ["dependencies", "devDependencies", "peerDependencies"];
function collectWorkspaceDeps(cwd, workspaces, workspaceNames) {
  const depMap = /* @__PURE__ */ new Map();
  for (const ws of workspaces) {
    const pkgPath = PATH.resolve(cwd, ws.location, "package.json");
    let pkg;
    try {
      pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
    } catch {
      continue;
    }
    for (const depField of DEP_FIELDS) {
      const deps = pkg[depField];
      if (!deps) continue;
      for (const [name, range] of Object.entries(deps)) {
        if (workspaceNames.has(name)) continue;
        if (range.startsWith("workspace:")) continue;
        const existing = depMap.get(name);
        if (existing) {
          if (!existing.workspaces.includes(ws.name)) {
            existing.workspaces.push(ws.name);
          }
          if (!existing.depTypes.includes(depField)) {
            existing.depTypes.push(depField);
          }
          if (!existing.ranges.includes(range)) {
            existing.ranges.push(range);
          }
        } else {
          depMap.set(name, {
            depTypes: [depField],
            name,
            ranges: [range],
            workspaces: [ws.name]
          });
        }
      }
    }
  }
  return depMap;
}
export {
  collectWorkspaceDeps
};
//# sourceMappingURL=collectWorkspaceDeps.mjs.map