import { Selector } from 'react-redux' ; import { ChartType } from '../../..' ; import { SettingsSpec } from '../../../../specs' ; import { GlobalChartState } from '../../../../state/chart_state' ; import { createCustomCachedSelector } from '../../../../state/create_selector' ; import { getSettingsSpecSelector } from '../../../../state/selectors/get_settings_specs' ; import { IndexedGeometry , GeometryValue } from '../../../../utils/geometry' ; import { XYChartSeriesIdentifier } from '../../utils/series' ; import { getTooltipInfoAndGeometriesSelector , TooltipAndHighlightedGeoms , } from './get_tooltip_values_highlighted_geoms' ; interface Props { settings : SettingsSpec | undefined ; highlightedGeometries : IndexedGeometry [ ] ; } function isOverElement ( prevProps , nextProps ) { if ( ! nextProps || ! nextProps . settings || ! nextProps . settings . onElementOver ) { return false ; } const { highlightedGeometries : nextGeomValues } = nextProps ; const prevGeomValues = prevProps ?. highlightedGeometries ?? [ ] ; return ( nextGeomValues . length > 0 && ( nextGeomValues . length !== prevGeomValues . length || ! nextGeomValues . every ( ( { value : next } , index ) => { const prev = prevGeomValues [ index ] . value ; return prev && prev . x === next . x && prev . y === next . y && prev . accessor === next . accessor ; } ) ) ) ; } internal export function createOnElementOverCaller ( ) { let prevProps = null ; let selector = null ; return ( state ) => { if ( selector === null && state . chartType === ChartType . XYAxis ) { selector = createCustomCachedSelector ( [ getTooltipInfoAndGeometriesSelector , getSettingsSpecSelector ] , ( { highlightedGeometries } , settings ) : void => { const nextProps = { settings , highlightedGeometries , } ; if ( isOverElement ( prevProps , nextProps ) && settings . onElementOver ) { const elements = highlightedGeometries . map < [ GeometryValue , XYChartSeriesIdentifier ] > ( ( { value , seriesIdentifier } ) => [ value , seriesIdentifier ] ) ; settings . onElementOver ( elements ) ; } prevProps = nextProps ; } ) ; } if ( selector ) { selector ( state ) ; } } ; }