import { get } from 'lodash' ; import { PhraseFilter , FilterValueFormatter , getPhraseFilterValue , getPhraseFilterField , FILTERS , isScriptedPhraseFilter , Filter , isPhraseFilter , } from '../../../../../common' ; const getScriptedPhraseValue = ( filter ) => get ( filter , [ 'script' , 'script' , 'params' , 'value' ] ) ; const getFormattedValueFn = ( value ) => { return ( formatter ? ) => { return formatter ? formatter . convert ( value ) : value ; } ; } ; const getParams = ( filter ) => { const scriptedPhraseValue = getScriptedPhraseValue ( filter ) ; const isScriptedFilter = Boolean ( scriptedPhraseValue ) ; const key = isScriptedFilter ? filter . meta . field || '' : getPhraseFilterField ( filter ) ; const query = scriptedPhraseValue || getPhraseFilterValue ( filter ) ; const params = { query } ; return { key , params , type : FILTERS . PHRASE , value : getFormattedValueFn ( query ) , } ; } ; export const isMapPhraseFilter = ( filter ) : filter is PhraseFilter => isPhraseFilter ( filter ) || isScriptedPhraseFilter ( filter ) ; export const mapPhrase = ( filter ) => { if ( ! isMapPhraseFilter ( filter ) ) { throw filter ; } return getParams ( filter ) ; } ;