// src/stats/sparkbar.ts
// Render a filled-block progress bar of a given width from a 0-1 ratio.
// Uses Unicode block elements for smooth graduation.
// Owned by Worker A; imported by renderGain.ts and format.ts.

const BLOCKS = [" ", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█"] as const;
const FULL = "█";

/**
 * Render a progress bar of `width` terminal columns representing `pct` (0-1).
 *
 * @param pct   - fraction in [0, 1] (clamped)
 * @param width - total column width of the bar (default 12)
 * @returns     - string of exactly `width` columns
 */
export function sparkbar(pct: number, width = 12): string {
  const clamped = Math.max(0, Math.min(1, pct));
  const filled = clamped * width;
  const fullBlocks = Math.floor(filled);
  const remainder = filled - fullBlocks;
  const partialIdx = Math.round(remainder * 8);

  let bar = FULL.repeat(fullBlocks);
  if (fullBlocks < width) {
    bar += partialIdx > 0 ? (BLOCKS[partialIdx] ?? " ") : " ";
    bar += " ".repeat(Math.max(0, width - fullBlocks - 1));
  }
  return bar.slice(0, width);
}
