import { describe, it, expect } from 'vitest'
import { extractCollection } from '~/shared/utils/apiHelpers'

describe('extractCollection', () => {
  it('returns the input if it is already an array', () => {
    const items = [{ id: 1 }, { id: 2 }]
    expect(extractCollection(items)).toEqual(items)
  })

  it('extracts from hydra:member', () => {
    const payload = { 'hydra:member': [{ id: 1 }], 'hydra:totalItems': 1 }
    expect(extractCollection(payload)).toEqual([{ id: 1 }])
  })

  it('extracts from member', () => {
    const payload = { member: [{ id: 1 }, { id: 2 }] }
    expect(extractCollection(payload)).toEqual([{ id: 1 }, { id: 2 }])
  })

  it('extracts from items', () => {
    const payload = { items: [{ id: 1 }] }
    expect(extractCollection(payload)).toEqual([{ id: 1 }])
  })

  it('extracts from data', () => {
    const payload = { data: [{ id: 1 }] }
    expect(extractCollection(payload)).toEqual([{ id: 1 }])
  })

  it('prefers member over hydra:member', () => {
    const payload = { member: [{ id: 'member' }], 'hydra:member': [{ id: 'hydra' }] }
    expect(extractCollection(payload)).toEqual([{ id: 'member' }])
  })

  it('returns empty array for null', () => {
    expect(extractCollection(null)).toEqual([])
  })

  it('returns empty array for undefined', () => {
    expect(extractCollection(undefined)).toEqual([])
  })

  it('returns empty array for empty object', () => {
    expect(extractCollection({})).toEqual([])
  })

  it('returns empty array for string', () => {
    expect(extractCollection('not an object')).toEqual([])
  })
})
