import { findIndex , isEmpty } from 'lodash' ; import { IAggConfig } from 'src/plugins/data/public' ; import { AggsState } from './agg_group_state' ; import { Schema , getSchemaByName } from '../schemas' ; const isAggRemovable = ( agg , group , schemas ) => { const schema = getSchemaByName ( schemas , agg . schema ) ; const metricCount = group . reduce ( ( count , aggregation ) => ( aggregation . schema === agg . schema ? ++ count : count ) , 0 ) ; return metricCount > schema . min ; } ; const getEnabledMetricAggsCount = ( group ) => { return group . reduce ( ( count , aggregation ) => aggregation . schema === 'metric' && aggregation . enabled ? ++ count : count , 0 ) ; } ; const calcAggIsTooLow = ( agg , aggIndex , group , schemas ) => { const schema = getSchemaByName ( schemas , agg . schema ) ; if ( ! schema . mustBeFirst ) { return false ; } const firstDifferentSchema = findIndex ( group , ( aggr ) => { return aggr . schema !== agg . schema ; } ) ; if ( firstDifferentSchema === - 1 ) { return false ; } return aggIndex > firstDifferentSchema ; } ; function isInvalidAggsTouched ( aggsState ) { const invalidAggs = Object . values ( aggsState ) . filter ( ( agg ) => ! agg . valid ) ; if ( isEmpty ( invalidAggs ) ) { return false ; } return invalidAggs . every ( ( agg ) => agg . touched ) ; } export { isAggRemovable , calcAggIsTooLow , isInvalidAggsTouched , getEnabledMetricAggsCount } ;