import { PagedAsyncIterableIterator } from '@azure/core-paging' ; import { ReactElement } from 'react' ; import { Common , fromFlatCommunicationIdentifier } from '@internal/acs-ui-common' ; import { StatefulChatClient } from '@internal/chat-stateful-client' ; import { ChatMessage , ChatThreadClient } from '@azure/communication-chat' ; import memoizeOne from 'memoize-one' ; /**
 * Object containing all the handlers required for chat components.
 *
 * Chat related components from this package are able to pick out relevant handlers from this object.
 * See  useHandlers and  usePropsFor .
 *
 *  public export type ChatHandlers = { onSendMessage : ( content : string ) => Promise < void > ; onMessageSeen : ( chatMessageId : string ) => Promise < void > ; onTyping : ( ) => Promise < void > ; onRemoveParticipant : ( userId : string ) => Promise < void > ; updateThreadTopicName : ( topicName : string ) => Promise < void > ; onLoadPreviousChatMessages : ( messagesToLoad : number ) => Promise < boolean > ; onUpdateMessage : ( messageId : string , content : string ) => Promise < void > ; onDeleteMessage : ( messageId : string ) => Promise < void > ; } ; /**
 * Create the default implementation of  ChatHandlers .
 *
 * Useful when implementing a custom component that utilizes the providers
 * exported from this library.
 *
 * Returned object is memoized to avoid rerenders when used as props for React Components.
 *
 *  public export const createDefaultChatHandlers = memoizeOne ( ( chatClient , chatThreadClient ) : ChatHandlers => { let messageIterator = undefined ; return { onSendMessage : async ( content ) => { const sendMessageRequest = { content , senderDisplayName : chatClient . getState ( ) . displayName } ; await chatThreadClient . sendMessage ( sendMessageRequest ) ; } , onUpdateMessage : async ( messageId , content ) => { await chatThreadClient . updateMessage ( messageId , { content } ) ; } , onDeleteMessage : async ( messageId ) => { await chatThreadClient . deleteMessage ( messageId ) ; } , onMessageSeen : async ( chatMessageId ) => { await chatThreadClient . sendReadReceipt ( { chatMessageId } ) ; } , onTyping : async ( ) => { await chatThreadClient . sendTypingNotification ( ) ; } , onRemoveParticipant : async ( userId ) => { await chatThreadClient . removeParticipant ( fromFlatCommunicationIdentifier ( userId ) ) ; } , updateThreadTopicName : async ( topicName ) => { await chatThreadClient . updateTopic ( topicName ) ; } , onLoadPreviousChatMessages : async ( messagesToLoad ) => { if ( messageIterator === undefined ) { messageIterator = chatThreadClient . listMessages ( { maxPageSize : 50 } ) ; } let remainingMessagesToGet = messagesToLoad ; let isAllChatMessagesLoaded = false ; while ( remainingMessagesToGet >= 1 ) { const message = await messageIterator . next ( ) ; if ( message . value ?. type && message . value . type === 'text' ) { remainingMessagesToGet -- ; } if ( message . done ) { isAllChatMessagesLoaded = true ; break ; } } return isAllChatMessagesLoaded ; } } ; } ) ; public export const createDefaultChatHandlersForComponent = < Props > ( chatClient , chatThreadClient , _ ) : Common < ChatHandlers , Props > => { return createDefaultChatHandlers ( chatClient , chatThreadClient ) ; } ;