"use strict";

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

function _getRequireWildcardCache() {
  if ("function" != typeof WeakMap) return null;
  var e = new WeakMap;
  return _getRequireWildcardCache = function() {
    return e
  }, e
}

function _interopRequireWildcard(e) {
  if (e && e.__esModule) return e;
  if (null === e || "object" !== _typeof(e) && "function" != typeof e) return {
    default: e
  };
  var t = _getRequireWildcardCache();
  if (t && t.has(e)) return t.get(e);
  var n = {},
    r = Object.defineProperty && Object.getOwnPropertyDescriptor;
  for (var o in e)
    if (Object.prototype.hasOwnProperty.call(e, o)) {
      var i = r ? Object.getOwnPropertyDescriptor(e, o) : null;
      i && (i.get || i.set) ? Object.defineProperty(n, o, i) : n[o] = e[o]
    } return n.default = e, t && t.set(e, n), n
}

function _typeof(e) {
  "@babel/helpers - typeof";
  return (_typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(e) {
    return typeof e
  } : function(e) {
    return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e
  })(e)
}

function _classCallCheck(e, t) {
  if (!(e instanceof t)) throw new TypeError("Cannot call a class as a function")
}

function _defineProperties(e, t) {
  for (var n = 0; n < t.length; n++) {
    var r = t[n];
    r.enumerable = r.enumerable || !1, r.configurable = !0, "value" in r && (r.writable = !0), Object.defineProperty(e, r.key, r)
  }
}

function _createClass(e, t, n) {
  return t && _defineProperties(e.prototype, t), n && _defineProperties(e, n), e
}

function _inherits(e, t) {
  if ("function" != typeof t && null !== t) throw new TypeError("Super expression must either be null or a function");
  e.prototype = Object.create(t && t.prototype, {
    constructor: {
      value: e,
      writable: !0,
      configurable: !0
    }
  }), t && _setPrototypeOf(e, t)
}

function _setPrototypeOf(e, t) {
  return (_setPrototypeOf = Object.setPrototypeOf || function(e, t) {
    return e.__proto__ = t, e
  })(e, t)
}

function _createSuper(e) {
  var t = _isNativeReflectConstruct();
  return function() {
    var n, r = _getPrototypeOf(e);
    if (t) {
      var o = _getPrototypeOf(this).constructor;
      n = Reflect.construct(r, arguments, o)
    } else n = r.apply(this, arguments);
    return _possibleConstructorReturn(this, n)
  }
}

function _possibleConstructorReturn(e, t) {
  return !t || "object" !== _typeof(t) && "function" != typeof t ? _assertThisInitialized(e) : t
}

function _assertThisInitialized(e) {
  if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  return e
}

function _isNativeReflectConstruct() {
  if ("undefined" == typeof Reflect || !Reflect.construct) return !1;
  if (Reflect.construct.sham) return !1;
  if ("function" == typeof Proxy) return !0;
  try {
    return Date.prototype.toString.call(Reflect.construct(Date, [], function() {})), !0
  } catch (e) {
    return !1
  }
}

function _getPrototypeOf(e) {
  return (_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function(e) {
    return e.__proto__ || Object.getPrototypeOf(e)
  })(e)
}

