import { useCallback , useMemo , useState } from 'react' ; import { i18n } from '@kbn/i18n' ; import { EuiComboBoxOptionOption } from '@elastic/eui' ; import { LatestFunctionConfigUI } from '../../../../../../../common/types/transform' ; import { StepDefineFormProps } from '../step_define_form' ; import { StepDefineExposedState } from '../common' ; import { LatestFunctionConfig } from '../../../../../../../common/api_schemas/transforms' ; import { AggConfigs , FieldParamType } from '../../../../../../../../../../src/plugins/data/common' ; import { useAppDependencies } from '../../../../../app_dependencies' ; /**
 * Latest function config mapper between API and UI
 */ export const latestConfigMapper = { toAPIConfig ( uiConfig ) { return { unique_key : uiConfig . unique_key ?. length ? uiConfig . unique_key . map ( ( v ) => v . value ! ) : [ ] , sort : uiConfig . sort ?. value !== undefined ? uiConfig . sort . value ! : '' , } ; } , toUIConfig ( ) { } , } ; param indexPattern param aggConfigs param runtimeMappings function getOptions ( indexPattern , aggConfigs , runtimeMappings ? ) { const aggConfig = aggConfigs . aggs [ 0 ] ; const param = aggConfig . type . params . find ( ( p ) => p . type === 'field' ) ; const filteredIndexPatternFields = param ? ( param as unknown as FieldParamType ) . getAvailableFields ( aggConfig ) . filter ( ( ip ) => ip . runtimeField === undefined ) : [ ] ; const ignoreFieldNames = new Set ( [ '_source' , '_type' , '_index' , '_id' , '_version' , '_score' ] ) ; const runtimeFieldsOptions = runtimeMappings ? Object . keys ( runtimeMappings ) . map ( ( k ) => ( { label : k , value : k } ) ) : [ ] ; const uniqueKeyOptions = filteredIndexPatternFields . filter ( ( v ) => ! ignoreFieldNames . has ( v . name ) ) . map ( ( v ) => ( { label : v . displayName , value : v . name , } ) ) ; const runtimeFieldsSortOptions = runtimeMappings ? Object . entries ( runtimeMappings ) . filter ( ( [ fieldName , fieldMapping ] ) => fieldMapping . type === 'date' ) . map ( ( [ fieldName , fieldMapping ] ) => ( { label : fieldName , value : fieldName , } ) ) : [ ] ; const indexPatternFieldsSortOptions = indexPattern . fields . filter ( ( v ) => ! ignoreFieldNames . has ( v . name ) && v . sortable && v . type === 'date' ) . map ( ( v ) => ( { label : v . displayName , value : v . name , } ) ) ; const sortByLabel = ( a , b ) => a . label . localeCompare ( b . label ) ; return { uniqueKeyOptions : [ ... uniqueKeyOptions , ... runtimeFieldsOptions ] . sort ( sortByLabel ) , sortFieldOptions : [ ... indexPatternFieldsSortOptions , ... runtimeFieldsSortOptions ] . sort ( sortByLabel ) , } ; } /**
 * Validates latest function configuration
 */ export function validateLatestConfig ( config ? ) { const isValid = ! ! config ?. unique_key ?. length && typeof config ?. sort === 'string' && config ?. sort . length > 0 ; return { isValid , ... ( isValid ? { } : { errorMessage : i18n . translate ( 'xpack.transform.latestPreview.latestPreviewIncompleteConfigCalloutBody' , { defaultMessage : 'Please choose at least one unique key and sort field.' , } ) , } ) , } ; } export function useLatestFunctionConfig ( defaults , indexPattern , runtimeMappings ) { const [ config , setLatestFunctionConfig ] = useState < LatestFunctionConfigUI > ( { unique_key : defaults . unique_key , sort : defaults . sort , } ) ; const { data } = useAppDependencies ( ) ; const { uniqueKeyOptions , sortFieldOptions } = useMemo ( ( ) => { const aggConfigs = data . search . aggs . createAggConfigs ( indexPattern , [ { type : 'terms' } ] ) ; return getOptions ( indexPattern , aggConfigs , runtimeMappings ) ; } , [ indexPattern , data . search . aggs , runtimeMappings ] ) ; const updateLatestFunctionConfig = useCallback ( ( update ) => setLatestFunctionConfig ( { ... config , ... update , } ) , [ config ] ) ; const requestPayload = useMemo ( ( ) => { const latest = latestConfigMapper . toAPIConfig ( config ) ; return latest ? { latest } : undefined ; } , [ config ] ) ; const validationStatus = useMemo ( ( ) => validateLatestConfig ( requestPayload ?. latest ) , [ requestPayload ?. latest ] ) ; return { config , uniqueKeyOptions , sortFieldOptions , updateLatestFunctionConfig , validationStatus , requestPayload , } ; } export type LatestFunctionService = ReturnType < typeof useLatestFunctionConfig > ;