import { describe, expect, it } from 'vitest';
import { toTitleCase } from '../text.js';

describe('toTitleCase', () => {
  it('returns empty string unchanged', () => {
    expect(toTitleCase('')).toBe('');
  });

  it('title-cases a single ALL-CAPS word', () => {
    expect(toTitleCase('BIOCOP')).toBe('Biocop');
    expect(toTitleCase('LAMBERTS')).toBe('Lamberts');
    expect(toTitleCase('SOLGAR')).toBe('Solgar');
  });

  it('title-cases a multi-word ALL-CAPS string with spaces', () => {
    expect(toTitleCase('EL GRANERO INTEGRAL')).toBe('El Granero Integral');
    expect(toTitleCase('COMPLEMENTOS Y NUTRICIÓN')).toBe('Complementos y Nutrición');
    expect(toTitleCase('LA FINESTRA SUL CIELO')).toBe('La Finestra Sul Cielo');
    expect(toTitleCase('DULCES LISSEN')).toBe('Dulces Lissen');
  });

  it('preserves dots as word boundaries (A.VOGEL → A.Vogel)', () => {
    expect(toTitleCase('A.VOGEL')).toBe('A.Vogel');
  });

  it('preserves hyphens as word boundaries (DAS-BROT → Das-Brot)', () => {
    expect(toTitleCase('DAS-BROT')).toBe('Das-Brot');
  });

  it('preserves ampersands as word boundaries (TEA & INFUSIONS → Tea & Infusions)', () => {
    expect(toTitleCase('TEA & INFUSIONS')).toBe('Tea & Infusions');
  });

  it('handles Spanish accented characters (NUTRICIÓN → Nutrición)', () => {
    expect(toTitleCase('COMPLEMENTOS Y NUTRICIÓN')).toBe('Complementos y Nutrición');
  });

  it('is idempotent on already Title-Cased strings', () => {
    expect(toTitleCase('La Finestra Sul Cielo')).toBe('La Finestra Sul Cielo');
    expect(toTitleCase('A.Vogel')).toBe('A.Vogel');
    expect(toTitleCase('El Granero Integral')).toBe('El Granero Integral');
    expect(toTitleCase(toTitleCase('A.VOGEL'))).toBe('A.Vogel');
  });

  it('leaves mixed-case strings unchanged (BioSana)', () => {
    expect(toTitleCase('BioSana')).toBe('BioSana');
  });

  it('does not corrupt digits or other non-letter characters in words', () => {
    expect(toTitleCase('500 KILOS')).toBe('500 Kilos');
  });

  it('handles multiple consecutive separators', () => {
    expect(toTitleCase('FOO  BAR')).toBe('Foo  Bar');
    expect(toTitleCase('A..VOGEL')).toBe('A..Vogel');
  });

  it('handles a string that is only separators', () => {
    expect(toTitleCase('   ')).toBe('   ');
    expect(toTitleCase('---')).toBe('---');
  });

  it('keeps single-letter conjunction lowercase (y/e/o/u) when not first', () => {
    expect(toTitleCase('AGUA Y SAL')).toBe('Agua y Sal');
    expect(toTitleCase('PADRE E HIJO')).toBe('Padre e Hijo');
    expect(toTitleCase('SIETE O NUEVE')).toBe('Siete o Nueve');
  });

  it('keeps single-letter conjunction capitalized when it IS the first word', () => {
    expect(toTitleCase('Y HIRVIO')).toBe('Y Hirvio');
  });

  it('does not lowercase non-conjunction single-letter words', () => {
    expect(toTitleCase('A SIDE B')).toBe('A Side B');
  });
});
