'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

var _d3Selection = require('d3-selection');

var d3Selection = _interopRequireWildcard(_d3Selection);

var _d3Array = require('d3-array');

var d3Array = _interopRequireWildcard(_d3Array);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var d3 = Object.assign({}, d3Selection, d3Array);

var ChartOverlay = function (_React$Component) {
  _inherits(ChartOverlay, _React$Component);

  function ChartOverlay(props) {
    _classCallCheck(this, ChartOverlay);

    var _this = _possibleConstructorReturn(this, (ChartOverlay.__proto__ || Object.getPrototypeOf(ChartOverlay)).call(this, props));

    _this.mouseMove = _this.mouseMove.bind(_this);
    return _this;
  }

  _createClass(ChartOverlay, [{
    key: 'mouseMove',
    value: function mouseMove(e) {
      var _props = this.props,
          xScale = _props.xScale,
          data = _props.data;

      var bisectXAxis = d3.bisector(function (d) {
        return d.x;
      }).left;
      var node = _reactDom2.default.findDOMNode(this);

      var bounds = node.getBoundingClientRect();
      var x = Math.floor(Math.abs(e.clientX - bounds.left));
      var y = Math.floor(Math.abs(e.clientY - bounds.top));

      var x0 = xScale.invert(x);

      var i = bisectXAxis(data, x0, 1);
      var d0 = this.props.data[i - 1];
      var d1 = this.props.data[i];
      var d = x0 - d0.x > d1.x - x0 ? d1 : d0;

      // const toolTipData = {
      //   key: d[this.props.xData],
      //   value: d[yData],
      // };

      var toolTipPos = { x: x, y: y };

      this.props.mouseMove(d, toolTipPos);
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      var _props2 = this.props,
          height = _props2.height,
          hideToolTip = _props2.hideToolTip,
          width = _props2.width;

      return _react2.default.createElement('rect', {
        className: 'overlay',
        width: width,
        fill: 'none',
        stroke: 'none',
        style: { pointerEvents: 'all' },
        height: height,
        onMouseMove: function onMouseMove(e) {
          _this2.mouseMove(e);
        },
        onMouseOut: hideToolTip
      });
    }
  }]);

  return ChartOverlay;
}(_react2.default.Component);

ChartOverlay.propTypes = {
  height: _propTypes2.default.number.isRequired,
  width: _propTypes2.default.number.isRequired,
  hideToolTip: _propTypes2.default.func,
  mouseMove: _propTypes2.default.func.isRequired,
  xScale: _propTypes2.default.func.isRequired,
  yScale: _propTypes2.default.func.isRequired,
  data: _propTypes2.default.array.isRequired
};

exports.default = ChartOverlay;
//# sourceMappingURL=ChartOverlay.js.map