/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { getColWidthAttr } from './utils.js';
import 'lodash/isFunction';
import 'lodash/isNumber';

var findAllChildren = function findAllChildren(col) {
  var loopQue = [];
  var result = [];
  if (col.children) {
    col.children.forEach(function (child) {
      return loopQue.push(child);
    });
    while (loopQue.length) {
      var child = loopQue.shift();
      if (!child.children || !child.children.length) {
        result.push(child);
      } else {
        child.children.forEach(function (child2) {
          return loopQue.push(child2);
        });
      }
    }
  }
  return result;
};
function setThWidthListByColumnDrag(dragCol, dragWidth, effectCol, options, callback) {
  var getThWidthList = options.getThWidthList,
    DEFAULT_MIN_WIDTH = options.DEFAULT_MIN_WIDTH;
  var thWidthList = getThWidthList();
  var dragChildrenCols = findAllChildren(dragCol);
  var effectChildrenCols = findAllChildren(effectCol);
  if (dragChildrenCols.length || effectChildrenCols.length) {
    var _effectCol$resize;
    var oldWidth = 0;
    var oldEffectWidth = 0;
    var notCalculateCols = [];
    var effectColsMinWidth = 0;
    var updateMap = {};
    if (!dragChildrenCols.length) {
      dragChildrenCols.push(dragCol);
    }
    if (!effectChildrenCols.length) {
      effectChildrenCols.push(effectCol);
    }
    dragChildrenCols.forEach(function (child) {
      oldWidth += thWidthList[child.colKey] || getColWidthAttr(child, "width");
      notCalculateCols.push(child.colKey);
    });
    effectChildrenCols.forEach(function (child) {
      var _child$resize;
      oldEffectWidth += thWidthList[child.colKey] || getColWidthAttr(child, "width");
      notCalculateCols.push(child.colKey);
      effectColsMinWidth += Math.max(((_child$resize = child.resize) === null || _child$resize === void 0 ? void 0 : _child$resize.minWidth) || DEFAULT_MIN_WIDTH, getColWidthAttr(child, "minWidth") || DEFAULT_MIN_WIDTH);
    });
    dragChildrenCols.forEach(function (child) {
      updateMap[child.colKey] = thWidthList[child.colKey] / oldWidth * dragWidth;
    });
    var remainWidth = Math.max(effectColsMinWidth, oldWidth + oldEffectWidth - dragWidth, Math.max(getColWidthAttr(effectCol, "minWidth") || DEFAULT_MIN_WIDTH, ((_effectCol$resize = effectCol.resize) === null || _effectCol$resize === void 0 ? void 0 : _effectCol$resize.minWidth) || DEFAULT_MIN_WIDTH));
    effectChildrenCols.forEach(function (child) {
      var _child$resize2;
      updateMap[child.colKey] = Math.max(((_child$resize2 = child.resize) === null || _child$resize2 === void 0 ? void 0 : _child$resize2.minWidth) || DEFAULT_MIN_WIDTH, getColWidthAttr(child, "minWidth") || DEFAULT_MIN_WIDTH, thWidthList[child.colKey] / oldEffectWidth * remainWidth);
    });
    callback(updateMap, notCalculateCols);
  } else {
    var _effectCol$resize2, _callback;
    var _oldWidth = thWidthList[dragCol.colKey] || getColWidthAttr(dragCol, "width");
    var _oldEffectWidth = thWidthList[effectCol.colKey] || getColWidthAttr(effectCol, "width");
    callback((_callback = {}, _defineProperty(_callback, dragCol.colKey, dragWidth), _defineProperty(_callback, effectCol.colKey, Math.max(((_effectCol$resize2 = effectCol.resize) === null || _effectCol$resize2 === void 0 ? void 0 : _effectCol$resize2.minWidth) || DEFAULT_MIN_WIDTH, getColWidthAttr(effectCol, "minWidth") || DEFAULT_MIN_WIDTH, _oldWidth + _oldEffectWidth - dragWidth)), _callback), [dragCol.colKey, effectCol.colKey]);
  }
}

export { setThWidthListByColumnDrag as default };
//# sourceMappingURL=set-column-width-by-drag.js.map
