import { useMemo } from 'react' ; import { isEmpty } from 'lodash' ; import { TypedLensByValueInput } from '../../../../../../lens/public' ; import { LayerConfig , LensAttributes } from '../configurations/lens_attributes' ; import { AllSeries , allSeriesKey , convertAllShortSeries , reportTypeKey , useSeriesStorage , } from './use_series_storage' ; import { getDefaultConfigs } from '../configurations/default_configs' ; import { ReportViewType , SeriesUrl , UrlFilter } from '../types' ; import { IndexPatternState , useAppIndexPatternContext } from './use_app_index_pattern' ; import { ALL_VALUES_SELECTED } from '../../field_value_suggestions/field_value_combobox' ; import { useTheme } from '../../../../hooks/use_theme' ; import { EuiTheme } from '../../../../../../../../src/plugins/kibana_react/common' ; import { LABEL_FIELDS_BREAKDOWN } from '../configurations/constants' ; import { ReportConfigMap , useExploratoryView } from '../contexts/exploratory_view_config' ; export const getFiltersFromDefs = ( reportDefinitions ) => { return Object . entries ( reportDefinitions ?? { } ) . map ( ( [ field , value ] ) => { return { field , values : value , } ; } ) . filter ( ( { values } ) => ! values . includes ( ALL_VALUES_SELECTED ) ) as UrlFilter [ ] ; } ; export function getLayerConfigs ( allSeries , reportType , theme , indexPatterns , reportConfigMap ) { const layerConfigs = [ ] ; allSeries . forEach ( ( series , seriesIndex ) => { const indexPattern = indexPatterns ?. [ series ?. dataType ] ; if ( indexPattern && ! isEmpty ( series . reportDefinitions ) && ! series . hidden && series . selectedMetricField ) { const seriesConfig = getDefaultConfigs ( { reportType , indexPattern , dataType : series . dataType , reportConfigMap , } ) ; const filters = ( series . filters ?? [ ] ) . concat ( getFiltersFromDefs ( series . reportDefinitions ) ) ; const color = `euiColorVis${ seriesIndex }` ; layerConfigs . push ( { filters , indexPattern , seriesConfig , time : series . time , name : series . name , breakdown : series . breakdown === LABEL_FIELDS_BREAKDOWN ? undefined : series . breakdown , seriesType : series . seriesType , operationType : series . operationType , reportDefinitions : series . reportDefinitions ?? { } , selectedMetricField : series . selectedMetricField , color : series . color ?? ( theme . eui as unknown as Record < string , string > ) [ color ] , } ) ; } } ) ; return layerConfigs ; } export const useLensAttributes = ( ) : TypedLensByValueInput [ 'attributes' ] | null => { const { storage , allSeries , lastRefresh , reportType } = useSeriesStorage ( ) ; const { indexPatterns } = useAppIndexPatternContext ( ) ; const { reportConfigMap } = useExploratoryView ( ) ; const theme = useTheme ( ) ; return useMemo ( ( ) => { const allSeriesT = convertAllShortSeries ( storage . get ( allSeriesKey ) ?? [ ] ) ; const reportTypeT = storage . get ( reportTypeKey ) as ReportViewType ; if ( isEmpty ( indexPatterns ) || isEmpty ( allSeriesT ) || ! reportTypeT ) { return null ; } const layerConfigs = getLayerConfigs ( allSeriesT , reportTypeT , theme , indexPatterns , reportConfigMap ) ; if ( layerConfigs . length < 1 ) { return null ; } const lensAttributes = new LensAttributes ( layerConfigs ) ; return lensAttributes . getJSON ( lastRefresh ) ; } , [ indexPatterns , reportType , storage , theme , lastRefresh , allSeries ] ) ; } ;