import { describe, it, expect } from 'vitest'
import { distributeIntoColumns } from '../src/composables/usePolyMasonry.js'

describe('distributeIntoColumns', () => {
  it('returns the requested number of columns', () => {
    const cols = distributeIntoColumns([], 3)
    expect(cols.length).toBe(3)
    expect(cols.every(c => c.length === 0)).toBe(true)
  })

  it('places each item in the then-shortest column', () => {
    // 3 square items (relative height 1 each), 3 columns -> one per column.
    const items = [
      { id: 1, width: 100, height: 100 },
      { id: 2, width: 100, height: 100 },
      { id: 3, width: 100, height: 100 }
    ]
    const cols = distributeIntoColumns(items, 3)
    expect(cols.map(c => c.map(i => i.id))).toEqual([[1], [2], [3]])
  })

  it('routes a 4th item to a balanced column', () => {
    const items = [
      { id: 1, width: 100, height: 300 }, // tall -> col0 height 3
      { id: 2, width: 100, height: 100 }, // col1 height 1
      { id: 3, width: 100, height: 100 }, // col2 height 1
      { id: 4, width: 100, height: 100 }  // shortest is col1 (1) -> col1
    ]
    const cols = distributeIntoColumns(items, 3)
    expect(cols[0].map(i => i.id)).toEqual([1])
    expect(cols[1].map(i => i.id)).toEqual([2, 4])
    expect(cols[2].map(i => i.id)).toEqual([3])
  })

  it('treats missing/zero dimensions as a square (height 1)', () => {
    const cols = distributeIntoColumns([{ id: 9 }], 1)
    expect(cols[0]).toEqual([{ id: 9 }])
  })
})
