function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useEffect, useState } from 'react';
import { DndContext, useSensor, useSensors, PointerSensor } from '@dnd-kit/core';
import { SortableContext, verticalListSortingStrategy, arrayMove } from '@dnd-kit/sortable';
import TreeItem from "./TreeItem";
import { restrictToVerticalAxis, restrictToParentElement } from '@dnd-kit/modifiers';
import "./index.less";
var DragSortTree = function DragSortTree(props) {
  var value = props.value,
    rowKey = props.rowKey,
    tabKey = props.tabKey,
    hiddenKeys = props.hiddenKeys,
    onChange = props.onChange;
  var _useState = useState(value || []),
    _useState2 = _slicedToArray(_useState, 2),
    items = _useState2[0],
    setItems = _useState2[1];
  var _useState3 = useState([]),
    _useState4 = _slicedToArray(_useState3, 2),
    expandedKeys = _useState4[0],
    setExpandedKeys = _useState4[1];
  useEffect(function () {
    var keys = value === null || value === void 0 ? void 0 : value.map(function (item) {
      return item[rowKey];
    });
    // 过滤需要折叠起来的key
    keys = keys.filter(function (key) {
      return hiddenKeys !== null && hiddenKeys !== void 0 && hiddenKeys.length ? !hiddenKeys.includes(key) : key;
    });
    setExpandedKeys(_toConsumableArray(keys));
  }, [hiddenKeys]);
  useEffect(function () {
    // 防止切换的时候数据不更新，拖拽之后，如果切换tab，需要用更新之后的数据
    setItems(value || []);
  }, [tabKey]);
  var sensors = useSensors(useSensor(PointerSensor, {
    activationConstraint: {
      distance: 5
    }
  }));
  var handleDragStart = function handleDragStart(event) {
    var _active$id, _active$id$split;
    var active = event.active;
    var activeId = active === null || active === void 0 ? void 0 : (_active$id = active.id) === null || _active$id === void 0 ? void 0 : (_active$id$split = _active$id.split('-')) === null || _active$id$split === void 0 ? void 0 : _active$id$split[1];
    var _keys = expandedKeys === null || expandedKeys === void 0 ? void 0 : expandedKeys.filter(function (key) {
      return key != activeId;
    });
    setExpandedKeys(_toConsumableArray(_keys));
  };
  var handleDragEnd = function handleDragEnd(event) {
    var active = event.active,
      over = event.over;
    if (!over || !active) return;
    try {
      var _active$id2, _active$id2$split, _over$id, _over$id$split;
      var activeId = active === null || active === void 0 ? void 0 : (_active$id2 = active.id) === null || _active$id2 === void 0 ? void 0 : (_active$id2$split = _active$id2.split('-')) === null || _active$id2$split === void 0 ? void 0 : _active$id2$split[1];
      var overId = over === null || over === void 0 ? void 0 : (_over$id = over.id) === null || _over$id === void 0 ? void 0 : (_over$id$split = _over$id.split('-')) === null || _over$id$split === void 0 ? void 0 : _over$id$split[1];
      if (activeId !== overId) {
        // 如果没有自动点击折叠则拖动结束再次打开
        if (!(hiddenKeys !== null && hiddenKeys !== void 0 && hiddenKeys.includes(Number(activeId)))) {
          var _keys = [].concat(_toConsumableArray(expandedKeys), [Number(activeId)]);
          setExpandedKeys(_toConsumableArray(_keys));
        }
        var activeIndex = items.findIndex(function (item) {
          return item[rowKey] == activeId;
        });
        var overIndex = items.findIndex(function (item) {
          return item[rowKey] == overId;
        });
        var _lists = arrayMove(items, activeIndex, overIndex);
        handleChange(_lists);
      }
    } catch (error) {}
  };
  var handleChange = function handleChange(value) {
    setItems(value);
    onChange && onChange(value);
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode-drag-sort-tree-wrap"
  }, /*#__PURE__*/React.createElement(DndContext, {
    sensors: sensors,
    onDragEnd: handleDragEnd,
    onDragStart: handleDragStart,
    modifiers: [restrictToVerticalAxis, restrictToParentElement]
  }, /*#__PURE__*/React.createElement(SortableContext, {
    items: items.map(function (item) {
      return "0-".concat(item[rowKey]);
    }),
    strategy: verticalListSortingStrategy
  }, items.map(function (item) {
    return /*#__PURE__*/React.createElement(TreeItem, {
      key: "0-".concat(item[rowKey]),
      id: "0-".concat(item[rowKey]),
      item: item,
      depth: 0,
      sensors: sensors,
      childrenProps: props,
      lists: items,
      expandedKeys: expandedKeys,
      setExpandedKeys: setExpandedKeys,
      onChange: handleChange
    });
  }))));
};
export default DragSortTree;