function _defineProperty(e, t, n) {
  return t in e ? Object.defineProperty(e, t, {
    value: n,
    enumerable: !0,
    configurable: !0,
    writable: !0
  }) : e[t] = n, e
}
Object.defineProperty(exports, "__esModule", {
  value: !0
}), exports.default = void 0;
var _react = _interopRequireWildcard(require("react")),
  _propTypes = _interopRequireDefault(require("prop-types")),
  _classnames = _interopRequireDefault(require("classnames")),
  _amosTool = require("amos-tool"),
  _Pane = _interopRequireDefault(require("./Pane")),
  _utils = require("./_utils"),
  DEFAULT_SEPARATOR_SIZE = 4,
  per = 100,
  SplitPane = /*#__PURE__*/ function(e) {
    function t(e) {
      var r;
      return _classCallCheck(this, t), r = n.call(this, e), _defineProperty(_assertThisInitialized(r), "updateSecondarySize", function(e) {
        r.setState({
          secondaryPaneSize: e
        })
      }), _defineProperty(_assertThisInitialized(r), "handleResize", function() {
        if (r.separatorRef && !r.props.percentage) {
          var e = r.containerRef.getBoundingClientRect(),
            t = r.separatorRef.getBoundingClientRect(),
            n = r.calcSecondaryPaneSize(e, t, {
              left: t.left,
              top: t.top
            }, !1);
          r.updateSecondarySize(n)
        }
      }), _defineProperty(_assertThisInitialized(r), "handleMouseMove", function(e) {
        if (r.state.resizing) {
          var t = r.containerRef.getBoundingClientRect(),
            n = r.separatorRef.getBoundingClientRect(),
            o = r.calcSecondaryPaneSize(t, n, {
              left: e.clientX,
              top: e.clientY
            }, !0);
          (0, _utils.clearSelection)(), r.updateSecondarySize(o)
        }
      }), _defineProperty(_assertThisInitialized(r), "handleTouchMove", function(e) {
        r.handleMouseMove(e.changedTouches[0])
      }), _defineProperty(_assertThisInitialized(r), "handleSplitterMouseDown", function() {
        (0, _utils.clearSelection)(), r.setState({
          resizing: !0
        })
      }), _defineProperty(_assertThisInitialized(r), "handleMouseUp", function() {
        r.setState(function(e) {
          return e.resizing ? {
            resizing: !1
          } : null
        })
      }), r.state = {
        secondaryPaneSize: 0,
        resizing: !1
      }, r
    }
    _inherits(t, e);
    var n = _createSuper(t);
    return _createClass(t, [{
      key: "componentDidMount",
      value: function() {
        window.addEventListener("resize", this.handleResize), document.addEventListener("mouseup", this.handleMouseUp), document.addEventListener("mousemove", this.handleMouseMove), document.addEventListener("touchend", this.handleMouseUp), document.addEventListener("touchmove", this.handleTouchMove);
        var e, t = this.props.secondaryDefaultSize;
        if (_amosTool.utils.isUndefined(t)) {
          var n, r = this.containerRef.getBoundingClientRect();
          n = this.separatorRef ? this.separatorRef.getBoundingClientRect() : {
            width: DEFAULT_SEPARATOR_SIZE,
            height: DEFAULT_SEPARATOR_SIZE
          }, e = this.calcSecondaryPaneSize(r, n, {
            left: r.left + (r.width - n.width) / 2,
            top: r.top + (r.height - n.height) / 2
          }, !1)
        } else e = t;
        this.updateSecondarySize(e)
      }
    }, {
      key: "componentDidUpdate",
      value: function(e, t) {
        t.secondaryPaneSize !== this.state.secondaryPaneSize && this.props.onSecondarySizeChange && this.props.onSecondarySizeChange(this.state.secondaryPaneSize), t.resizing !== this.state.resizing && (this.state.resizing ? this.props.onDragStart && this.props.onDragStart(this.state.secondaryPaneSize) : this.props.onDragEnd && this.props.onDragEnd(this.state.secondaryPaneSize))
      }
    }, {
      key: "componentWillUnmount",
      value: function() {
        window.removeEventListener("resize", this.handleResize), document.removeEventListener("mouseup", this.handleMouseUp), document.removeEventListener("mousemove", this.handleMouseMove), document.removeEventListener("touchend", this.handleMouseUp), document.removeEventListener("touchmove", this.handleTouchMove)
      }
    }, {
      key: "calcSecondaryPaneSize",
      value: function(e, t, n, r) {
        var o, i, a, s = this.props,
          u = s.mainPaneIndex,
          c = s.percentage,
          l = s.mainPaneMinSize,
          p = s.secondaryMinSize;
        this.props.vertical ? (o = e.height, i = t.height, a = n.top - e.top) : (o = e.width, i = t.width, a = n.left - e.left), r && (a -= i / 2), a < 0 ? a = 0 : a > o - i && (a = o - i);
        var f;
        f = 1 === u ? a : o - i - a;
        var d = o - i - f;
        return c && (f = f * per / o, d = d * per / o, i = i * per / o, o = per), d < l ? f = Math.max(f - (l - d), 0) : f < p && (f = Math.min(o - i - l, p)), f
      }
    }, {
      key: "render",
      value: function() {
        var e, t = this,
          n = this.props,
          r = n.className,
          o = n.prefixCls,
          i = n.children,
          a = n.vertical,
          s = n.separatorType,
          u = n.mainPaneIndex,
          c = n.percentage,
          l = this.state.resizing,
          p = (0, _classnames.default)(o, r, (e = {}, _defineProperty(e, "".concat(o, "-vertical"), a), _defineProperty(e, "".concat(o, "-resizing"), l), e)),
          f = _react.default.Children.toArray(i).slice(0, 2);
        0 === f.length && f.push( /*#__PURE__*/ _react.default.createElement("div", null));
        for (var d = [], h = 0 !== u && 1 !== u ? 0 : u, y = 0; y < f.length; ++y) {
          var _ = !0,
            v = null;
          f.length > 1 && y !== h && (_ = !1, v = this.state.secondaryPaneSize), d.push( /*#__PURE__*/ _react.default.createElement(_Pane.default, {
            prefixCls: o,
            vertical: a,
            percentage: c,
            isMainPane: _,
            size: v
          }, f[y]))
        }
        var g = (0, _utils.getCursor)(s, a ? "vertical" : "horizontal"); /*#__PURE__*/
        /*#__PURE__*/
        return _react.default.createElement("div", {
          className: p,
          ref: function(e) {
            return t.containerRef = e
          }
        }, d[0], d.length > 1 && _react.default.createElement("div", {
          role: "separator",
          className: "".concat(o, "-separator"),
          style: g,
          ref: function(e) {
            return t.separatorRef = e
          },
          onMouseDown: this.handleSplitterMouseDown,
          onTouchStart: this.handleSplitterMouseDown
        }), d.length > 1 && d[1])
      }
    }]), t
  }(_react.Component);
