import { Conversation } from '../models/conversation' ; import { ConversationState } from '../logic/conversations/conversationsTypes' ; type LabelValue = 'NumMessages' | 'NumUnread' | 'Time' | 'Size' | 'NumAttachments' ; type LabelContextValue = 'ContextNumMessages' | 'ContextNumUnread' | 'ContextTime' | 'ContextSize' | 'ContextNumAttachments' ; export const getSenders = ( { Senders = [ ] } ) => Senders ; export const getRecipients = ( { Recipients = [ ] } ) => Recipients ; export const getConversationContextValue = ( conversation , value , labelID ) => { const matchingLabel = conversation ?. Labels ?. find ( ( label ) => label . ID === labelID ) ; const matchingValue = matchingLabel ?. [ `Context${ value }` as LabelContextValue ] ; if ( matchingValue !== undefined ) { return matchingValue ; } if ( conversation ?. [ value ] !== undefined ) { return conversation [ value ] as number ; } return 0 ; } ; export const getNumAttachments = ( conversation ) => conversation ?. NumAttachments || 0 ; export const hasAttachments = ( conversation ) => getNumAttachments ( conversation ) > 0 ; export const getNumUnread = ( conversation , labelID ) => getConversationContextValue ( conversation , 'NumUnread' , labelID ) ; export const isUnread = ( conversation , labelID ) => getNumUnread ( conversation , labelID ) !== 0 ; export const getNumMessages = ( conversation , labelID ) => getConversationContextValue ( conversation , 'NumMessages' , labelID ) ; export const getTime = ( conversation , labelID ) => getConversationContextValue ( conversation , 'Time' , labelID ) ; export const getSize = ( conversation , labelID ) => getConversationContextValue ( conversation , 'Size' , labelID ) ; /**
 * Returns a map of boolean with all labels on the conversation and for each true if all messages have the label and false if not
 */ export const getLabelIDs = ( conversation , contextLabelID ) => { const contextNumMessages = getNumMessages ( conversation , contextLabelID ) ; return ( conversation ?. Labels ?. reduce < { [ labelID ] : boolean | undefined ; } > ( ( acc , label ) => { acc [ label . ID ] = label . ContextNumMessages === contextNumMessages ; return acc ; } , { } ) || { } ) ; } ; export const mergeConversations = ( reference , data ) : ConversationState => { return { ... reference , ... data , Conversation : ! ! reference ?. Conversation || ! ! data . Conversation ? ( { ... reference ?. Conversation , ... data . Conversation } as Conversation ) : undefined , errors : { ... reference ?. errors , ... data . errors } , } as ConversationState ; } ;