import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from "react";

var Resizer = /*#__PURE__*/function (_React$Component) {
  _inherits(Resizer, _React$Component);

  var _super = _createSuper(Resizer);

  function Resizer() {
    var _this;

    _classCallCheck(this, Resizer);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));

    _defineProperty(_assertThisInitialized(_this), "state", {
      dragging: false
    });

    _defineProperty(_assertThisInitialized(_this), "startPosition", void 0);

    _defineProperty(_assertThisInitialized(_this), "getMousePosition", function (e) {
      return _this.props.axis === "x" ? e.pageX : e.pageY;
    });

    _defineProperty(_assertThisInitialized(_this), "onMouseDown", function (e) {
      e.preventDefault();
      e.stopPropagation();
      _this.startPosition = _this.getMousePosition(e);

      _this.setState({
        dragging: true
      }, function () {
        document.addEventListener("mousemove", _this.onMouseMove);
        document.addEventListener("mouseup", _this.onMouseUp);
      });

      _this.props.onResizeStart();
    });

    _defineProperty(_assertThisInitialized(_this), "onMouseUp", function () {
      if (_this.state.dragging) {
        _this.setState({
          dragging: false
        });

        _this.props.onResizeStop();
      }
    });

    _defineProperty(_assertThisInitialized(_this), "onMouseMove", function (e) {
      if (!_this.state.dragging) {
        return;
      }

      var mousePosition = _this.getMousePosition(e); // this.props.onResize(this.startPosition - mousePosition);


      _this.props.onResize(mousePosition);

      _this.startPosition = mousePosition;
    });

    return _this;
  }

  _createClass(Resizer, [{
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      document.removeEventListener("mousemove", this.onMouseMove);
      document.removeEventListener("mouseup", this.onMouseUp);
    }
  }, {
    key: "render",
    value: function render() {
      return this.props.children({
        isResizing: this.state.dragging,
        onMouseDown: this.onMouseDown
      });
    }
  }]);

  return Resizer;
}(React.Component);

export default Resizer;
//# sourceMappingURL=Resizer.js.map