/**
 * Derive a human-readable app name from a JAR filename.
 *
 * Rule:
 *  1. Strip the `.jar` extension.
 *  2. Truncate at the first digit (0-9) or `.`.
 *  3. Replace `-` and `_` with spaces.
 *  4. Collapse multiple spaces and trim.
 *  5. Drop 1-char orphan tokens (e.g. the trailing `v` in `my-app-v2`).
 *  6. Title-case each remaining word.
 *
 * The result is a suggestion — the caller is expected to let the user override.
 */
export function deriveAppName(filename: string): string {
  if (!filename) return '';

  let stem = filename.replace(/\.jar$/i, '');

  // Truncate at first digit or dot
  const match = stem.match(/[0-9.]/);
  if (match && match.index !== undefined) {
    stem = stem.slice(0, match.index);
  }

  // Separators → space
  stem = stem.replace(/[-_]+/g, ' ');

  // Collapse whitespace + trim
  stem = stem.replace(/\s+/g, ' ').trim();
  if (!stem) return '';

  // Drop 1-char orphan tokens
  const tokens = stem.split(' ').filter((t) => t.length > 1);
  if (tokens.length === 0) return '';

  // Title-case
  return tokens.map((t) => t.charAt(0).toUpperCase() + t.slice(1).toLowerCase()).join(' ');
}
