// src/lib/updo/renderTable.ts
import chalk from "chalk";
import semver from "semver";
function diffColor(from, to, fallback) {
  const fromV = semver.minVersion(from)?.version;
  const toV = semver.minVersion(to)?.version;
  if (!fromV || !toV) return fallback;
  const diff = semver.diff(fromV, toV);
  if (diff === "major" || diff === "premajor") return chalk.red;
  if (diff === "minor" || diff === "preminor") return chalk.yellow;
  return chalk.green;
}
function versionColor(version, current, mixedBaseline) {
  if (!version) return chalk.gray("-");
  if (current === "mixed") {
    if (!mixedBaseline || version === mixedBaseline) return chalk.green(version);
    return diffColor(mixedBaseline, version, chalk.green)(version);
  }
  if (!current || version === current) return chalk.gray(version);
  return diffColor(current, version, chalk.gray)(version);
}
var columns = [
  {
    color: (_v, dep) => dep.updateAvailable ? chalk.white(dep.name) : chalk.gray(dep.name),
    header: "Package",
    value: (dep) => dep.name
  },
  {
    color: (_v, dep) => chalk.gray(dep.current),
    header: "Current",
    value: (dep) => dep.current
  },
  {
    color: (_v, dep) => versionColor(dep.wanted, dep.current),
    header: "Wanted",
    value: (dep) => dep.wanted ?? "-"
  },
  {
    color: (_v, dep) => versionColor(dep.latest, dep.current, dep.wanted),
    header: "Latest",
    value: (dep) => dep.latest ?? "-"
  },
  {
    color: (_v, dep) => versionColor(dep.next, dep.current, dep.wanted),
    header: "Next/RC",
    value: (dep) => dep.next ?? "-"
  },
  {
    color: (_v, dep) => chalk.gray(`${dep.workspaces.length}`),
    header: "Packages",
    value: (dep) => `${dep.workspaces.length}`
  }
];
function renderTable(deps) {
  const updatable = deps.filter((d) => d.updateAvailable);
  if (updatable.length === 0) {
    console.log(chalk.green("\nAll packages are up to date!"));
    return;
  }
  const widths = columns.map((col) => {
    let maxData = 0;
    for (const dep of updatable) {
      maxData = Math.max(maxData, col.value(dep).length);
    }
    return Math.max(col.header.length, maxData);
  });
  const header = columns.map((col, i) => chalk.bold(col.header.padEnd(widths[i]))).join("  ");
  console.log(`
${header}`);
  let totalWidth = -2;
  for (const w of widths) {
    totalWidth += w + 2;
  }
  console.log(chalk.gray("\u2500".repeat(totalWidth)));
  for (const dep of updatable) {
    const row = columns.map((col, i) => {
      const raw = col.value(dep);
      const colored = col.color(raw, dep);
      const padding = widths[i] - raw.length;
      return colored + " ".repeat(Math.max(0, padding));
    }).join("  ");
    console.log(row);
  }
  console.log();
}
export {
  renderTable
};
//# sourceMappingURL=renderTable.mjs.map