import {
    fromPairs,
    head,
    last,
    max,
    mapObjIndexed,
    sortBy,
    toPairs,
} from 'ramda'

export function calculateReserves (cumulative, reserveData, mineral, column, series) {
    const reserves = getReserves(reserveData, mineral)

    if (!reserves) {
        // console.debug('No reserves!')
        return {}
    }

    const [reserveYear, reserveAmount] = last(sortBy(head, toPairs(reserves)))
    const cumulativeOnReserveYear = cumulative.data[reserveYear][column]

    return mapObjIndexed((row, year) => {
        return fromPairs([
            ['Year', year],
            [series, max(0, reserveAmount - (row[column] - cumulativeOnReserveYear))]
        ])
    }, cumulative.data)
}

export function getReserves (reserves, mineral) {
    return reserves.data && reserves.data[mineral]
}

import {
    fromPairs,
    head,
    last,
    max,
    mapObjIndexed,
    sortBy,
    toPairs,
} from 'ramda'

export function calculateReserves (cumulative, reserveData, mineral, column, series) {
    const reserves = getReserves(reserveData, mineral)

    if (!reserves) {
        // console.debug('No reserves!')
        return {}
    }

    const [reserveYear, reserveAmount] = last(sortBy(head, toPairs(reserves)))
    const cumulativeOnReserveYear = cumulative.data[reserveYear][column]

    return mapObjIndexed((row, year) => {
        return fromPairs([
            ['Year', year],
            [series, max(1, reserveAmount - (row[column] - cumulativeOnReserveYear))]
        ])
    }, cumulative.data)
}

export function getReserves (reserves, mineral) {
    return reserves.data && reserves.data[mineral]
}

Revert "Change calculateReserves minimum value to zero"
Revert "Change calculateReserves minimum value to zero"

This reverts commit d84468b7631cf9c08d1f9d185f59f36c79d16d68.
