import { IndexPatternField } from 'src/plugins/data/public' ; import { FieldFilterState , isFieldFiltered } from './field_filter' ; interface GroupedFields { selected : IndexPatternField [ ] ; popular : IndexPatternField [ ] ; unpopular : IndexPatternField [ ] ; } /**
 * group the fields into selected, popular and unpopular, filter by fieldFilterState
 */ export function groupFields ( fields , columns , popularLimit , fieldCounts , fieldFilterState ) { const result = { selected : [ ] , popular : [ ] , unpopular : [ ] , } ; if ( ! Array . isArray ( fields ) || ! Array . isArray ( columns ) || typeof fieldCounts !== 'object' ) { return result ; } const popular = fields . filter ( ( field ) => ! columns . includes ( field . name ) && field . count ) . sort ( ( a , b ) => ( b . count || 0 ) - ( a . count || 0 ) ) . map ( ( field ) => field . name ) . slice ( 0 , popularLimit ) ; const compareFn = ( a , b ) => { if ( ! a . displayName ) { return 0 ; } return a . displayName . localeCompare ( b . displayName || '' ) ; } ; const fieldsSorted = fields . sort ( compareFn ) ; for ( const field of fieldsSorted ) { if ( ! isFieldFiltered ( field , fieldFilterState , fieldCounts ) ) { continue ; } if ( columns . includes ( field . name ) ) { result . selected . push ( field ) ; } else if ( popular . includes ( field . name ) && field . type !== '_source' ) { result . popular . push ( field ) ; } else if ( field . type !== '_source' ) { result . unpopular . push ( field ) ; } } return result ; }