SplitPane.propTypes = {
  prefixCls: _propTypes.default.string,
  className: _propTypes.default.string,
  // cortege: col-resize, row-resize;
  // move: move;
  // aspect: e-resize, w-resize;
  separatorType: _propTypes.default.oneOf(["cortege", "move", "aspect"]),
  vertical: _propTypes.default.bool,
  percentage: _propTypes.default.bool,
  mainPaneIndex: _propTypes.default.number,
  mainPaneMinSize: _propTypes.default.number,
  // 次面板初始大小
  secondaryDefaultSize: _propTypes.default.number,
  secondaryMinSize: _propTypes.default.number,
  onDragStart: _propTypes.default.func,
  onDragEnd: _propTypes.default.func,
  onSecondarySizeChange: _propTypes.default.func,
  children: _propTypes.default.arrayOf(_propTypes.default.node)
}, SplitPane.defaultProps = {
  prefixCls: "amos-splitpane",
  separatorType: "cortege",
  // 默认采用 行列 模式
  vertical: !1,
  percentage: !1,
  mainPaneIndex: 0,
  mainPaneMinSize: 0,
  secondaryDefaultSize: void 0,
  secondaryMinSize: 0,
  onDragStart: null,
  onDragEnd: null,
  onSecondarySizeChange: null,
  children: []
};
var _default = SplitPane;
exports.default = _default, module.exports = exports.default;
