import { getVisSchemas , VisToExpressionAst , SchemaConfig } from '../../../visualizations/public' ; import { buildExpression , buildExpressionFunction } from '../../../expressions/public' ; import { PaletteOutput } from '../../../charts/common' ; import { PIE_VIS_EXPRESSION_NAME , PIE_LABELS_FUNCTION , PieVisExpressionFunctionDefinition , PieVisParams , LabelsParams , } from '../../../chart_expressions/expression_pie/common' ; import { getEsaggsFn } from './to_ast_esaggs' ; const prepareDimension = ( params ) => { const visdimension = buildExpressionFunction ( 'visdimension' , { accessor : params . accessor } ) ; if ( params . format ) { visdimension . addArgument ( 'format' , params . format . id ) ; visdimension . addArgument ( 'formatParams' , JSON . stringify ( params . format . params ) ) ; } return buildExpression ( [ visdimension ] ) ; } ; const preparePalette = ( palette ? ) => { const paletteExpressionFunction = buildExpressionFunction ( 'system_palette' , { name : palette ?. name , } ) ; return buildExpression ( [ paletteExpressionFunction ] ) ; } ; const prepareLabels = ( params ) => { const pieLabels = buildExpressionFunction ( PIE_LABELS_FUNCTION , { show : params . show , lastLevel : params . last_level , values : params . values , truncate : params . truncate , } ) ; if ( params . position ) { pieLabels . addArgument ( 'position' , params . position ) ; } if ( params . valuesFormat ) { pieLabels . addArgument ( 'valuesFormat' , params . valuesFormat ) ; } if ( params . percentDecimals != null ) { pieLabels . addArgument ( 'percentDecimals' , params . percentDecimals ) ; } return buildExpression ( [ pieLabels ] ) ; } ; export const toExpressionAst = async ( vis , params ) => { const schemas = getVisSchemas ( vis , params ) ; const args = { addTooltip : vis . params . addTooltip , addLegend : vis . params . addLegend , legendPosition : vis . params . legendPosition , nestedLegend : vis . params ?. nestedLegend , truncateLegend : vis . params . truncateLegend , maxLegendLines : vis . params . maxLegendLines , distinctColors : vis . params ?. distinctColors , isDonut : vis . params . isDonut , emptySizeRatio : vis . params . emptySizeRatio , palette : preparePalette ( vis . params ?. palette ) , labels : prepareLabels ( vis . params . labels ) , metric : schemas . metric . map ( prepareDimension ) , buckets : schemas . segment ?. map ( prepareDimension ) , splitColumn : schemas . split_column ?. map ( prepareDimension ) , splitRow : schemas . split_row ?. map ( prepareDimension ) , } ; const visTypePie = buildExpressionFunction < PieVisExpressionFunctionDefinition > ( PIE_VIS_EXPRESSION_NAME , args ) ; const ast = buildExpression ( [ getEsaggsFn ( vis ) , visTypePie ] ) ; return ast . toAst ( ) ; } ;