const { normalizeName } = require('../../src/utils/normalize');

describe('normalizeName (DUP-1)', () => {
  test('basic lowercase + trim', () => {
    expect(normalizeName('  Jalen Brunson  ')).toBe('jalen brunson');
  });

  test('strips accents via NFD', () => {
    expect(normalizeName('Luka Dončić')).toBe('luka doncic');
  });

  test('drops suffixes jr/sr/ii/iii/iv/v', () => {
    expect(normalizeName('Tim Hardaway Jr.')).toBe('tim hardaway');
    expect(normalizeName('Wade Phillips Sr.')).toBe('wade phillips');
    expect(normalizeName('Sammy Davis III')).toBe('sammy davis');
    // VIII isn't in the suffix list (i, ii, iii, iv, v only) — it's
    // realistically not a player-name suffix, so the regex leaves it.
    expect(normalizeName('Henry VIII')).toBe('henry viii');
  });

  test('strips punctuation (default: keepDigits=false drops digits too)', () => {
    expect(normalizeName('A.J. Brown')).toBe('a j brown');
    expect(normalizeName("Shaq O'Neal #34")).toBe('shaq o neal');
  });

  test('keepDigits true preserves jersey numbers', () => {
    expect(normalizeName("Shaq O'Neal #34", { keepDigits: true })).toBe('shaq o neal 34');
    expect(normalizeName('Player 23', { keepDigits: true })).toBe('player 23');
    expect(normalizeName('Player 23', { keepDigits: false })).toBe('player');
  });

  test('collapses runs of whitespace', () => {
    expect(normalizeName('Spaced   Out\tName')).toBe('spaced out name');
  });

  test('null / undefined / empty input returns empty string', () => {
    expect(normalizeName(null)).toBe('');
    expect(normalizeName(undefined)).toBe('');
    expect(normalizeName('')).toBe('');
  });

  test('numeric input gets toStringed', () => {
    expect(normalizeName(42, { keepDigits: true })).toBe('42');
  });

  test('idempotent — normalize(normalize(x)) === normalize(x)', () => {
    const samples = ['Luka Dončić', 'Tim Hardaway Jr.', "A.J. Brown #11"];
    for (const s of samples) {
      const once = normalizeName(s);
      expect(normalizeName(once)).toBe(once);
    }
    for (const s of samples) {
      const once = normalizeName(s, { keepDigits: true });
      expect(normalizeName(once, { keepDigits: true })).toBe(once);
    }
  });
});
