function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useEffect, useRef, useState } from 'react';
import classNames from 'classnames';
import DragBar from "./DragBar";
import useStyle from "./styles";
var DragContent = function DragContent(props) {
  var children = props.children,
    className = props.className,
    direction = props.direction,
    _props$moveMaxWidth = props.moveMaxWidth,
    moveMaxWidth = _props$moveMaxWidth === void 0 ? 0 : _props$moveMaxWidth,
    _props$moveMaxHeight = props.moveMaxHeight,
    moveMaxHeight = _props$moveMaxHeight === void 0 ? 0 : _props$moveMaxHeight,
    style = props.style,
    dragMouseDown = props.dragMouseDown,
    dragMouseMove = props.dragMouseMove,
    dragMouseUp = props.dragMouseUp;
  var componentName = 'spec-grid-dragContent';
  var _useStyle = useStyle(componentName),
    _useStyle2 = _slicedToArray(_useStyle, 2),
    wrapSSR = _useStyle2[0],
    hashId = _useStyle2[1];
  var divRef = useRef();
  var _useState = useState(0),
    _useState2 = _slicedToArray(_useState, 2),
    targetWidth = _useState2[0],
    setTargetWidth = _useState2[1];
  var _useState3 = useState(0),
    _useState4 = _slicedToArray(_useState3, 2),
    targetHeight = _useState4[0],
    setTargetHeight = _useState4[1];
  useEffect(function () {
    setTargetWidth(divRef.current.offsetWidth);
    setTargetHeight(divRef.current.offsetHeight);
  }, []);
  var onmousedown = function onmousedown(e, direction) {
    var parent = divRef.current;
    var nextNode = parent.nextSibling; //相邻兄弟节点
    var prevNode = parent.previousSibling; //相邻兄弟节点

    //获取前后兄弟节点宽高
    var prevWidth = 0,
      prevHeight = 0,
      nextWidth = 0,
      nextHeight = 0;
    if (prevNode) {
      prevWidth = prevNode.offsetWidth;
      prevHeight = prevNode.offsetHeight;
    }
    if (nextNode) {
      nextWidth = nextNode.offsetWidth;
      nextHeight = nextNode.offsetHeight;
    }
    //鼠标按下
    if (dragMouseDown) dragMouseDown(e, direction);
    var initWidth = parent.offsetWidth;
    var initHeight = parent.offsetHeight;
    // let initLeft = parent.offsetLeft;
    // let initTop = parent.offsetTop;

    var startX = e.clientX;
    var startY = e.clientY;
    document.onmousemove = function (e) {
      var currX = e.clientX;
      var currY = e.clientY;
      var disY = currY - startY;
      var disX = currX - startX;
      // @ts-ignore
      var hasT = /top/.test(direction);
      // @ts-ignore
      var hasB = /bottom/.test(direction);
      // @ts-ignore
      var hasL = /left/.test(direction);
      // @ts-ignore
      var hasR = /right/.test(direction);
      // @ts-ignore

      if (hasL || hasR) {
        var newWidth = initWidth + (hasL ? -disX : hasR ? disX : 0);
        var width = newWidth > 0 ? newWidth : 0;
        //获取兄弟节点可变动的宽度
        var changeWidth = hasL ? prevWidth : hasR ? nextWidth : 0;
        var maxWidth = changeWidth + initWidth;
        if (moveMaxWidth > 0 && maxWidth > moveMaxWidth) {
          maxWidth = moveMaxWidth;
        }
        //拉伸容器宽度不可小于初始宽度
        //判断拉伸宽度不可超过 拉伸容器宽度+变动的兄弟容器宽度或设置的最大宽度
        if (width > targetWidth && width <= maxWidth) {
          //左拉且有前兄弟节点即变化其宽度
          if (hasL && prevNode) {
            var newPrevWidth = prevWidth + (hasL ? disX : hasR ? -disX : 0);
            prevNode.style.width = newPrevWidth + 'px';
          }
          //右拉且有后兄弟节点即变化其宽度
          if (hasR && nextNode) {
            var newNextWidth = nextWidth + (hasL ? disX : hasR ? -disX : 0);
            nextNode.style.width = newNextWidth + 'px';
          }
          //设置拉伸容器的宽度
          parent.style.width = width + "px";
        }
      }
      if (hasT || hasB) {
        var newHeight = initHeight + (hasT ? -disY : hasB ? disY : 0);
        var height = newHeight > 0 ? newHeight : 0;
        //获取兄弟节点需要变动的高度
        var changeHeight = hasT ? prevHeight : hasB ? nextHeight : 0;
        var maxHeight = changeHeight + initHeight;
        if (moveMaxHeight > 0 && maxHeight > moveMaxHeight) {
          maxHeight = moveMaxHeight;
        }
        //拉伸容器高度不可小于初始高度
        //判断拉伸高度不可超过 拉伸容器高度+变动的兄弟容器高度或设置的最大高度
        if (height > targetHeight && height <= maxHeight) {
          //上拉且有前兄弟节点即变化其高度
          if (hasT && prevNode) {
            var newPrevHeight = prevHeight + (hasT ? disY : hasB ? -disY : 0);
            prevNode.style.height = newPrevHeight + 'px';
          }
          //下拉且有后兄弟节点即变化其高度
          if (hasB && nextNode) {
            var newNextHeight = nextHeight + (hasT ? disY : hasB ? -disY : 0);
            nextNode.style.height = newNextHeight + 'px';
          }
          //设置拉伸容器的高度
          parent.style.height = height + "px";
        }
      }
      //鼠标按下
      if (dragMouseMove) dragMouseMove(e, direction);
    };
    document.onmouseup = function (e) {
      document.onmousemove = document.onmouseup = null;
      //鼠标按下
      if (dragMouseUp) dragMouseUp(e, direction);
    };
  };
  return wrapSSR( /*#__PURE__*/React.createElement("div", {
    ref: divRef,
    className: classNames(className, componentName, hashId),
    style: style
  }, children, direction && direction.map(function (item, idx) {
    return /*#__PURE__*/React.createElement(DragBar, {
      key: idx,
      direction: item,
      onMouseDown: onmousedown
    });
  })));
};
export default DragContent;