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))
}

import {
    filter,
    fromPairs,
    defaultTo,
    head,
    indexBy,
    is,
    keys,
    last,
    map,
    prop,
    range,
} from 'ramda'
import { productionSeries } from './csv'

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

    const production = productionSeries(data.series)
    const dataYears = filter((row) => is(Number, row[production]), data.data)
    const first = parseInt(head(keys(dataYears)))
    const latest = parseInt(last(keys(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))
}

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