Source: app/util/lineChartUtils/clubTenViewSettings.js

import { isBmiRangeSafe } from 'common/src/app/util/BmiUtils';
import formValueSelector from 'redux-form/lib/formValueSelector';
import FormNames from 'common/src/app/data/enum/FormNames';
import ProgressOverviewFilterFields from 'common/src/app/data/enum/FieldNames/ProgressOverviewFieldNames';
import { userProfileSelector } from 'common/src/app/selectors/userProfileSelectors';

const formFieldSelector = formValueSelector(FormNames.MY_PROFILE);

/**
 * Work out value and when to show the line that shows 90% of your initial weight
 * @param values object of values, doesn't have to be form values: { startWeight, targetWeight, height }
 * If the Club 10 line is below target or healthy BMI, don't show
 */
const clubTenViewSettings = (values, state) => {
  const clubTenWeight = values.startWeight * 0.9;
  const healthyBMI = isBmiRangeSafe(clubTenWeight, values.height);
  const userProfile = userProfileSelector(state) || {};
  const dontShow = { lineVisible: false, displayToggle: false };

  if (userProfile && userProfile.isPregnant) {
    return dontShow;
  }

  // Checkbox status
  const userFormStatus = formFieldSelector(state, ProgressOverviewFilterFields.CLUB_TEN);

  if (userFormStatus && healthyBMI && clubTenWeight > values.targetWeight) {
    return { lineVisible: true, displayToggle: true, value: clubTenWeight };
  } else if (!healthyBMI || clubTenWeight <= values.targetWeight) {
    return dontShow;
  }

  return { lineVisible: false, displayToggle: true };
};

export default clubTenViewSettings;