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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var VueCompositionAPI = require('@vue/composition-api');

function useColumnResize(tableElmRef, refreshTable) {
  var resizeLineRef = VueCompositionAPI.ref();
  var resizeLineParams = {
    isDragging: false,
    draggingCol: null,
    draggingStart: 0
  };
  var resizeLineStyle = VueCompositionAPI.reactive({
    display: "none",
    left: "10px",
    height: "10px"
  });

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

    if (!resizeLineParams.isDragging) {
      if (targetBoundRect.width >= 30 && targetBoundRect.right - e.pageX <= 8) {
        target.style.cursor = "col-resize";
        resizeLineParams.draggingCol = target;
      } else {
        target.style.cursor = "";
        resizeLineParams.draggingCol = null;
      }
    }
  };

  var onColumnMousedown = function onColumnMousedown(e, col) {
    var _tableElmRef$value;

    if (!resizeLineParams.draggingCol) return;
    var target = e.target.closest("th");
    var targetBoundRect = target.getBoundingClientRect();
    var tableBoundRect = (_tableElmRef$value = tableElmRef.value) === null || _tableElmRef$value === void 0 ? void 0 : _tableElmRef$value.getBoundingClientRect();
    var resizeLinePos = targetBoundRect.right - tableBoundRect.left;
    var colLeft = targetBoundRect.left - tableBoundRect.left;
    var minColLen = 30;
    var minResizeLineLeft = colLeft + minColLen;
    resizeLineParams.isDragging = true;
    resizeLineParams.draggingStart = e.x;

    if (resizeLineRef !== null && resizeLineRef !== void 0 && resizeLineRef.value) {
      var _tableElmRef$value2;

      resizeLineStyle.display = "block";
      resizeLineStyle.left = "".concat(resizeLinePos, "px");
      resizeLineStyle.height = "".concat((_tableElmRef$value2 = tableElmRef.value) === null || _tableElmRef$value2 === void 0 ? void 0 : _tableElmRef$value2.clientHeight, "px");
    }

    var onDragEnd = function onDragEnd() {
      if (resizeLineParams.isDragging) {
        var width = parseInt(resizeLineStyle.left, 10) - colLeft;
        col.width = "".concat(Math.floor(width), "px");
        resizeLineParams.isDragging = false;
        resizeLineParams.draggingCol = 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) {
        resizeLineStyle.left = "".concat(Math.max(resizeLinePos + e2.x - resizeLineParams.draggingStart, minResizeLineLeft), "px");
      }
    };

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

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

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

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

exports["default"] = useColumnResize;
//# sourceMappingURL=useColumnResize.js.map
