// src/lib/updo/resolveVersions.ts
import semver from "semver";
function computeWanted(info, ranges) {
  const intersected = ranges.join(" ");
  return semver.maxSatisfying(info.versions, intersected) ?? void 0;
}
function computePrerelease(info, latest) {
  if (!info) return void 0;
  let best;
  for (const tag of ["next", "rc", "beta"]) {
    const v = info.distTags[tag];
    if (!v) continue;
    if (!best || semver.gt(v, best)) best = v;
  }
  if (!best) return void 0;
  if (latest && !semver.gt(best, latest)) return void 0;
  return best;
}
function hasNewerVersion(version, current) {
  return version !== void 0 && semver.gt(version, current);
}
function isUpdateAvailable(current, wanted, latest, next) {
  if (!current) return false;
  return hasNewerVersion(wanted, current) || hasNewerVersion(latest, current) || hasNewerVersion(next, current);
}
function resolveVersions(deps, registry, installedVersions) {
  const resolved = [];
  for (const [name, dep] of deps) {
    const info = registry.get(name);
    const current = installedVersions.get(name);
    const wanted = info ? computeWanted(info, dep.ranges) : void 0;
    const latest = info?.distTags.latest;
    const next = computePrerelease(info, latest);
    const updateAvailable = isUpdateAvailable(current, wanted, latest, next);
    resolved.push({
      current,
      declaredRange: dep.ranges.length === 1 ? dep.ranges[0] : dep.ranges.join(" | "),
      depTypes: dep.depTypes,
      latest,
      name,
      next,
      updateAvailable,
      wanted: wanted && current && wanted !== current ? wanted : void 0,
      workspaces: dep.workspaces
    });
  }
  return resolved.toSorted((a, b) => {
    if (a.updateAvailable !== b.updateAvailable) return a.updateAvailable ? -1 : 1;
    return a.name.localeCompare(b.name);
  });
}
export {
  resolveVersions
};
//# sourceMappingURL=resolveVersions.mjs.map