import { isEqual } from 'lodash' ; import { Dictionary } from '../../../../../../../common/types/common' ; import { PivotSupportedAggs } from '../../../../../../../common/types/pivot_aggs' ; import { isLatestTransform , isPivotTransform , TransformBaseConfig , } from '../../../../../../../common/types/transform' ; import { matchAllQuery , PivotAggsConfig , PivotAggsConfigDict , PivotGroupByConfig , PivotGroupByConfigDict , PIVOT_SUPPORTED_GROUP_BY_AGGS , } from '../../../../../common' ; import { StepDefineExposedState } from './types' ; import { getAggConfigFromEsAgg } from '../../../../../common/pivot_aggs' ; import { TRANSFORM_FUNCTION } from '../../../../../../../common/constants' ; import { StepDefineFormProps } from '../step_define_form' ; import { validateLatestConfig } from '../hooks/use_latest_function_config' ; import { validatePivotConfig } from '../hooks/use_pivot_config' ; import { getCombinedRuntimeMappings } from '../../../../../common/request' ; export function applyTransformConfigToDefineState ( state , transformConfig ? , indexPattern ? ) { state . runtimeMappings = getCombinedRuntimeMappings ( indexPattern , transformConfig ?. source ?. runtime_mappings ) ; if ( transformConfig === undefined ) { return state ; } if ( isPivotTransform ( transformConfig ) ) { state . transformFunction = TRANSFORM_FUNCTION . PIVOT ; state . aggList = Object . keys ( transformConfig . pivot . aggregations ) . reduce ( ( aggList , aggName ) => { const aggConfig = transformConfig . pivot . aggregations [ aggName as PivotSupportedAggs ] as Dictionary < any > ; aggList [ aggName ] = getAggConfigFromEsAgg ( aggConfig , aggName ) as PivotAggsConfig ; return aggList ; } , { } as PivotAggsConfigDict ) ; state . groupByList = Object . keys ( transformConfig . pivot . group_by ) . reduce ( ( groupByList , groupByName ) => { const groupByConfig = transformConfig . pivot . group_by [ groupByName ] as Dictionary < any > ; const groupBy = Object . keys ( groupByConfig ) [ 0 ] ; groupByList [ groupByName ] = { agg : groupBy as PIVOT_SUPPORTED_GROUP_BY_AGGS , aggName : groupByName , dropDownName : groupByName , ... groupByConfig [ groupBy ] , } as PivotGroupByConfig ; return groupByList ; } , { } as PivotGroupByConfigDict ) ; state . previewRequest = { pivot : transformConfig . pivot , } ; state . validationStatus = validatePivotConfig ( transformConfig . pivot ) ; } if ( isLatestTransform ( transformConfig ) ) { state . transformFunction = TRANSFORM_FUNCTION . LATEST ; state . latestConfig = { unique_key : transformConfig . latest . unique_key . map ( ( v ) => ( { value : v , label : indexPattern ? indexPattern . fields . find ( ( f ) => f . name === v ) ?. displayName ?? v : v , } ) ) , sort : { value : transformConfig . latest . sort , label : indexPattern ? indexPattern . fields . find ( ( f ) => f . name === transformConfig . latest . sort ) ?. displayName ?? transformConfig . latest . sort : transformConfig . latest . sort , } , } ; state . previewRequest = { latest : transformConfig . latest , } ; state . validationStatus = validateLatestConfig ( transformConfig . latest ) ; } const query = transformConfig . source . query ; if ( query !== undefined && ! isEqual ( query , matchAllQuery ) ) { state . isAdvancedSourceEditorEnabled = true ; state . searchQuery = query ; state . sourceConfigUpdated = true ; } state . valid = true ; return state ; }