import { ConnectionQuality } from '@zocks-communications/client-sdk-js'
import { sortParticipantsBySpeakers } from './utils'
import { ParticipantModel } from '../../store'

const createMockParticipants = (activeSpeakers: string[]): ParticipantModel[] =>
  Array.from({ length: 10 }, (x, i) => {
    return {
      id: i.toString(10),
      name: i.toString(10),
      tracks: {
        videoTracks: [],
        audioTracks: [],
        screenShareTrack: null,
      },
      connectionQuality: ConnectionQuality.UNKNOWN,
      isSpeaking: activeSpeakers.indexOf(i.toString(10)) >= 0,
    }
  })

test('sortParticipantsBySpeakers does not change participants when there are no active speakers', () => {
  const mockActiveSpeakers: string[] = []
  const mockParticipants = createMockParticipants(mockActiveSpeakers)

  expect(sortParticipantsBySpeakers(mockActiveSpeakers, mockParticipants)).toStrictEqual(mockParticipants)
})

test('sortParticipantsBySpeakers does not change participants when there is a single active speaker', () => {
  const mockActiveSpeakers = ['8']
  const mockParticipants = createMockParticipants(mockActiveSpeakers)

  const sortedParticipants = sortParticipantsBySpeakers(mockActiveSpeakers, mockParticipants)

  const expectedOrder = ['8', '0', '1', '2', '3', '4', '5', '6', '7', '9']
  expect(sortedParticipants.map((p) => p.id)).toEqual(expectedOrder)
})

test('sortParticipantsBySpeakers does not change participants when there are a multiple active speakers', () => {
  const mockActiveSpeakers = ['8', '3', '7']
  const mockParticipants = createMockParticipants(mockActiveSpeakers)

  const sortedParticipants = sortParticipantsBySpeakers(mockActiveSpeakers, mockParticipants)

  const expectedOrder = ['8', '3', '7', '0', '1', '2', '4', '5', '6', '9']
  expect(sortedParticipants.map((p) => p.id)).toEqual(expectedOrder)
})
