import { combineLatest , Observable , timer , of } from 'rxjs' ; import { map , catchError , filter , mergeMap , tap } from 'rxjs/operators' ; import { i18n } from '@kbn/i18n' ; import { FetchResult , NewsfeedPluginBrowserConfig } from '../types' ; import { NewsfeedApiDriver } from './driver' ; import { NeverFetchNewsfeedApiDriver } from './never_fetch_driver' ; import { NewsfeedStorage } from './storage' ; export enum NewsfeedApiEndpoint { KIBANA = 'kibana' , KIBANA_ANALYTICS = 'kibana-analytics' , SECURITY_SOLUTION = 'security-solution' , OBSERVABILITY = 'observability' } export interface NewsfeedApi { /**
     * The current fetch results
     */ fetchResults$ : Observable < void | null | FetchResult > ; /**
     * Mark the given items as read.
     * Will refresh the `hasNew` value of the emitted FetchResult accordingly
     */ markAsRead ( itemHashes ) : void ; } export function getApi ( config , kibanaVersion , newsfeedId , isScreenshotMode ) { const storage = new NewsfeedStorage ( newsfeedId ) ; const mainInterval = config . mainInterval . asMilliseconds ( ) ; const createNewsfeedApiDriver = ( ) => { if ( isScreenshotMode ) { return new NeverFetchNewsfeedApiDriver ( ) ; } const userLanguage = i18n . getLocale ( ) ; const fetchInterval = config . fetchInterval . asMilliseconds ( ) ; return new NewsfeedApiDriver ( kibanaVersion , userLanguage , fetchInterval , storage ) ; } ; const driver = createNewsfeedApiDriver ( ) ; const results$ = timer ( 0 , mainInterval ) . pipe ( filter ( ( ) => driver . shouldFetch ( ) ) , mergeMap ( ( ) => driver . fetchNewsfeedItems ( config . service ) . pipe ( catchError ( ( err ) => { window . console . error ( err ) ; return of ( { error : err , kibanaVersion , hasNew : false , feedItems : [ ] , } ) ; } ) ) ) , tap ( ( ) => storage . setLastFetchTime ( new Date ( ) ) ) ) ; const merged$ = combineLatest ( [ results$ , storage . isAnyUnread$ ( ) ] ) . pipe ( map ( ( [ results , isAnyUnread ] ) => { return { ... results , hasNew : results . error ? false : isAnyUnread , } ; } ) ) ; return { fetchResults$ : merged$ , markAsRead : ( itemHashes ) => { storage . markItemsAsRead ( itemHashes ) ; } , } ; }