import { Inject , Injectable } from '@angular/core' ; import { BehaviorSubject } from 'rxjs' ; import { filter } from 'rxjs/operators' ; import { Recipient } from '../core/recipient' ; import { CHAT_SERVICE_TOKEN , ChatService } from './chat-service' ; export class ChatWindowState { constructor ( readonly recipient , public isCollapsed ) { } } export interface AttachableTrack { attach ( elem ) : void ; } /**
 * Used to open chat windows programmatically.
 */ @ Injectable ( ) export class ChatListStateService { public openChats$ = new BehaviorSubject < ChatWindowState [ ] > ( [ ] ) ; public openTracks$ = new BehaviorSubject < AttachableTrack [ ] > ( [ ] ) ; constructor ( @ Inject ( CHAT_SERVICE_TOKEN ) private chatService ) { this . chatService . state$ . pipe ( filter ( newState => newState === 'disconnected' ) ) . subscribe ( ( ) => { this . openChats$ . next ( [ ] ) ; } ) ; this . chatService . contactRequestsReceived$ . subscribe ( contacts => { for ( const contact of contacts ) { this . openChat ( contact ) ; } } ) ; } private openChatCollapsed ( recipient ) { if ( ! this . isChatWithRecipientOpen ( recipient ) ) { const openChats = this . openChats$ . getValue ( ) ; const chatWindow = new ChatWindowState ( recipient , true ) ; const copyWithNewContact = [ chatWindow ] . concat ( openChats ) ; this . openChats$ . next ( copyWithNewContact ) ; } } public openChat ( recipient ) { this . openChatCollapsed ( recipient ) ; this . findChatWindowStateByRecipient ( recipient ) . isCollapsed = false ; } public closeChat ( recipient ) { const openChats = this . openChats$ . getValue ( ) ; const index = this . findChatWindowStateIndexByRecipient ( recipient ) ; if ( index >= 0 ) { const copyWithoutContact = openChats . slice ( ) ; copyWithoutContact . splice ( index , 1 ) ; this . openChats$ . next ( copyWithoutContact ) ; } } public openTrack ( track ) { this . openTracks$ . next ( this . openTracks$ . getValue ( ) . concat ( [ track ] ) ) ; } public closeTrack ( track ) { this . openTracks$ . next ( this . openTracks$ . getValue ( ) . filter ( s => s !== track ) ) ; } isChatWithRecipientOpen ( recipient ) { return this . findChatWindowStateByRecipient ( recipient ) !== undefined ; } private findChatWindowStateIndexByRecipient ( recipient ) { return this . openChats$ . getValue ( ) . findIndex ( ( chatWindowState ) => chatWindowState . recipient . equalsBareJid ( recipient ) ) ; } private findChatWindowStateByRecipient ( recipient ) { return this . openChats$ . getValue ( ) . find ( chat => chat . recipient . equalsBareJid ( recipient ) ) ; } }