import TreeItem from "./TreeItem/index.js";
import React, { useEffect, useState } from "react";
import { DndContext, PointerSensor, useSensor, useSensors } from "@dnd-kit/core";
import { SortableContext, arrayMove, verticalListSortingStrategy } from "@dnd-kit/sortable";
import { restrictToParentElement, restrictToVerticalAxis } from "@dnd-kit/modifiers";
//#region src/components/drag-sort-tree/index.tsx
const DragSortTree = (props) => {
	const { value, rowKey, tabKey, hiddenKeys, onChange } = props;
	const [items, setItems] = useState(value || []);
	const [expandedKeys, setExpandedKeys] = useState([]);
	useEffect(() => {
		let keys = value === null || value === void 0 ? void 0 : value.map((item) => item[rowKey]);
		keys = keys.filter((key) => (hiddenKeys === null || hiddenKeys === void 0 ? void 0 : hiddenKeys.length) ? !hiddenKeys.includes(key) : key);
		setExpandedKeys([...keys]);
	}, [hiddenKeys]);
	useEffect(() => {
		setItems(value || []);
	}, [tabKey]);
	const sensors = useSensors(useSensor(PointerSensor, { activationConstraint: { distance: 5 } }));
	const handleDragStart = (event) => {
		var _active$id;
		const { active } = event;
		const activeId = active === null || active === void 0 || (_active$id = active.id) === null || _active$id === void 0 || (_active$id = _active$id.split("-")) === null || _active$id === void 0 ? void 0 : _active$id[1];
		setExpandedKeys([...expandedKeys === null || expandedKeys === void 0 ? void 0 : expandedKeys.filter((key) => key != activeId)]);
	};
	const handleDragEnd = (event) => {
		const { active, over } = event;
		if (!over || !active) return;
		try {
			var _active$id2, _over$id;
			let activeId = active === null || active === void 0 || (_active$id2 = active.id) === null || _active$id2 === void 0 || (_active$id2 = _active$id2.split("-")) === null || _active$id2 === void 0 ? void 0 : _active$id2[1];
			let overId = over === null || over === void 0 || (_over$id = over.id) === null || _over$id === void 0 || (_over$id = _over$id.split("-")) === null || _over$id === void 0 ? void 0 : _over$id[1];
			if (activeId !== overId) {
				if (!(hiddenKeys === null || hiddenKeys === void 0 ? void 0 : hiddenKeys.includes(Number(activeId)))) setExpandedKeys([...[...expandedKeys, Number(activeId)]]);
				handleChange(arrayMove(items, items.findIndex((item) => item[rowKey] == activeId), items.findIndex((item) => item[rowKey] == overId)));
			}
		} catch (error) {}
	};
	const handleChange = (value) => {
		setItems(value);
		onChange && onChange(value);
	};
	return /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode-drag-sort-tree-wrap" }, /* @__PURE__ */ React.createElement(DndContext, {
		sensors,
		onDragEnd: handleDragEnd,
		onDragStart: handleDragStart,
		modifiers: [restrictToVerticalAxis, restrictToParentElement]
	}, /* @__PURE__ */ React.createElement(SortableContext, {
		items: items.map((item) => `0-${item[rowKey]}`),
		strategy: verticalListSortingStrategy
	}, items.map((item) => /* @__PURE__ */ React.createElement(TreeItem, {
		key: `0-${item[rowKey]}`,
		id: `0-${item[rowKey]}`,
		item,
		depth: 0,
		sensors,
		childrenProps: props,
		lists: items,
		expandedKeys,
		setExpandedKeys,
		onChange: handleChange
	})))));
};
//#endregion
export { DragSortTree as default };
