import { IAggType , IAggConfig , IndexPattern , search } from '../../../data/public' ; const { propFilter } = search . aggs ; const filterByName = propFilter ( 'name' ) ; type AggTypeFilter = ( aggType : IAggType , indexPattern : IndexPattern , aggConfig : IAggConfig , aggFilter : string [ ] ) => boolean ; const filters = [ /**
     * This filter checks the defined aggFilter in the schemas of that visualization
     * and limits available aggregations based on that.
     */ ( aggType , indexPattern , aggConfig , aggFilter ) => { const doesSchemaAllowAggType = filterByName ( [ aggType ] , aggFilter ) . length !== 0 ; return doesSchemaAllowAggType ; } , /**
     * Check index pattern aggregation restrictions and limit available aggTypes.
     */ ( aggType , indexPattern , aggConfig , aggFilter ) => { const aggRestrictions = indexPattern . getAggregationRestrictions ( ) ; if ( ! aggRestrictions ) { return true ; } const aggName = aggType . name ; return ( aggName === 'count' || ( ! ! aggRestrictions && Object . keys ( aggRestrictions ) . includes ( aggName ) ) || false ) ; } , ] ; export function filterAggTypes ( aggTypes , indexPattern , aggConfig , aggFilter ) { const allowedAggTypes = aggTypes . filter ( ( aggType ) => { const isAggTypeAllowed = filters . every ( ( filter ) => filter ( aggType , indexPattern , aggConfig , aggFilter ) ) ; return isAggTypeAllowed ; } ) ; return allowedAggTypes ; }