var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/drag-sort-tree/index.tsx
var drag_sort_tree_exports = {};
__export(drag_sort_tree_exports, {
  default: () => drag_sort_tree_default
});
module.exports = __toCommonJS(drag_sort_tree_exports);
var import_react = __toESM(require("react"));
var import_core = require("@dnd-kit/core");
var import_sortable = require("@dnd-kit/sortable");
var import_TreeItem = __toESM(require("./TreeItem"));
var import_modifiers = require("@dnd-kit/modifiers");
var import_index = require("./index.less");
var DragSortTree = (props) => {
  const { value, rowKey, tabKey, hiddenKeys, onChange } = props;
  const [items, setItems] = (0, import_react.useState)(value || []);
  const [expandedKeys, setExpandedKeys] = (0, import_react.useState)([]);
  (0, import_react.useEffect)(() => {
    let keys = value == null ? void 0 : value.map((item) => item[rowKey]);
    keys = keys.filter(
      (key) => (hiddenKeys == null ? void 0 : hiddenKeys.length) ? !hiddenKeys.includes(key) : key
    );
    setExpandedKeys([...keys]);
  }, [hiddenKeys]);
  (0, import_react.useEffect)(() => {
    setItems(value || []);
  }, [tabKey]);
  const sensors = (0, import_core.useSensors)(
    (0, import_core.useSensor)(import_core.PointerSensor, {
      activationConstraint: {
        distance: 5
      }
    })
  );
  const handleDragStart = (event) => {
    var _a, _b;
    const { active } = event;
    const activeId = (_b = (_a = active == null ? void 0 : active.id) == null ? void 0 : _a.split("-")) == null ? void 0 : _b[1];
    const _keys = expandedKeys == null ? void 0 : expandedKeys.filter((key) => key != activeId);
    setExpandedKeys([..._keys]);
  };
  const handleDragEnd = (event) => {
    var _a, _b, _c, _d;
    const { active, over } = event;
    if (!over || !active) return;
    try {
      let activeId = (_b = (_a = active == null ? void 0 : active.id) == null ? void 0 : _a.split("-")) == null ? void 0 : _b[1];
      let overId = (_d = (_c = over == null ? void 0 : over.id) == null ? void 0 : _c.split("-")) == null ? void 0 : _d[1];
      if (activeId !== overId) {
        if (!(hiddenKeys == null ? void 0 : hiddenKeys.includes(Number(activeId)))) {
          const _keys = [...expandedKeys, Number(activeId)];
          setExpandedKeys([..._keys]);
        }
        const activeIndex = items.findIndex(
          (item) => item[rowKey] == activeId
        );
        const overIndex = items.findIndex(
          (item) => item[rowKey] == overId
        );
        const _lists = (0, import_sortable.arrayMove)(items, activeIndex, overIndex);
        handleChange(_lists);
      }
    } catch (error) {
    }
  };
  const handleChange = (value2) => {
    setItems(value2);
    onChange && onChange(value2);
  };
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode-drag-sort-tree-wrap" }, /* @__PURE__ */ import_react.default.createElement(
    import_core.DndContext,
    {
      sensors,
      onDragEnd: handleDragEnd,
      onDragStart: handleDragStart,
      modifiers: [import_modifiers.restrictToVerticalAxis, import_modifiers.restrictToParentElement]
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_sortable.SortableContext,
      {
        items: items.map((item) => `0-${item[rowKey]}`),
        strategy: import_sortable.verticalListSortingStrategy
      },
      items.map((item) => /* @__PURE__ */ import_react.default.createElement(
        import_TreeItem.default,
        {
          key: `0-${item[rowKey]}`,
          id: `0-${item[rowKey]}`,
          item,
          depth: 0,
          sensors,
          childrenProps: props,
          lists: items,
          expandedKeys,
          setExpandedKeys,
          onChange: handleChange
        }
      ))
    )
  ));
};
var drag_sort_tree_default = DragSortTree;
