import { describe, it, expect } from 'vitest'
import { resolveColumnForSource } from './resolve-property-for-source'

describe('resolveColumnForSource', () => {
  describe('single column string', () => {
    it('should return the same column for all sources', () => {
      const column = 'population'
      expect(resolveColumnForSource(column, 0, 3)).toBe('population')
      expect(resolveColumnForSource(column, 1, 3)).toBe('population')
      expect(resolveColumnForSource(column, 2, 3)).toBe('population')
    })
  })

  describe('array with single column', () => {
    it('should return the same column for all sources', () => {
      const column = ['population']
      expect(resolveColumnForSource(column, 0, 3)).toBe('population')
      expect(resolveColumnForSource(column, 1, 3)).toBe('population')
      expect(resolveColumnForSource(column, 2, 3)).toBe('population')
    })
  })

  describe('array with multiple columns matching sources', () => {
    it('should return the column at the correct index', () => {
      const column = ['population', 'area', 'density']
      expect(resolveColumnForSource(column, 0, 3)).toBe('population')
      expect(resolveColumnForSource(column, 1, 3)).toBe('area')
      expect(resolveColumnForSource(column, 2, 3)).toBe('density')
    })
  })

  describe('array length mismatch', () => {
    it('should throw error when array length does not match sources', () => {
      const column = ['population', 'area']
      expect(() => resolveColumnForSource(column, 0, 3)).toThrow(
        /column array length \(2\) must match sources length \(3\)/i,
      )
    })

    it('should include helpful error message', () => {
      const column = ['col1', 'col2', 'col3', 'col4']
      expect(() => resolveColumnForSource(column, 0, 2)).toThrow(
        /Either provide a single column for all sources, or one column per source/i,
      )
    })
  })
})
