import { getUserId , getDisplayName , getParticipants , ChatBaseSelectorProps } from './baseSelectors' ; import * as reselect from 'reselect' ; import { toFlatCommunicationIdentifier } from '@internal/acs-ui-common' ; import { ChatParticipant } from '@azure/communication-chat' ; import { ParticipantListParticipant } from '@internal/react-components' ; import { ChatClientState } from '@internal/chat-stateful-client' ; import { getIdentifierKind } from '@azure/communication-common' ; const convertChatParticipantsToCommunicationParticipants = ( chatParticipants ) : ParticipantListParticipant [ ] => { return chatParticipants . map ( ( participant ) => { return { userId : toFlatCommunicationIdentifier ( participant . id ) , displayName : participant . displayName , isRemovable : getIdentifierKind ( participant . id ) . kind == 'communicationUser' } ; } ) ; } ; /**
 * get the index of moderator to help updating its display name if they are the local user or removing them from list of participants otherwise
 */ const moderatorIndex = ( participants ) : number => { return participants . map ( ( p ) => p . displayName ) . indexOf ( undefined ) ; } ; /**
 * Selector type for  ParticipantList component.
 *
 *  public export type ChatParticipantListSelector = ( state : ChatClientState , props : ChatBaseSelectorProps ) => { myUserId : string ; participants : ParticipantListParticipant [ ] ; } ; /**
 * Selector for  ParticipantList component.
 *
 *  public export const chatParticipantListSelector = reselect . createSelector ( [ getUserId , getParticipants , getDisplayName ] , ( userId , chatParticipants , displayName ) => { let participants = convertChatParticipantsToCommunicationParticipants ( Object . values ( chatParticipants ) ) ; if ( 0 !== participants . length ) { const moderatorIdx = moderatorIndex ( participants ) ; if ( - 1 !== moderatorIdx ) { const userIndex = participants . map ( ( p ) => p . userId ) . indexOf ( userId ) ; if ( moderatorIdx === userIndex ) { participants [ moderatorIdx ] . displayName = displayName ; } else { participants = participants . filter ( ( p ) => p . displayName ) ; } } } return { myUserId : userId , participants : participants } ; } ) ;