import { LegendItemLabel } from '../../../../state/selectors/get_legend_items_labels' ; import { Layer } from '../../specs' ; import { CHILDREN_KEY , HIERARCHY_ROOT_KEY , HierarchyOfArrays } from './group_by_rollup' ; internal export function getLegendLabels ( layers , tree , legendMaxDepth ) { return flatSlicesNames ( layers , 0 , tree ) . filter ( ( { depth } ) => depth <= legendMaxDepth ) ; } function flatSlicesNames ( layers , depth , tree , keys = new Map ( ) ) { if ( tree . length === 0 ) { return [ ] ; } for ( const [ key , arrayNode ] of tree ) { const layer = layers [ depth - 1 ] ; const formatter = layer ?. nodeLabel ; const formattedValue = formatter ? formatter ( key ) : `${ key }` ; if ( formattedValue && formattedValue !== HIERARCHY_ROOT_KEY ) { keys . set ( formattedValue , Math . max ( depth , keys . get ( formattedValue ) ?? 0 ) ) ; } const children = arrayNode [ CHILDREN_KEY ] ; flatSlicesNames ( layers , depth + 1 , children , keys ) ; } return [ ... keys . keys ( ) ] . map ( ( k ) => ( { label : k , depth : keys . get ( k ) ?? 0 , } ) ) ; }