/**
 * tdesign v0.46.4
 * (c) 2022 tdesign
 * @license MIT
 */

import { ref, reactive } from '@vue/composition-api';
import isNumber from 'lodash/isNumber';
import setThWidthListByColumnDrag from '../../_common/js/table/set-column-width-by-drag.js';
import recalculateColumnWidth from '../../_common/js/table/recalculate-column-width.js';
import '@babel/runtime/helpers/defineProperty';
import '../../_common/js/table/utils.js';
import 'lodash/isFunction';

var DEFAULT_MIN_WIDTH = 80;
var DEFAULT_MAX_WIDTH = 600;
function useColumnResize(tableContentRef, refreshTable, getThWidthList, updateThWidthList) {
  var resizeLineRef = ref();
  var notCalculateWidthCols = ref([]);
  var effectColMap = ref({});

  var setEffectColMap = function setEffectColMap(nodes, parent) {
    if (!nodes) return;
    nodes.forEach(function (n, index) {
      var parentPrevCol = parent ? effectColMap.value[parent.colKey].prev : nodes[index + 1];
      var parentNextCol = parent ? effectColMap.value[parent.colKey].next : nodes[index - 1];
      var prev = index === 0 ? parentPrevCol : nodes[index - 1];
      var next = index === nodes.length - 1 ? parentNextCol : nodes[index + 1];
      effectColMap.value[n.colKey] = {
        prev: prev,
        next: next
      };
      setEffectColMap(n.children, n);
    });
  };

  var resizeLineParams = {
    isDragging: false,
    draggingCol: null,
    draggingStart: 0,
    effectCol: null
  };
  var resizeLineStyle = reactive({
    display: "none",
    height: "10px",
    left: "10px",
    bottom: "0"
  });

  var setNotCalculateWidthCols = function setNotCalculateWidthCols(colKeys) {
    notCalculateWidthCols.value = colKeys;
  };

  var onColumnMouseover = function onColumnMouseover(e) {
    if (!resizeLineRef.value) return;
    var target = e.target.closest("th");
    var targetBoundRect = target.getBoundingClientRect();

    if (!resizeLineParams.isDragging) {
      var distance = 8;

      if (targetBoundRect.right - e.pageX <= distance) {
        target.style.cursor = "col-resize";
        resizeLineParams.draggingCol = target;
        resizeLineParams.effectCol = "next";
      } else if (e.pageX - targetBoundRect.left <= distance) {
        var prevEl = target.previousElementSibling;

        if (prevEl) {
          target.style.cursor = "col-resize";
          resizeLineParams.draggingCol = prevEl;
          resizeLineParams.effectCol = "prev";
        } else {
          target.style.cursor = "";
          resizeLineParams.draggingCol = null;
          resizeLineParams.effectCol = null;
        }
      } else {
        target.style.cursor = "";
        resizeLineParams.draggingCol = null;
        resizeLineParams.effectCol = null;
      }
    }
  };

  var onColumnMousedown = function onColumnMousedown(e, col) {
    var _tableContentRef$valu;

    if (!resizeLineParams.draggingCol) return;

    var getMinMaxColWidth = function getMinMaxColWidth(col2, effectPrevCol2) {
      var _targetCol$resize, _targetCol$resize2;

      var targetCol = null;

      if (resizeLineParams.effectCol === "next") {
        targetCol = col2;
      } else {
        targetCol = effectPrevCol2;
      }

      var propMinWidth = isNumber(targetCol.minWidth) ? targetCol.minWidth : parseFloat(targetCol.minWidth);
      return {
        minColWidth: Math.max(((_targetCol$resize = targetCol.resize) === null || _targetCol$resize === void 0 ? void 0 : _targetCol$resize.minWidth) || DEFAULT_MIN_WIDTH, propMinWidth || DEFAULT_MIN_WIDTH),
        maxColWidth: ((_targetCol$resize2 = targetCol.resize) === null || _targetCol$resize2 === void 0 ? void 0 : _targetCol$resize2.maxWidth) || DEFAULT_MAX_WIDTH
      };
    };

    var target = resizeLineParams.draggingCol;
    var targetBoundRect = target.getBoundingClientRect();
    var tableBoundRect = (_tableContentRef$valu = tableContentRef.value) === null || _tableContentRef$valu === void 0 ? void 0 : _tableContentRef$valu.getBoundingClientRect();
    var resizeLinePos = targetBoundRect.right - tableBoundRect.left;
    var colLeft = targetBoundRect.left - tableBoundRect.left;
    var effectNextCol = effectColMap.value[col.colKey].next;
    var effectPrevCol = effectColMap.value[col.colKey].prev;

    var _getMinMaxColWidth = getMinMaxColWidth(col, effectPrevCol),
        minColWidth = _getMinMaxColWidth.minColWidth,
        maxColWidth = _getMinMaxColWidth.maxColWidth;

    var minResizeLineLeft = colLeft + minColWidth;
    var maxResizeLineLeft = colLeft + maxColWidth;
    resizeLineParams.isDragging = true;
    resizeLineParams.draggingStart = e.x;

    if (resizeLineRef !== null && resizeLineRef !== void 0 && resizeLineRef.value) {
      resizeLineStyle.display = "block";
      resizeLineStyle.height = "".concat(tableBoundRect.bottom - targetBoundRect.top, "px");
      resizeLineStyle.left = "".concat(resizeLinePos, "px");
      var parent = tableContentRef.value.parentElement.getBoundingClientRect();
      resizeLineStyle.bottom = "".concat(parent.bottom - tableBoundRect.bottom, "px");
    }

    var onDragEnd = function onDragEnd() {
      if (resizeLineParams.isDragging) {
        var width = Math.ceil(parseInt(resizeLineStyle.left, 10) - colLeft) || 0;

        if (width <= minColWidth) {
          width = minColWidth;
        } else if (width >= maxColWidth) {
          width = maxColWidth;
        }

        if (resizeLineParams.effectCol === "next") {
          setThWidthListByColumnDrag(col, width, effectNextCol, {
            getThWidthList: getThWidthList,
            DEFAULT_MIN_WIDTH: DEFAULT_MIN_WIDTH
          }, function (updateMap, notCalculateCols) {
            updateThWidthList(updateMap);
            setNotCalculateWidthCols(notCalculateCols);
          });
        } else if (resizeLineParams.effectCol === "prev") {
          setThWidthListByColumnDrag(effectPrevCol, width, col, {
            getThWidthList: getThWidthList,
            DEFAULT_MIN_WIDTH: DEFAULT_MIN_WIDTH
          }, function (updateMap, notCalculateCols) {
            updateThWidthList(updateMap);
            setNotCalculateWidthCols(notCalculateCols);
          });
        }

        resizeLineParams.isDragging = false;
        resizeLineParams.draggingCol = null;
        resizeLineParams.effectCol = null;
        target.style.cursor = "";
        resizeLineStyle.display = "none";
        resizeLineStyle.left = "0";
        document.removeEventListener("mousemove", onDragOver);
        document.removeEventListener("mouseup", onDragEnd);
        document.onselectstart = null;
        document.ondragstart = null;
      }

      refreshTable();
    };

    var onDragOver = function onDragOver(e2) {
      if (resizeLineParams.isDragging) {
        var left = resizeLinePos + e2.x - resizeLineParams.draggingStart;
        resizeLineStyle.left = "".concat(Math.min(Math.max(left, minResizeLineLeft), maxResizeLineLeft), "px");
      }
    };

    document.addEventListener("mouseup", onDragEnd);
    document.addEventListener("mousemove", onDragOver);

    document.onselectstart = function () {
      return false;
    };

    document.ondragstart = function () {
      return false;
    };
  };

  var recalculateColWidth = function recalculateColWidth(columns, thWidthList, tableLayout, tableElmWidth) {
    recalculateColumnWidth(columns, thWidthList, tableLayout, tableElmWidth, notCalculateWidthCols.value, function (widthMap) {
      updateThWidthList(widthMap);

      if (notCalculateWidthCols.value.length) {
        notCalculateWidthCols.value = [];
      }
    });
  };

  return {
    resizeLineRef: resizeLineRef,
    resizeLineStyle: resizeLineStyle,
    onColumnMouseover: onColumnMouseover,
    onColumnMousedown: onColumnMousedown,
    recalculateColWidth: recalculateColWidth,
    setEffectColMap: setEffectColMap
  };
}

export { useColumnResize as default };
//# sourceMappingURL=useColumnResize.js.map
