function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import React, { useMemo, useEffect } from 'react';
import * as R from 'ramda';
import { roundtenth } from '@jadesrochers/reacthelpers';
import { isBarHighlightedX } from '@jadesrochers/selectbox';
import { Rect, HighlightRect } from './svgfeatures.jsx';

// Creates Histogram bars on a plot providing there is xdata
// sets ymax once it calculates it, 
// Set up for selection by default, will do nothing unless 
// the selection is hooked up on the other end.

var getBins = R.curry((data, ticks) => {
  return R.pipe(R.drop(1), R.zip(R.init(ticks)), R.map(bin => ({
    data: R.filter(val => val > bin[0] && val <= bin[1])(data),
    x0: bin[0],
    x1: bin[1]
  })))(ticks);
});
var calcHistRects = R.curry((height, xscale, yscale) => R.pipe(R.map(bin => ({
  length: bin['data'].length,
  xmin: xscale(bin['x0']) + 0.5,
  xmax: xscale(bin['x1'])
})), R.map(bin => _objectSpread(_objectSpread({}, bin), {}, {
  width: roundtenth(bin.xmax - bin.xmin - 0.5)
})), R.map(bin => _objectSpread(_objectSpread({}, bin), {}, {
  yheight: roundtenth(yscale(bin.length))
})), R.map(bin => _objectSpread(_objectSpread({}, bin), {}, {
  yoffset: roundtenth(height - bin.yheight)
}))));
var HistBars = props => {
  var rectvals, hist, barmax, maxmin;
  if (props.xscale && props.yscale) {
    hist = getBins(props.xdata)(props.xscale.ticks(props.nbins));
    barmax = Math.max(...R.map(n => n.data.length)(hist));
    rectvals = calcHistRects(props.height, props.xscale, props.yscale)(hist);
    maxmin = R.map(bin => ({
      xmax: bin.xmax,
      xmin: bin.xmin
    }))(rectvals);
  } else {
    barmax = 10;
    maxmin = [0, 10];
  }
  useEffect(() => {
    props.ymaxSet(barmax);
    props.setPlotData(maxmin);
  }, [barmax]);
  /* useMemo(() => props.setPlotData(maxmin), [barmax]) */
  if (!props.xscale && !props.yscale) {
    return null;
  }
  var propsToChildren = R.map(bin => /*#__PURE__*/React.cloneElement(props.children, R.mergeRight(_objectSpread({}, props), {
    key: bin.xmin,
    fill: props.fill ? props.fill : undefined,
    width: bin.width,
    height: bin.yheight,
    xoffset: bin.xmin,
    yoffset: bin.yoffset,
    xmax: bin.xmax,
    children: undefined
  })))(rectvals);
  return /*#__PURE__*/React.createElement("g", null, propsToChildren);
};
var HighlightBars = props => {
  return /*#__PURE__*/React.createElement(HistBars, props, /*#__PURE__*/React.createElement(HighlightRect, {
    highlightfill: props.highlightfill ? props.highlightfill : undefined,
    isHighlighted: props.isHighlighted ? props.isHighlighted : isBarHighlightedX
  }));
};
var RegBars = props => {
  return /*#__PURE__*/React.createElement(HistBars, props, /*#__PURE__*/React.createElement(Rect, null));
};
export { HighlightBars, RegBars };