import { Selector } from 'react-redux' ; import { ChartType } from '../../..' ; import { SeriesIdentifier } from '../../../../common/series_id' ; import { GlobalChartState } from '../../../../state/chart_state' ; import { createCustomCachedSelector } from '../../../../state/create_selector' ; import { getSettingsSpecSelector } from '../../../../state/selectors/get_settings_specs' ; import { Cell , isPickedCells } from '../../layout/types/viewmodel_types' ; import { getSpecOrNull } from './heatmap_spec' ; import { getPickedShapes } from './picked_shapes' ; function isOverElement ( prev = [ ] , next ) { if ( next . length === 0 ) { return ; } if ( next . length !== prev . length ) { return true ; } return ! next . every ( ( nextCell , index ) => { const prevCell = prev [ index ] ; if ( prevCell === null ) { return false ; } return nextCell . value === prevCell . value && nextCell . x === prevCell . x && nextCell . y === prevCell . y ; } ) ; } internal export function createOnElementOverCaller ( ) { let prevPickedShapes = [ ] ; let selector = null ; return ( state ) => { if ( selector === null && state . chartType === ChartType . Heatmap ) { selector = createCustomCachedSelector ( [ getSpecOrNull , getPickedShapes , getSettingsSpecSelector ] , ( spec , nextPickedShapes , settings ) : void => { if ( ! spec ) { return ; } if ( ! settings . onElementOver ) { return ; } if ( ! isPickedCells ( nextPickedShapes ) ) { return ; } if ( isOverElement ( prevPickedShapes , nextPickedShapes ) ) { const elements = nextPickedShapes . map < [ Cell , SeriesIdentifier ] > ( ( value ) => [ value , { specId : spec . id , key : `spec{${ spec . id }}` , } , ] ) ; settings . onElementOver ( elements ) ; } prevPickedShapes = nextPickedShapes ; } ) ; } if ( selector ) { selector ( state ) ; } } ; }