import { useState } from 'react';
import * as R from 'ramda';

// The limit object is designed to be passed to a Component that 
// is selecting data and one that is displaying it to configure 
// the data that shows up.
var useLimits = () => {
  var [xlimits, setxlimits] = useState({
    min: 0,
    max: 0
  });
  var [ylimits, setylimits] = useState({
    min: 0,
    max: 0
  });
  // The scale functions come from d3-scale; scale.invert()
  // gets the original values, which are used to check data against limits.
  var [xscale, setXscale] = useState(() => {
    var invert = n => n;
    return {
      invert: invert
    };
  });
  var [yscale, setYscale] = useState(() => {
    var invert = n => n;
    return {
      invert: invert
    };
  });
  var setLimits = R.curry((which, maxmin) => {
    var vals = R.values(maxmin);
    var scale, setlims;
    if (R.equals(which)('x')) {
      scale = xscale;
      setlims = setxlimits;
    } else {
      scale = yscale;
      setlims = setylimits;
    }
    var scaled = R.map(scale.invert)(vals);
    setlims({
      min: R.min(...scaled),
      max: R.max(...scaled)
    });
  });
  var setRawLims = (which, maxmin) => {
    var min = R.min(...maxmin),
      max = R.max(...maxmin);
    if (R.equals(which)('x')) {
      setxlimits({
        min: min,
        max: max
      });
    } else {
      setylimits({
        min: min,
        max: max
      });
    }
  };
  return {
    setLimits,
    setRawLims,
    setXscale,
    setYscale,
    xlimits,
    ylimits
  };
};
export { useLimits };