import { ChartDataPoint } from '@toptal/picasso-charts' ; import { Point , ReferenceLine } from './../../AnalyticsChart' ; import generateReferenceKey from '../generate-reference-key' ; const toChartFormat = ( data , referenceLines , xAxisKey , formatLabel ) => { const formattedData = [ ] ; const decoratePointWithRefData = ( point , dataKey ) => referenceLines ?. reduce ( ( point , { data } , index ) => { point [ generateReferenceKey ( index ) ] = data [ dataKey ] ; return point ; } , point ) || point ; data . forEach ( ( { id , values } ) => { Object . entries ( values ) . forEach ( ( [ key , value ] ) => { const label = formatLabel ( key ) ; const isEmptyValue = value === null ; const nonNullValue = value === null ? 0 : value ; const existingItem = formattedData . find ( ( { [ xAxisKey ] : existingLabel } ) => existingLabel === label ) ; if ( existingItem ) { existingItem [ id ] = nonNullValue ; if ( isEmptyValue ) { existingItem [ `${ id }IsEmpty` ] = true ; } return ; } const dataPoint = { [ xAxisKey ] : label , [ id ] : nonNullValue } ; if ( isEmptyValue ) { dataPoint [ `${ id }IsEmpty` ] = true ; } formattedData . push ( decoratePointWithRefData ( dataPoint , key ) ) ; } ) ; } ) ; return formattedData ; } ; export default toChartFormat ;