import { Selector } from 'reselect' ; import { ChartType } from '../../..' ; import { SeriesIdentifier } from '../../../../common/series_id' ; import { SettingsSpec } from '../../../../specs' ; import { GlobalChartState , PointerState } from '../../../../state/chart_state' ; import { createCustomCachedSelector } from '../../../../state/create_selector' ; import { getLastClickSelector } from '../../../../state/selectors/get_last_click' ; import { getSettingsSpecSelector } from '../../../../state/selectors/get_settings_specs' ; import { isClicking } from '../../../../state/utils' ; import { Cell , isPickedCells } from '../../layout/types/viewmodel_types' ; import { getSpecOrNull } from './heatmap_spec' ; import { getPickedShapes } from './picked_shapes' ; internal export function createOnElementClickCaller ( ) { let prevClick = null ; let selector = null ; return ( state ) => { if ( selector === null && state . chartType === ChartType . Heatmap ) { selector = createCustomCachedSelector ( [ getSpecOrNull , getLastClickSelector , getSettingsSpecSelector , getPickedShapes ] , ( spec , lastClick , settings , pickedShapes ) : void => { if ( ! spec ) { return ; } if ( ! settings . onElementClick ) { return ; } if ( ! isPickedCells ( pickedShapes ) ) { return ; } const nextPickedShapesLength = pickedShapes . length ; if ( nextPickedShapesLength > 0 && isClicking ( prevClick , lastClick ) && settings && settings . onElementClick ) { const elements = pickedShapes . map < [ Cell , SeriesIdentifier ] > ( ( value ) => [ value , { specId : spec . id , key : `spec{${ spec . id }}` , } , ] ) ; settings . onElementClick ( elements ) ; } prevClick = lastClick ; } ) ; } if ( selector ) { selector ( state ) ; } } ; }