import { DominantSpeakersInfo } from '@azure/communication-calling' ; import { memoizeFnAll , toFlatCommunicationIdentifier } from '@internal/acs-ui-common' ; import { CallClientState , RemoteParticipantState , RemoteVideoStreamState } from '@internal/calling-stateful-client' ; import { VideoGalleryLocalParticipant , VideoGalleryRemoteParticipant , VideoGalleryStream } from '@internal/react-components' ; import { createSelector } from 'reselect' ; import { CallingBaseSelectorProps , getDisplayName , getDominantSpeakers , getIdentifier , getIsMuted , getIsScreenSharingOn , getLocalVideoStreams , getRemoteParticipants , getScreenShareRemoteParticipant } from './baseSelectors' ; import { checkIsSpeaking } from './SelectorUtils' ; const convertRemoteVideoStreamToVideoGalleryStream = ( stream ) : VideoGalleryStream => { return { id : stream . id , isAvailable : stream . isAvailable , isMirrored : stream . view ?. isMirrored , renderElement : stream . view ?. target } ; } ; const convertRemoteParticipantToVideoGalleryRemoteParticipant = ( userId , isMuted , isSpeaking , videoStreams , displayName ? ) : VideoGalleryRemoteParticipant => { const rawVideoStreamsArray = Object . values ( videoStreams ) ; let videoStream = undefined ; let screenShareStream = undefined ; if ( rawVideoStreamsArray [ 0 ] ) { if ( rawVideoStreamsArray [ 0 ] . mediaStreamType === 'Video' ) { videoStream = convertRemoteVideoStreamToVideoGalleryStream ( rawVideoStreamsArray [ 0 ] ) ; } else { screenShareStream = convertRemoteVideoStreamToVideoGalleryStream ( rawVideoStreamsArray [ 0 ] ) ; } } if ( rawVideoStreamsArray [ 1 ] ) { if ( rawVideoStreamsArray [ 1 ] . mediaStreamType === 'ScreenSharing' ) { screenShareStream = convertRemoteVideoStreamToVideoGalleryStream ( rawVideoStreamsArray [ 1 ] ) ; } else { videoStream = convertRemoteVideoStreamToVideoGalleryStream ( rawVideoStreamsArray [ 1 ] ) ; } } return { userId , displayName , isMuted , isSpeaking , videoStream , screenShareStream , isScreenSharingOn : screenShareStream !== undefined && screenShareStream . isAvailable } ; } ; const memoizedAllConvertRemoteParticipant = memoizeFnAll ( ( userId , isMuted , isSpeaking , videoStreams , displayName ? ) : VideoGalleryRemoteParticipant => { return convertRemoteParticipantToVideoGalleryRemoteParticipant ( userId , isMuted , isSpeaking , videoStreams , displayName ) ; } ) ; const videoGalleryRemoteParticipantsMemo = ( remoteParticipants ) : VideoGalleryRemoteParticipant [ ] => { if ( ! remoteParticipants ) { return [ ] ; } return memoizedAllConvertRemoteParticipant ( ( memoizedFn ) => { return Object . values ( remoteParticipants ) . filter ( ( participant ) => { return participant . state !== 'InLobby' ; } ) . map ( ( participant ) => { return memoizedFn ( toFlatCommunicationIdentifier ( participant . identifier ) , participant . isMuted , checkIsSpeaking ( participant ) , participant . videoStreams , participant . displayName ) ; } ) ; } ) ; } ; const dominantSpeakersWithFlatId = ( dominantSpeakers ? ) : undefined | string [ ] => { return dominantSpeakers ?. speakersList . map ( toFlatCommunicationIdentifier ) ; } ; /**
 * Selector type for  VideoGallery component.
 *
 *  public export type VideoGallerySelector = ( state : CallClientState , props : CallingBaseSelectorProps ) => { screenShareParticipant : VideoGalleryRemoteParticipant | undefined ; localParticipant : VideoGalleryLocalParticipant ; remoteParticipants : VideoGalleryRemoteParticipant [ ] ; dominantSpeakers ? : string [ ] ; } ; /**
 * Provides data attributes to  VideoGallery component.
 *  public export const videoGallerySelector = createSelector ( [ getScreenShareRemoteParticipant , getRemoteParticipants , getLocalVideoStreams , getIsMuted , getIsScreenSharingOn , getDisplayName , getIdentifier , getDominantSpeakers ] , ( screenShareRemoteParticipantId , remoteParticipants , localVideoStreams , isMuted , isScreenSharingOn , displayName , identifier , dominantSpeakers ) => { const screenShareRemoteParticipant = screenShareRemoteParticipantId && remoteParticipants ? remoteParticipants [ screenShareRemoteParticipantId ] : undefined ; const localVideoStream = localVideoStreams ?. find ( ( i ) => i . mediaStreamType === 'Video' ) ; const dominantSpeakerIds = dominantSpeakersWithFlatId ( dominantSpeakers ) ; const dominantSpeakersMap = { } ; dominantSpeakerIds ?. forEach ( ( speaker , idx ) => ( dominantSpeakersMap [ speaker ] = idx ) ) ; return { screenShareParticipant : screenShareRemoteParticipant ? convertRemoteParticipantToVideoGalleryRemoteParticipant ( toFlatCommunicationIdentifier ( screenShareRemoteParticipant . identifier ) , screenShareRemoteParticipant . isMuted , checkIsSpeaking ( screenShareRemoteParticipant ) , screenShareRemoteParticipant . videoStreams , screenShareRemoteParticipant . displayName ) : undefined , localParticipant : { userId : identifier , displayName : displayName ?? '' , isMuted : isMuted , isScreenSharingOn : isScreenSharingOn , videoStream : { isAvailable : ! ! localVideoStream , isMirrored : localVideoStream ?. view ?. isMirrored , renderElement : localVideoStream ?. view ?. target } } , remoteParticipants : videoGalleryRemoteParticipantsMemo ( remoteParticipants ) , dominantSpeakers : dominantSpeakerIds } ; } ) ;