const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_index = require("./TreeItem/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _dnd_kit_core = require("@dnd-kit/core");
let _dnd_kit_sortable = require("@dnd-kit/sortable");
let _dnd_kit_modifiers = require("@dnd-kit/modifiers");
//#region src/components/drag-sort-tree/index.tsx
const DragSortTree = (props) => {
	const { value, rowKey, tabKey, hiddenKeys, onChange } = props;
	const [items, setItems] = (0, react.useState)(value || []);
	const [expandedKeys, setExpandedKeys] = (0, react.useState)([]);
	(0, react.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]);
	(0, react.useEffect)(() => {
		setItems(value || []);
	}, [tabKey]);
	const sensors = (0, _dnd_kit_core.useSensors)((0, _dnd_kit_core.useSensor)(_dnd_kit_core.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((0, _dnd_kit_sortable.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.default.createElement("div", { className: "pisell-lowcode-drag-sort-tree-wrap" }, /* @__PURE__ */ react.default.createElement(_dnd_kit_core.DndContext, {
		sensors,
		onDragEnd: handleDragEnd,
		onDragStart: handleDragStart,
		modifiers: [_dnd_kit_modifiers.restrictToVerticalAxis, _dnd_kit_modifiers.restrictToParentElement]
	}, /* @__PURE__ */ react.default.createElement(_dnd_kit_sortable.SortableContext, {
		items: items.map((item) => `0-${item[rowKey]}`),
		strategy: _dnd_kit_sortable.verticalListSortingStrategy
	}, items.map((item) => /* @__PURE__ */ react.default.createElement(require_index.default, {
		key: `0-${item[rowKey]}`,
		id: `0-${item[rowKey]}`,
		item,
		depth: 0,
		sensors,
		childrenProps: props,
		lists: items,
		expandedKeys,
		setExpandedKeys,
		onChange: handleChange
	})))));
};
//#endregion
exports.default = DragSortTree;
