import { Observable , Subscription } from 'rxjs' ; import { map , tap } from 'rxjs/operators' ; import { TimefilterSetup } from '../timefilter' ; import { FilterManager } from '../filter_manager' ; import { QueryState , QueryStateChange } from './index' ; import { createStateContainer } from '../../../../opensearch_dashboards_utils/public' ; import { isFilterPinned , compareFilters , COMPARE_ALL_OPTIONS } from '../../../common' ; import { QueryStringContract } from '../query_string' ; export function createQueryStateObservable ( { timefilter : { timefilter } , filterManager , queryString , } ) { return new Observable ( ( subscriber ) => { const state = createStateContainer < QueryState > ( { time : timefilter . getTime ( ) , refreshInterval : timefilter . getRefreshInterval ( ) , filters : filterManager . getFilters ( ) , query : queryString . getQuery ( ) , } ) ; let currentChange = { } ; const subs = [ queryString . getUpdates$ ( ) . subscribe ( ( ) => { currentChange . query = true ; state . set ( { ... state . get ( ) , query : queryString . getQuery ( ) } ) ; } ) , timefilter . getTimeUpdate$ ( ) . subscribe ( ( ) => { currentChange . time = true ; state . set ( { ... state . get ( ) , time : timefilter . getTime ( ) } ) ; } ) , timefilter . getRefreshIntervalUpdate$ ( ) . subscribe ( ( ) => { currentChange . refreshInterval = true ; state . set ( { ... state . get ( ) , refreshInterval : timefilter . getRefreshInterval ( ) } ) ; } ) , filterManager . getUpdates$ ( ) . subscribe ( ( ) => { currentChange . filters = true ; const { filters } = state . get ( ) ; const globalOld = filters ?. filter ( ( f ) => isFilterPinned ( f ) ) ; const appOld = filters ?. filter ( ( f ) => ! isFilterPinned ( f ) ) ; const globalNew = filterManager . getGlobalFilters ( ) ; const appNew = filterManager . getAppFilters ( ) ; if ( ! globalOld || ! compareFilters ( globalOld , globalNew , COMPARE_ALL_OPTIONS ) ) { currentChange . globalFilters = true ; } if ( ! appOld || ! compareFilters ( appOld , appNew , COMPARE_ALL_OPTIONS ) ) { currentChange . appFilters = true ; } state . set ( { ... state . get ( ) , filters : filterManager . getFilters ( ) , } ) ; } ) , state . state$ . pipe ( map ( ( newState ) => ( { state : newState , changes : currentChange } ) ) , tap ( ( ) => { currentChange = { } ; } ) ) . subscribe ( subscriber ) , ] ; return ( ) => { subs . forEach ( ( s ) => s . unsubscribe ( ) ) ; } ; } ) ; }