import { get , hasIn } from 'lodash' ; import { FilterValueFormatter , RangeFilter , isScriptedRangeFilter , isRangeFilter , Filter , FILTERS , } from '../../../../../common' ; const getFormattedValueFn = ( left , right ) => { return ( formatter ? ) => { let displayValue = `${ left } to ${ right }` ; if ( formatter ) { const convert = formatter . getConverterFor ( 'text' ) ; displayValue = `${ convert ( left ) } to ${ convert ( right ) }` ; } return displayValue ; } ; } ; const getFirstRangeKey = ( filter ) => filter . range && Object . keys ( filter . range ) [ 0 ] ; const getRangeByKey = ( filter , key ) => get ( filter , [ 'range' , key ] ) ; function getParams ( filter ) { const isScriptedRange = isScriptedRangeFilter ( filter ) ; const key = ( isScriptedRange ? filter . meta . field : getFirstRangeKey ( filter ) ) || '' ; const params = isScriptedRange ? get ( filter , 'script.script.params' ) : getRangeByKey ( filter , key ) ; let left = hasIn ( params , 'gte' ) ? params . gte : params . gt ; if ( left == null ) left = - Infinity ; let right = hasIn ( params , 'lte' ) ? params . lte : params . lt ; if ( right == null ) right = Infinity ; const value = getFormattedValueFn ( left , right ) ; return { type : FILTERS . RANGE , key , value , params } ; } export const isMapRangeFilter = ( filter ) : filter is RangeFilter => isRangeFilter ( filter ) || isScriptedRangeFilter ( filter ) ; export const mapRange = ( filter ) => { if ( ! isMapRangeFilter ( filter ) ) { throw filter ; } return getParams ( filter ) ; } ;