import { head, isNil, pathOr, sum } from 'ramda';
import getNumberFormatted from '../../../../../../../helpers/getNumberFormatted';
import parseLocaleNumber from '../../../../../../../helpers/parseLocaleNumber';

const LastRowCell = ({columnId, lastRowCfg, columnMeta = {}, getColumnDataFn}) => {
    const cellData = pathOr({}, [], head(lastRowCfg.filter(o => !isNil(o[columnId]))));
    let cellValue = cellData[columnId];

    if (columnMeta.enableFormula) {
        const getAllRowsValues = getColumnDataFn(columnId)
            .map(v => parseLocaleNumber(v));

        if (cellValue === 'sum') {
            cellValue = getNumberFormatted(sum(getAllRowsValues));
        } else {
            cellValue = 0;
        }
    }

    return <td>{cellValue}</td>;
};

export default LastRowCell;