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

import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { toRefs, ref, computed, watch } from '@vue/composition-api';
import Sortable from 'sortablejs';
import get from 'lodash/get';
import useClassName from './useClassName.js';
import '../../_common/js/log/index.js';
import swapDragArrayElement from '../../_common/js/utils/swapDragArrayElement.js';
import log from '../../_common/js/log/log.js';
import '../../config-provider/useConfig.js';
import 'lodash/cloneDeep';
import 'lodash/mergeWith';
import '../../config-provider/context.js';
import 'lodash/merge';
import '../../_common/js/global-config/default-config.js';
import '../../_common/js/global-config/locale/zh_CN.js';
import '../../config-provider/type.js';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function useDragSort(props, context) {
  var _toRefs = toRefs(props),
      sortOnRowDraggable = _toRefs.sortOnRowDraggable,
      dragSort = _toRefs.dragSort,
      columns = _toRefs.columns,
      data = _toRefs.data;

  var _useClassName = useClassName(),
      tableDraggableClasses = _useClassName.tableDraggableClasses,
      tableBaseClass = _useClassName.tableBaseClass;

  var primaryTableRef = ref(null);
  var dragCol = computed(function () {
    return columns.value.find(function (item) {
      return item.colKey === "drag";
    });
  });
  var isRowDraggable = computed(function () {
    return sortOnRowDraggable.value || dragSort.value === "row";
  });
  var isRowHandlerDraggable = computed(function () {
    return ["drag-col", "row-handler"].includes(dragSort.value) && !!dragCol.value;
  });
  var isColDraggable = computed(function () {
    return dragSort.value === "col";
  });
  var lastRowList = ref([]);
  var lastColList = ref([]);

  if (dragSort.value === "drag-col") {
    log.error("Table", "dragSort='drag-col' is going to be deprecated, please use dragSort='col' instead.");
  }

  if (props.sortOnRowDraggable) {
    log.error("Table", "`sortOnRowDraggable` is going to be deprecated, please use dragSort='row' instead.");
  }

  watch(data, function (data2) {
    lastRowList.value = data2 === null || data2 === void 0 ? void 0 : data2.map(function (item) {
      return get(item, props.rowKey);
    });
  }, {
    immediate: true
  });
  watch(columns, function (columns2) {
    lastColList.value = columns2 || [];
  }, {
    immediate: true
  });

  var registerRowDragEvent = function registerRowDragEvent(element) {
    if (!isRowHandlerDraggable.value && !isRowDraggable.value) return;
    var dragContainer = element === null || element === void 0 ? void 0 : element.querySelector("tbody");

    if (!dragContainer) {
      console.error("tbody does not exist.");
      return null;
    }

    var dragInstanceTmp = null;

    var baseOptions = _objectSpread(_objectSpread({
      animation: 150
    }, props.dragSortOptions), {}, {
      ghostClass: tableDraggableClasses.ghost,
      chosenClass: tableDraggableClasses.chosen,
      dragClass: tableDraggableClasses.dragging,
      onEnd: function onEnd(evt) {
        var _dragInstanceTmp, _props$onDragSort;

        (_dragInstanceTmp = dragInstanceTmp) === null || _dragInstanceTmp === void 0 ? void 0 : _dragInstanceTmp.sort(lastRowList.value);
        var currentIndex = evt.oldIndex,
            targetIndex = evt.newIndex;
        var params = {
          currentIndex: currentIndex,
          current: data.value[currentIndex],
          targetIndex: targetIndex,
          target: data.value[targetIndex],
          currentData: swapDragArrayElement(props.data, currentIndex, targetIndex),
          e: evt,
          sort: "row"
        };
        (_props$onDragSort = props.onDragSort) === null || _props$onDragSort === void 0 ? void 0 : _props$onDragSort.call(props, params);
        context.emit("drag-sort", params);
      }
    });

    if (isRowDraggable.value) {
      dragInstanceTmp = new Sortable(dragContainer, _objectSpread({}, baseOptions));
    } else {
      dragInstanceTmp = new Sortable(dragContainer, _objectSpread(_objectSpread({}, baseOptions), {}, {
        handle: ".".concat(tableDraggableClasses.handle)
      }));
    }

    lastRowList.value = dragInstanceTmp.toArray();
  };

  var registerColDragEvent = function registerColDragEvent(tableElement) {
    var _dragInstanceTmp3;

    if (!isColDraggable.value || !tableElement) return;
    var dragInstanceTmp = null;

    var options = _objectSpread(_objectSpread({
      animation: 150
    }, props.dragSortOptions), {}, {
      dataIdAttr: "data-colkey",
      direction: "vertical",
      ghostClass: tableDraggableClasses.ghost,
      chosenClass: tableDraggableClasses.chosen,
      dragClass: tableDraggableClasses.dragging,
      handle: ".".concat(tableBaseClass.thCellInner),
      onEnd: function onEnd(evt) {
        var _dragInstanceTmp2, _props$onDragSort2;

        (_dragInstanceTmp2 = dragInstanceTmp) === null || _dragInstanceTmp2 === void 0 ? void 0 : _dragInstanceTmp2.sort(_toConsumableArray(lastColList.value));
        var currentIndex = evt.oldIndex,
            targetIndex = evt.newIndex;
        var params = {
          currentIndex: currentIndex,
          current: columns.value[currentIndex],
          targetIndex: targetIndex,
          target: columns.value[targetIndex],
          currentData: swapDragArrayElement(columns.value, currentIndex, targetIndex),
          e: evt,
          sort: "col"
        };
        (_props$onDragSort2 = props.onDragSort) === null || _props$onDragSort2 === void 0 ? void 0 : _props$onDragSort2.call(props, params);
        context.emit("drag-sort", params);
      }
    });

    var container = tableElement.querySelector("thead > tr");
    dragInstanceTmp = new Sortable(container, options);
    lastColList.value = (_dragInstanceTmp3 = dragInstanceTmp) === null || _dragInstanceTmp3 === void 0 ? void 0 : _dragInstanceTmp3.toArray();
  };

  function setDragSortPrimaryTableRef(primaryTableElement) {
    primaryTableRef.value = primaryTableElement;
  }

  watch([primaryTableRef], function (_ref) {
    var _ref2 = _slicedToArray(_ref, 1),
        val = _ref2[0];

    (val === null || val === void 0 ? void 0 : val.$el) && registerRowDragEvent(val === null || val === void 0 ? void 0 : val.$el);
    (val === null || val === void 0 ? void 0 : val.$el) && registerColDragEvent(val === null || val === void 0 ? void 0 : val.$el);
  });
  return {
    isRowDraggable: isRowDraggable,
    isRowHandlerDraggable: isRowHandlerDraggable,
    isColDraggable: isColDraggable,
    setDragSortPrimaryTableRef: setDragSortPrimaryTableRef
  };
}

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