import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import { useState, useCallback } from 'react';
import { set } from 'dot-prop-immutable';
import ResizeAlign from "./ResizeAlign";
import ResizeHandle from "./ResizeHandle";
import Resizer from "./Resizer";
import ResizeArea from "./ResizeArea";
var defaultCols = {
  span: 24,
  offset: 0
};
export { ResizeAlign, ResizeHandle, Resizer, ResizeArea };
export var resizeHandler = function resizeHandler(props) {
  var refChildren = [];
  var element = props.element,
      updateElement = props.updateElement,
      resizeStart = props.resizeStart,
      resizeStop = props.resizeStop;
  var elements = element.elements;

  var _useState = useState(false),
      _useState2 = _slicedToArray(_useState, 2),
      resizing = _useState2[0],
      setResizing = _useState2[1];

  var _useState3 = useState(null),
      _useState4 = _slicedToArray(_useState3, 2),
      colSpan = _useState4[0],
      setColSpan = _useState4[1];

  var _useState5 = useState(null),
      _useState6 = _slicedToArray(_useState5, 2),
      colOffset = _useState6[0],
      setColOffset = _useState6[1];

  var _useState7 = useState(0),
      _useState8 = _slicedToArray(_useState7, 2),
      leftCols = _useState8[0],
      setLeftCols = _useState8[1];

  var onResizeStart = useCallback(function (childElement, childIndex) {
    resizeStart(); // @ts-ignore

    setResizing(true);
    setResizing({
      childElement: childElement,
      childIndex: childIndex
    });
    var colsLeft = 0;

    for (var i = elements.length - 1; i >= 0; i -= 1) {
      var _item$data, _item$data$settings;

      var item = elements[i];

      var _ref = ((_item$data = item.data) === null || _item$data === void 0 ? void 0 : (_item$data$settings = _item$data.settings) === null || _item$data$settings === void 0 ? void 0 : _item$data$settings.cols) || defaultCols,
          _ref$span = _ref.span,
          itemCol = _ref$span === void 0 ? 24 : _ref$span,
          _ref$offset = _ref.offset,
          itemOffset = _ref$offset === void 0 ? 0 : _ref$offset;

      if (item.id === childElement.id) {
        colsLeft += itemOffset;
      } else if (i < childIndex) {
        var _childElement$data, _childElement$data$se;

        var _ref2 = ((_childElement$data = childElement.data) === null || _childElement$data === void 0 ? void 0 : (_childElement$data$se = _childElement$data.settings) === null || _childElement$data$se === void 0 ? void 0 : _childElement$data$se.cols) || defaultCols,
            _ref2$span = _ref2.span,
            childSpan = _ref2$span === void 0 ? 24 : _ref2$span;

        if (colsLeft + itemCol + itemOffset + childSpan <= 24) {
          colsLeft += itemCol + itemOffset;
        } else {
          break;
        }
      }
    }

    setLeftCols(colsLeft);
  }, [elements, resizeStart]);
  var onResizeStop = useCallback(function () {
    setResizing(false);

    if (!isNaN(colSpan) || !isNaN(colOffset)) {
      var _childElement$data2, _childElement$data2$s;

      var childElement = resizing.childElement;

      var _ref3 = ((_childElement$data2 = childElement.data) === null || _childElement$data2 === void 0 ? void 0 : (_childElement$data2$s = _childElement$data2.settings) === null || _childElement$data2$s === void 0 ? void 0 : _childElement$data2$s.cols) || defaultCols,
          span = _ref3.span,
          _ref3$offset = _ref3.offset,
          offset = _ref3$offset === void 0 ? 0 : _ref3$offset;

      if (!isNaN(colSpan) && colSpan !== null && colSpan !== undefined) {
        childElement = set(childElement, 'data.settings.cols', {
          span: colSpan,
          offset: offset
        });
      } // if (!isNaN(colOffset)) {
      //   childElement = set(childElement, 'data.settings.cols', { span, offset: colOffset });
      // }


      updateElement({
        element: childElement
      });
    } // reset


    setColSpan(null);
    setColOffset(null);
    setLeftCols(0);
    resizeStop();
  }, [resizing, colSpan, colOffset, updateElement, resizeStop]);
  var onResize = useCallback(function (pointX, position) {
    var _childElement$data3, _childElement$data3$s;

    var targetCol = 0;
    var targetOffset = 0;
    var childElement = resizing.childElement;

    var _ref4 = ((_childElement$data3 = childElement.data) === null || _childElement$data3 === void 0 ? void 0 : (_childElement$data3$s = _childElement$data3.settings) === null || _childElement$data3$s === void 0 ? void 0 : _childElement$data3$s.cols) || defaultCols,
        _ref4$span = _ref4.span,
        childSpan = _ref4$span === void 0 ? 24 : _ref4$span,
        _ref4$offset = _ref4.offset,
        childOffset = _ref4$offset === void 0 ? 0 : _ref4$offset; // 获取拖动过程离鼠标最近的栅格列


    refChildren.some(function (item, index) {
      var _item$getBoundingClie = item.getBoundingClientRect(),
          _item$getBoundingClie2 = _item$getBoundingClie.left,
          left = _item$getBoundingClie2 === void 0 ? 0 : _item$getBoundingClie2; // 右->左


      if (position === 'right' && index > leftCols && Math.abs(left - pointX) <= 10) {
        targetCol = index - leftCols;
      } // 左->右


      if (position === 'left' && Math.abs(left - pointX) <= 10 && childSpan + childOffset - index > 0) {
        // targetCol = childSpan - (index + 1);
        targetOffset = index;
        targetCol = childSpan + childOffset - index;
      }

      return position === 'right' && index > leftCols && Math.abs(left - pointX) <= 10 || position === 'left' && Math.abs(left - pointX) <= 10 && childSpan + childOffset - index > 0;
    });

    if (!isNaN(targetCol) && targetCol > 0) {
      setColSpan(targetCol > 24 ? 24 : targetCol);

      if (targetOffset === 0) {
        setColOffset(0);
      }
    }

    if (!isNaN(targetOffset) && targetOffset > 0) {
      setColOffset(targetOffset);
    }
  }, [resizing, colSpan, leftCols, updateElement]);
  return {
    refChildren: refChildren,
    resizing: resizing,
    resizingCols: {
      span: colSpan,
      offset: colOffset,
      index: resizing ? resizing.childIndex : null
    },
    onResizeStart: onResizeStart,
    onResize: onResize,
    onResizeStop: onResizeStop
  };
};
//# sourceMappingURL=index.js.map