interface InitialsOpts {
  maxChars?: number;   // max total chars — default 2
  fallback?: string;  // returned on empty input — default '??'
}

/**
 * Derive display initials from a full name.
 * "John Doe" -> "JD", "Alice" -> "AL", "" -> "??"
 */
export function initialsOf(name: string | null | undefined, opts: InitialsOpts = {}): string {
  const { maxChars = 2, fallback = '??' } = opts;
  if (!name?.trim()) return fallback;
  const words = name.trim().split(/\s+/);
  if (words.length === 1) {
    return (words[0] ?? fallback).slice(0, maxChars).toUpperCase();
  }
  return words
    .slice(0, maxChars)
    .map((w) => w[0])
    .join('')
    .toUpperCase();
}