import { DailyWaterAmountsType , SelectedTreeType , WateringType , } from '../../common/interfaces' ; export const parseWaterings = ( waterings ) : { [ key ] : number ; } => { const parsedWaterings = { } ; if ( ! waterings ) return parsedWaterings ; waterings . forEach ( watering => { const { timestamp , amount } = watering ; parsedWaterings [ + new Date ( timestamp . split ( 'T' ) [ 0 ] ) ] = parsedWaterings [ + new Date ( timestamp . split ( 'T' ) [ 0 ] ) ] + amount || amount ; } ) ; return parsedWaterings ; } ; export const mapStackedBarchartData = ( selectedTree , today = new Date ( ) ) : DailyWaterAmountsType [ ] => { const date = new Date ( today . toISOString ( ) . split ( 'T' ) [ 0 ] ) ; const waterings = parseWaterings ( selectedTree . waterings ) ; const rainings = [ ... selectedTree . radolan_days ] . reverse ( ) ; return new Array ( 30 ) . fill ( null ) . map ( ( _ , i ) => { const timestamp = new Date ( date ) ; const id = + timestamp ; const wateringValue = waterings [ id ] || 0 ; const rainValue = rainings . slice ( 24 * ( i - 1 ) , 24 * i ) . reduce ( ( a , b ) => a + b , 0 ) ; date . setDate ( date . getDate ( ) - 1 ) ; return { id , timestamp , rainValue , wateringValue } ; } ) ; } ;