"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _propTypes = _interopRequireDefault(require("prop-types"));
var _reactDom = _interopRequireDefault(require("react-dom"));
var _Browser = _interopRequireDefault(require("./Browser"));
var _react = _interopRequireDefault(require("react"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
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); }
var ReflexSplitter = /*#__PURE__*/function (_React$Component) {
  _inheritsLoose(ReflexSplitter, _React$Component);
  /////////////////////////////////////////////////////////
  //
  //
  /////////////////////////////////////////////////////////

  /////////////////////////////////////////////////////////
  //
  //
  /////////////////////////////////////////////////////////

  /////////////////////////////////////////////////////////
  //
  //
  /////////////////////////////////////////////////////////
  function ReflexSplitter(props) {
    var _this;
    _this = _React$Component.call(this, props) || this;
    _this.state = {
      active: false
    };
    _this.onMouseMove = _this.onMouseMove.bind(_assertThisInitialized(_this));
    _this.onMouseDown = _this.onMouseDown.bind(_assertThisInitialized(_this));
    _this.onMouseUp = _this.onMouseUp.bind(_assertThisInitialized(_this));
    _this.document = props.document;
    return _this;
  }

  /////////////////////////////////////////////////////////
  //
  //
  /////////////////////////////////////////////////////////
  var _proto = ReflexSplitter.prototype;
  _proto.componentDidMount = function componentDidMount() {
    if (!this.document) {
      return;
    }
    this.document.addEventListener("touchend", this.onMouseUp);
    this.document.addEventListener("mouseup", this.onMouseUp);
    this.document.addEventListener("mousemove", this.onMouseMove, {
      passive: false
    });
    this.document.addEventListener("touchmove", this.onMouseMove, {
      passive: false
    });
  }

  /////////////////////////////////////////////////////////
  //
  //
  /////////////////////////////////////////////////////////
  ;
  _proto.componentWillUnmount = function componentWillUnmount() {
    if (!this.document) {
      return;
    }
    this.document.removeEventListener("mouseup", this.onMouseUp);
    this.document.removeEventListener("touchend", this.onMouseUp);
    this.document.removeEventListener("mousemove", this.onMouseMove);
    this.document.removeEventListener("touchmove", this.onMouseMove);
  }

  /////////////////////////////////////////////////////////
  //
  //
  /////////////////////////////////////////////////////////
  ;
  _proto.onMouseMove = function onMouseMove(event) {
    if (this.state.active) {
      this.props.events.emit("splitter.resize", {
        splitter: this,
        event: event
      });
      if (this.props.onResize) {
        this.props.onResize({
          domElement: _reactDom["default"].findDOMNode(this),
          component: this
        });
      }
      event.stopPropagation();
      event.preventDefault();
    }
  }

  /////////////////////////////////////////////////////////
  //
  //
  /////////////////////////////////////////////////////////
  ;
  _proto.onMouseDown = function onMouseDown(event) {
    this.setState({
      active: true
    });
    if (this.props.onStartResize) {
      // cancels resize from controller
      // if needed by returning true
      // to onStartResize
      if (this.props.onStartResize({
        domElement: _reactDom["default"].findDOMNode(this),
        component: this
      })) {
        return;
      }
    }
    this.props.events.emit("splitter.startResize", {
      splitter: this,
      event: event
    });
  }

  /////////////////////////////////////////////////////////
  //
  //
  /////////////////////////////////////////////////////////
  ;
  _proto.onMouseUp = function onMouseUp(event) {
    if (this.state.active) {
      this.setState({
        active: false
      });
      if (this.props.onStopResize) {
        this.props.onStopResize({
          domElement: _reactDom["default"].findDOMNode(this),
          component: this
        });
      }
      this.props.events.emit("splitter.stopResize", {
        splitter: this,
        event: event
      });
    }
  }

  /////////////////////////////////////////////////////////
  //
  //
  /////////////////////////////////////////////////////////
  ;
  _proto.render = function render() {
    var classNames = ["reflex-splitter"].concat(this.props.className.split(" "));
    if (_Browser["default"].isMobile()) {
      classNames.push("reflex-thin");
    }
    if (this.state.active) {
      classNames.push("active");
    }
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: classNames.join(" "),
      onTouchStart: this.onMouseDown,
      onMouseDown: this.onMouseDown,
      style: this.props.style,
      id: this.props.id
    }, this.props.children);
  };
  return ReflexSplitter;
}(_react["default"].Component);
exports["default"] = ReflexSplitter;
_defineProperty(ReflexSplitter, "defaultProps", {
  document: typeof document === "undefined" ? null : document,
  onStartResize: null,
  onStopResize: null,
  propagate: false,
  onResize: null,
  className: "",
  style: {}
});
ReflexSplitter.propTypes = process.env.NODE_ENV !== "production" ? {
  children: _propTypes["default"].oneOfType([_propTypes["default"].arrayOf(_propTypes["default"].node), _propTypes["default"].node]),
  onStartResize: _propTypes["default"].func,
  onStopResize: _propTypes["default"].func,
  className: _propTypes["default"].string,
  propagate: _propTypes["default"].bool,
  onResize: _propTypes["default"].func,
  style: _propTypes["default"].object
} : {};
module.exports = exports.default;