import { OpenSearchDashboardsDatatable } from '../../../../../plugins/expressions/public' ; import { deserializeAggConfig } from '../../search/expressions' ; import { opensearchFilters , Filter } from '../../../public' ; import { getIndexPatterns } from '../../services' ; import type { ValueClickContext } from '../../../../embeddable/public' ; param { EventData [ 'table' ] } table param { number } columnIndex param { number } rowIndex return { array } const getOtherBucketFilterTerms = ( table , columnIndex , rowIndex ) => { if ( rowIndex === - 1 ) { return [ ] ; } const rows = table . rows . filter ( ( row ) => { return table . columns . every ( ( column , i ) => { return row [ column . id ] === table . rows [ rowIndex ] [ column . id ] || i >= columnIndex ; } ) ; } ) ; const terms = rows . map ( ( row ) => row [ table . columns [ columnIndex ] . id ] ) ; return [ ... new Set ( terms . filter ( ( term ) => { const notOther = term !== '__other__' ; const notMissing = term !== '__missing__' ; return notOther && notMissing ; } ) ) , ] ; } ; param { EventData [ 'table' ] } table param { number } columnIndex param { number } rowIndex param { string } cellValue return { Filter [ ] | undefined } const createFilter = async ( table , columnIndex , rowIndex ) => { if ( ! table || ! table . columns || ! table . columns [ columnIndex ] ) { return ; } const column = table . columns [ columnIndex ] ; if ( ! column . meta || ! column . meta . indexPatternId ) { return ; } const aggConfig = deserializeAggConfig ( { type : column . meta . type , aggConfigParams : column . meta . aggConfigParams ? column . meta . aggConfigParams : { } , indexPattern : await getIndexPatterns ( ) . get ( column . meta . indexPatternId ) , } ) ; let filter = [ ] ; const value = rowIndex > - 1 ? table . rows [ rowIndex ] [ column . id ] : null ; if ( value === null || value === undefined || ! aggConfig . isFilterable ( ) ) { return ; } if ( aggConfig . type . name === 'terms' && aggConfig . params . otherBucket ) { const terms = getOtherBucketFilterTerms ( table , columnIndex , rowIndex ) ; filter = aggConfig . createFilter ( value , { terms } ) ; } else { filter = aggConfig . createFilter ( value ) ; } if ( ! filter ) { return ; } if ( ! Array . isArray ( filter ) ) { filter = [ filter ] ; } return filter ; } ; public export const createFiltersFromValueClickAction = async ( { data , negate , } ) => { const filters = [ ] ; await Promise . all ( data . filter ( ( point ) => point ) . map ( async ( val ) => { const { table , column , row } = val ; const filter = ( await createFilter ( table , column , row ) ) || [ ] ; if ( filter ) { filter . forEach ( ( f ) => { if ( negate ) { f = opensearchFilters . toggleFilterNegated ( f ) ; } filters . push ( f ) ; } ) ; } } ) ) ; return opensearchFilters . mapAndFlattenFilters ( filters ) ; } ;