import { getNextTabNameHelper } from './helpers';
import { Tab } from './schemas';
import { EntityMap } from '@ngrx/signals/entities';

describe('getNextTabNameHelper', () => {
  const createTab = (id: number, name: string): Tab => ({
    id,
    name,
    createdAt: Date.now(),
    activatedAt: Date.now(),
    tags: [],
    metadata: {},
    location: { current: -1, locations: [] },
  });

  it('should return "Vorgang 1" when no tabs exist', () => {
    const entities: EntityMap<Tab> = {};
    const result = getNextTabNameHelper(entities);
    expect(result).toBe('Vorgang 1');
  });

  it('should return "Vorgang 1" when no Vorgang tabs exist', () => {
    const entities: EntityMap<Tab> = {
      1: createTab(1, 'Bestellbestätigung'),
      2: createTab(2, 'Artikelsuche'),
    };
    const result = getNextTabNameHelper(entities);
    expect(result).toBe('Vorgang 1');
  });

  it('should return "Vorgang 2" when one Vorgang tab exists', () => {
    const entities: EntityMap<Tab> = {
      1: createTab(1, 'Vorgang 1'),
    };
    const result = getNextTabNameHelper(entities);
    expect(result).toBe('Vorgang 2');
  });

  it('should return "Vorgang 4" when three Vorgang tabs exist', () => {
    const entities: EntityMap<Tab> = {
      1: createTab(1, 'Vorgang 1'),
      2: createTab(2, 'Vorgang 3'),
      3: createTab(3, 'Vorgang 2'),
    };
    const result = getNextTabNameHelper(entities);
    expect(result).toBe('Vorgang 4');
  });

  it('should count only Vorgang tabs, ignore other tabs', () => {
    const entities: EntityMap<Tab> = {
      1: createTab(1, 'Vorgang 1'),
      2: createTab(2, 'Bestellbestätigung'),
      3: createTab(3, 'Vorgang 2'),
      4: createTab(4, 'Artikelsuche'),
      5: createTab(5, 'Max Mustermann - Bestellbestätigung'),
    };
    const result = getNextTabNameHelper(entities);
    expect(result).toBe('Vorgang 3');
  });

  it('should handle gaps in numbering by counting tabs', () => {
    const entities: EntityMap<Tab> = {
      1: createTab(1, 'Vorgang 1'),
      2: createTab(2, 'Vorgang 5'),
      3: createTab(3, 'Vorgang 10'),
    };
    // Count is 3, so next should be Vorgang 4
    const result = getNextTabNameHelper(entities);
    expect(result).toBe('Vorgang 4');
  });
});
