'use strict';

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

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _mockData = require('@vx/mock-data');

var _mockData2 = _interopRequireDefault(_mockData);

var _scale = require('@vx/scale');

var _scale2 = _interopRequireDefault(_scale);

var _group = require('@vx/group');

var _group2 = _interopRequireDefault(_group);

var _axis = require('@vx/axis');

var _axis2 = _interopRequireDefault(_axis);

var _brush = require('@vx/brush');

var _brush2 = _interopRequireDefault(_brush);

var _sillyColorScale = require('../util/sillyColorScale');

var _sillyColorScale2 = _interopRequireDefault(_sillyColorScale);

var _reactMotion = require('react-motion');

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

var _jsxFileName = '/Users/hshoff/code/vx/packages/vx-demo/components/charts/BrushChart.js';


var points = _mockData2.default.genRandomNormalPoints();

var BrushChart = function (_React$Component) {
  (0, _inherits3.default)(BrushChart, _React$Component);

  function BrushChart(props) {
    (0, _classCallCheck3.default)(this, BrushChart);

    var _this = (0, _possibleConstructorReturn3.default)(this, (BrushChart.__proto__ || (0, _getPrototypeOf2.default)(BrushChart)).call(this, props));

    var width = props.width,
        height = props.height,
        margin = props.margin;

    _this.extent = {
      x0: margin.left,
      x1: width - margin.left,
      y0: margin.top,
      y1: height - margin.top
    };

    _this.initialDomain = {
      x: [-4.5, 4.5],
      y: [-4.5 / 2, 4.5 / 2]
    };

    _this.xScale = _scale2.default.scaleLinear({
      domain: _this.initialDomain.x,
      range: [0, width - margin.left - margin.right],
      clamp: true
    });

    _this.yScale = _scale2.default.scaleLinear({
      domain: _this.initialDomain.y,
      range: [height - margin.top - margin.bottom, 0],
      clamp: true
    });

    _this.handleMouseDown = _this.handleMouseDown.bind(_this);
    _this.handleMouseMove = _this.handleMouseMove.bind(_this);
    _this.handleMouseUp = _this.handleMouseUp.bind(_this);
    return _this;
  }

  (0, _createClass3.default)(BrushChart, [{
    key: 'scaleReset',
    value: function scaleReset() {
      var xScale = this.xScale,
          yScale = this.yScale,
          initialDomain = this.initialDomain;

      xScale.domain(initialDomain.x);
      yScale.domain(initialDomain.y);
    }
  }, {
    key: 'handleMouseDown',
    value: function handleMouseDown(event) {
      var onBrushStart = this.props.onBrushStart;
      var region = this.extent;

      var _Brush$getCoordsFromE = _brush2.default.getCoordsFromEvent(this.svg, event),
          x = _Brush$getCoordsFromE.x,
          y = _Brush$getCoordsFromE.y;

      onBrushStart(_brush2.default.constrainToRegion({ region: region, x: x, y: y }));
    }
  }, {
    key: 'handleMouseMove',
    value: function handleMouseMove(event) {
      var _props = this.props,
          brush = _props.brush,
          onBrushDrag = _props.onBrushDrag,
          updateBrush = _props.updateBrush;
      // only update the brush region if we're dragging

      if (!brush.isBrushing) return;
      var region = this.extent;

      var _Brush$getCoordsFromE2 = _brush2.default.getCoordsFromEvent(this.svg, event),
          x = _Brush$getCoordsFromE2.x,
          y = _Brush$getCoordsFromE2.y;

      onBrushDrag(_brush2.default.constrainToRegion({ region: region, x: x, y: y }));
    }
  }, {
    key: 'handleMouseUp',
    value: function handleMouseUp(event) {
      var _props2 = this.props,
          brush = _props2.brush,
          onBrushEnd = _props2.onBrushEnd,
          onBrushReset = _props2.onBrushReset;
      var region = this.extent;

      if (brush.end) {
        var _Brush$getCoordsFromE3 = _brush2.default.getCoordsFromEvent(this.svg, event),
            x = _Brush$getCoordsFromE3.x,
            y = _Brush$getCoordsFromE3.y;

        onBrushEnd(_brush2.default.constrainToRegion({ region: region, x: x, y: y }));
        return;
      }
      onBrushReset(event);
      this.scaleReset();
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      var _props3 = this.props,
          width = _props3.width,
          height = _props3.height,
          brush = _props3.brush,
          margin = _props3.margin;
      var xScale = this.xScale,
          yScale = this.yScale;

      var x = function x(d) {
        return d[0];
      };
      var y = function y(d) {
        return d[1];
      };
      var z = function z(d) {
        return d[2];
      };

      var xMax = width - margin.left - margin.right;
      var yMax = height - margin.top - margin.bottom;

      if (brush.domain) {
        var domain = brush.domain;
        var x0 = domain.x0,
            x1 = domain.x1,
            y0 = domain.y0,
            y1 = domain.y1;

        xScale.domain([x0, x1].map(function (d) {
          return d - margin.left;
        }).map(xScale.invert));
        yScale.domain([y1, y0].map(function (d) {
          return d - margin.top;
        }).map(yScale.invert));
      }

      return _react2.default.createElement('svg', {
        ref: function ref(c) {
          _this2.svg = c;
        },
        width: width,
        height: height,
        onMouseDown: this.handleMouseDown,
        onMouseMove: this.handleMouseMove,
        onMouseUp: this.handleMouseUp,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 99
        }
      }, _react2.default.createElement(_axis2.default.AxisBottom, {
        scale: xScale,
        top: yMax + margin.top,
        left: margin.left,
        label: '',
        stroke: '#1b1a1e',
        tickTextFill: '#1b1a1e',
        __source: {
          fileName: _jsxFileName,
          lineNumber: 107
        }
      }), _react2.default.createElement(_axis2.default.AxisLeft, {
        scale: yScale,
        top: margin.top,
        left: margin.left,
        label: '',
        stroke: '#1b1a1e',
        tickTextFill: '#1b1a1e',
        __source: {
          fileName: _jsxFileName,
          lineNumber: 115
        }
      }), _react2.default.createElement(_group2.default, { top: margin.top, left: margin.left, __source: {
          fileName: _jsxFileName,
          lineNumber: 123
        }
      }, points.map(function (point) {
        return _react2.default.createElement(_reactMotion.Motion, {
          key: x(point) + '-' + y(point) + '-' + z(point),
          defaultStyle: { x: xMax / 2, y: yMax / 2 },
          style: {
            x: (0, _reactMotion.spring)(xScale(x(point))),
            y: (0, _reactMotion.spring)(yScale(y(point)))
          }, __source: {
            fileName: _jsxFileName,
            lineNumber: 126
          }
        }, function (interpolatingStyle) {
          return _react2.default.createElement('circle', {
            fill: (0, _sillyColorScale2.default)(z(point)),
            cx: interpolatingStyle.x,
            cy: interpolatingStyle.y,
            r: 3,
            __source: {
              fileName: _jsxFileName,
              lineNumber: 135
            }
          });
        });
      })), _react2.default.createElement(_brush2.default.BoxBrush, { brush: brush, __source: {
          fileName: _jsxFileName,
          lineNumber: 147
        }
      }));
    }
  }]);

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

exports.default = _brush2.default.withBrush(BrushChart);