import { ExpressionFunctionKibana , ExpressionFunctionKibanaContext } from '../../../data/public' ; import { buildExpression , buildExpressionFunction } from '../../../expressions/public' ; import { VisToExpressionAst } from '../types' ; import { queryToAst , filtersToAst } from '../../../data/common' ; internal export const toExpressionAst = async ( vis , params ) => { const { savedSearchId , searchSource } = vis . data ; const query = searchSource ?. getField ( 'query' ) ; let filters = searchSource ?. getField ( 'filter' ) ; if ( typeof filters === 'function' ) { filters = filters ( ) ; } const kibana = buildExpressionFunction < ExpressionFunctionKibana > ( 'kibana' , { } ) ; const kibanaContext = buildExpressionFunction < ExpressionFunctionKibanaContext > ( 'kibana_context' , { q : query && queryToAst ( query ) , filters : filters && filtersToAst ( filters ) , savedSearchId , } ) ; const ast = buildExpression ( [ kibana , kibanaContext ] ) ; const expression = ast . toAst ( ) ; if ( ! vis . type . toExpressionAst ) { throw new Error ( 'Visualization type definition should have toExpressionAst function defined' ) ; } const visExpressionAst = await vis . type . toExpressionAst ( vis , params ) ; expression . chain . push ( ... visExpressionAst . chain ) ; return expression ; } ;