<commit_before>import { createSelector, createStructuredSelector } from 'reselect';
import isEmpty from 'lodash/isEmpty';
import { yearTicksFormatter } from 'components/widgets/utils/data';
import { formatNumber } from 'utils/format';

const getData = state => state.data || null;
const getSettings = state => state.settings || null;
const getLocationName = state => state.locationName || null;
const getSentences = state => state.config && state.config.sentences;
const getColors = state => state.colors || null;

export const parseData = createSelector(
  [getData, getSettings],
  (data, settings) => {
    if (isEmpty(data)) return null;
    const years = {};
    const selectedData = data[settings.unit];
    Object.keys(selectedData).forEach(key =>
      selectedData[key].forEach(obj => {
        if (years[obj.year]) years[obj.year][key] = obj.value;
        else years[obj.year] = { year: obj.year, [key]: obj.value };
      })
    );
    return Object.values(years);
  }
);

export const parseConfig = createSelector(
  [getData, getSettings, getColors],
  (data, settings, colors) => {
    if (isEmpty(data)) return null;
    const selectedData = data[settings.unit];
    const yKeys = {};
    Object.keys(selectedData).forEach((k, i) => {
      yKeys[k] = {
        fill: colors.ramp && colors.ramp[i],
        stackId: 1
      };
    });
    let tooltip = [
      {
        key: 'year'
      }
    ];
    const labels = {
      YSF: 'Young Secondary Forest',
      MASF: 'Mid-Age Secondary Forests'
    };
    tooltip = tooltip.concat(
      Object.keys(selectedData)
        .map((k, i) => ({
          key: k,
          label: labels[k] ? labels[k] : k,
          color: colors.ramp && colors.ramp[i]
        }))
        .reverse()
    );
    return {
      height: 250,
      xKey: 'year',
      yKeys: {
        bars: yKeys
      },
      xAxis: {
        ticksFormatter: yearTicksFormatter
      },
      tooltip
    };
  }
);

export const parseSentence = createSelector(
  [getSettings, getLocationName, getSentences, parseData],
  (settings, location, sentences, parsedData) => {
    if (isEmpty(parsedData)) return null;
    const maxYear = parsedData[parsedData.length - 1];
    const amount = Object.values(maxYear).reduce(
      (acc, n) => acc + n,
      -maxYear.year
    );
    const variables = {
      cGain: 'carbon',
      co2Gain: 'carbon dioxide'
    };
    const variable = variables[settings.unit];
    const { initial } = sentences;

    return {
      sentence: initial,
      params: {
        location,
        amount: formatNumber({ num: amount * 1000000, unit: 't' }),
        variable,
        maxYear: maxYear.year
      }
    };
  }
);

export const parseTitle = createSelector(
  [],
  () => 'Potential tree biomass gain'
);
<commit_msg>Changed carbon dioxide to CO2 and added format to units in tooltip<commit_after>import { createSelector, createStructuredSelector } from 'reselect';
import isEmpty from 'lodash/isEmpty';
import { yearTicksFormatter } from 'components/widgets/utils/data';
import { formatNumber } from 'utils/format';

const getData = state => state.data || null;
const getSettings = state => state.settings || null;
const getLocationName = state => state.locationName || null;
const getSentences = state => state.config && state.config.sentences;
const getColors = state => state.colors || null;

export const parseData = createSelector(
  [getData, getSettings],
  (data, settings) => {
    if (isEmpty(data)) return null;
    const years = {};
    const selectedData = data[settings.unit];
    Object.keys(selectedData).forEach(key =>
      selectedData[key].forEach(obj => {
        if (years[obj.year]) years[obj.year][key] = obj.value;
        else years[obj.year] = { year: obj.year, [key]: obj.value };
      })
    );
    return Object.values(years);
  }
);

export const parseConfig = createSelector(
  [getData, getSettings, getColors],
  (data, settings, colors) => {
    if (isEmpty(data)) return null;
    const selectedData = data[settings.unit];
    const yKeys = {};
    Object.keys(selectedData).forEach((k, i) => {
      yKeys[k] = {
        fill: colors.ramp && colors.ramp[i],
        stackId: 1
      };
    });
    let tooltip = [
      {
        key: 'year'
      }
    ];
    const labels = {
      YSF: 'Young Secondary Forest',
      MASF: 'Mid-Age Secondary Forests'
    };
    tooltip = tooltip.concat(
      Object.keys(selectedData)
        .map((k, i) => ({
          key: k,
          label: labels[k] ? labels[k] : k,
          color: colors.ramp && colors.ramp[i],
          unit: 't',
          unitFormat: num => formatNumber({ num, unit: '' })
        }))
        .reverse()
    );
    return {
      height: 250,
      xKey: 'year',
      yKeys: {
        bars: yKeys
      },
      xAxis: {
        ticksFormatter: yearTicksFormatter
      },
      tooltip
    };
  }
);

export const parseSentence = createSelector(
  [getSettings, getLocationName, getSentences, parseData],
  (settings, location, sentences, parsedData) => {
    if (isEmpty(parsedData)) return null;
    const maxYear = parsedData[parsedData.length - 1];
    const amount = Object.values(maxYear).reduce(
      (acc, n) => acc + n,
      -maxYear.year
    );
    const variables = {
      cGain: 'carbon',
      co2Gain: 'CO₂'
    };
    const variable = variables[settings.unit];
    const { initial } = sentences;

    return {
      sentence: initial,
      params: {
        location,
        amount: formatNumber({ num: amount * 1000000, unit: 't' }),
        variable,
        maxYear: maxYear.year
      }
    };
  }
);

export const parseTitle = createSelector(
  [],
  () => 'Potential tree biomass gain'
);

{'fraction_non_alphanumeric': 0.138464241785275, 'fraction_numerical': 0.004744333157617291, 'mean_word_length': 2.5731324544883867, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1698162', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1739, 'n_words': 427}