// src/lib/secure/renderSecurityAudit.ts
import chalk from "chalk";
var PURPLE = chalk.hex("#b26cff");
var COLOR = {
  gray: chalk.gray,
  green: chalk.green,
  purple: PURPLE,
  red: chalk.red,
  yellow: chalk.yellow
};
function depTypeLabel(depTypes) {
  return depTypes.map((depType) => depType === "dependencies" ? "dep" : "dev").join(",");
}
function formatAge(ageDays) {
  if (ageDays === void 0) return "-";
  if (ageDays < 1) return "<1d";
  if (ageDays < 365) return `${Math.floor(ageDays)}d`;
  return `${(ageDays / 365).toFixed(1)}y`;
}
function formatSecureDownloads(downloads) {
  if (downloads === void 0) return "-";
  if (downloads < 1e3) return `${downloads}`;
  const formatter = new Intl.NumberFormat("en-US", {
    maximumFractionDigits: 1,
    notation: "compact"
  });
  return formatter.format(downloads).toLowerCase();
}
function pad(raw, colored, width) {
  return colored + " ".repeat(Math.max(0, width - raw.length));
}
var columns = [
  {
    color: (_row, raw) => chalk.white(raw),
    header: "Package",
    value: (row) => row.name
  },
  {
    color: (_row, raw) => chalk.gray(raw),
    header: "Version",
    value: (row) => row.version ?? "-"
  },
  {
    color: (_row, raw) => chalk.gray(raw),
    header: "Type",
    value: (row) => depTypeLabel(row.depTypes)
  },
  {
    color: (_row, raw) => chalk.gray(raw),
    header: "Decls",
    value: (row) => `${row.declarationCount}`
  },
  {
    color: (row, raw) => COLOR[row.ageLevel](raw),
    header: "Age",
    value: (row) => formatAge(row.ageDays)
  },
  {
    color: (row, raw) => COLOR[row.downloadsLevel](raw),
    header: "Downloads/wk",
    value: (row) => formatSecureDownloads(row.downloads)
  }
];
function renderSecurityAuditRows(rows) {
  if (rows.length === 0) {
    console.log(chalk.green("Secure: no external direct dependencies found."));
    return;
  }
  const widths = columns.map((column) => {
    let width = column.header.length;
    for (const row of rows) {
      width = Math.max(width, column.value(row).length);
    }
    return width;
  });
  console.log(chalk.green("Secure: direct dependency audit"));
  const header = columns.map((column, index) => chalk.bold(column.header.padEnd(widths[index]))).join("  ");
  console.log(header);
  console.log(chalk.gray("\u2500".repeat(widths.reduce((total, width) => total + width + 2, -2))));
  for (const row of rows) {
    const line = columns.map((column, index) => {
      const raw = column.value(row);
      return pad(raw, column.color(row, raw), widths[index]);
    }).join("  ");
    console.log(line);
  }
  const redCount = rows.filter((row) => row.red).length;
  if (redCount > 0) {
    console.warn(chalk.red(`Secure: ${redCount} red finding${redCount === 1 ? "" : "s"} found.`));
  } else {
    console.log(chalk.green("Secure: no red findings."));
  }
}
export {
  formatSecureDownloads,
  renderSecurityAuditRows
};
//# sourceMappingURL=renderSecurityAudit.mjs.map
