import {
    fromPairs,
    defaultTo,
    head,
    indexBy,
    keys,
    last,
    map,
    prop,
    range,
} from 'ramda'

export function accumulateData (data, column, series, extraYears = 0) {
    let currentYear
    let total = 0

    const dataYears = keys(data.data)
    const first = parseInt(head(dataYears))
    const latest = parseInt(last(dataYears))
    const years = range(first, latest + extraYears)
    const latestData = data.data[latest][column]

    console.debug('Range:', first, latest, latestData, column, total, data)

    // TODO Write separate function that uses world production data until the estimation continues!
    return indexBy(prop('Year'), map((year) => {
        if (year <= latest) {
            currentYear = defaultTo(0, parseFloat(data.data[year][column]))
        } else {
            currentYear = latestData
        }
        total += currentYear

        return fromPairs([
            ['Year', year],
            [series, total],
        ])
    }, years))
}

@@ -2,16 +2,28 @@
 mport %7B%0A
+    filter,%0A
     from
@@ -29,16 +29,16 @@
 mPairs,%0A
-
     defa
@@ -67,16 +67,24 @@
 ndexBy,%0A
+    is,%0A
     keys
@@ -139,16 +139,57 @@
  'ramda'
+%0Aimport %7B productionSeries %7D from './csv'
 %0A%0Aexport
@@ -307,25 +307,118 @@
 nst 
-dataYears = keys(
+production = productionSeries(data.series)%0A    const dataYears = filter((row) =%3E is(Number, row%5Bproduction%5D), 
 data
@@ -456,16 +456,21 @@
 nt(head(
+keys(
 dataYear
@@ -464,32 +464,33 @@
 keys(dataYears))
+)
 %0A    const lates
@@ -507,16 +507,21 @@
 nt(last(
+keys(
 dataYear
@@ -523,16 +523,17 @@
 aYears))
+)
 %0A    con

Fix accumulateData for production series where latest year is missing a value