import {
  BULK_IMPORT_NUMBERS,
  CONTAINER_EXPORT_NUMBERS,
  CONTAINER_IMPORT_NUMBERS,
  pickLowestFreeNumber,
  pickTrainNumberPool,
} from './train-number.util';

describe('train-number.util', () => {
  describe('pickTrainNumberPool', () => {
    it('picks container export (odd) when container wagons dominate and direction is EXPORT', () => {
      const pool = pickTrainNumberPool(5, 2, 'EXPORT');
      expect(pool.cargo).toBe('CONTAINER');
      expect(pool.direction).toBe('EXPORT');
      expect(pool.numbers).toEqual(CONTAINER_EXPORT_NUMBERS);
    });

    it('picks container import (even) when container wagons dominate and direction is IMPORT', () => {
      const pool = pickTrainNumberPool(5, 2, 'IMPORT');
      expect(pool.numbers).toEqual(CONTAINER_IMPORT_NUMBERS);
    });

    it('picks bulk when bulk wagons dominate', () => {
      const pool = pickTrainNumberPool(1, 9, 'IMPORT');
      expect(pool.cargo).toBe('BULK');
      expect(pool.numbers).toEqual(BULK_IMPORT_NUMBERS);
    });

    it('treats a tie as container', () => {
      expect(pickTrainNumberPool(3, 3, 'EXPORT').cargo).toBe('CONTAINER');
    });

    it('defaults DOMESTIC to the export/odd pool', () => {
      expect(pickTrainNumberPool(5, 0, 'DOMESTIC').direction).toBe('EXPORT');
      expect(pickTrainNumberPool(5, 0, null).direction).toBe('EXPORT');
    });
  });

  describe('pickLowestFreeNumber', () => {
    it('returns the lowest unused number', () => {
      expect(pickLowestFreeNumber(CONTAINER_EXPORT_NUMBERS, ['8001'])).toBe('8101');
    });

    it('returns the first number when none are used', () => {
      expect(pickLowestFreeNumber(CONTAINER_EXPORT_NUMBERS, [])).toBe('8001');
    });

    it('returns null when the pool is exhausted', () => {
      expect(pickLowestFreeNumber(BULK_IMPORT_NUMBERS, [...BULK_IMPORT_NUMBERS])).toBeNull();
    });
  });
});
