import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import React, { useCallback, useMemo } from "react";
import { isFunction } from "@pisell/utils";
import { DndContext, PointerSensor, useSensor, useSensors } from "@dnd-kit/core";
import { SortableContext, arrayMove, useSortable } from "@dnd-kit/sortable";
import { CSS } from "@dnd-kit/utilities";
//#region src/components/DragSortList/index.tsx
function Sortable({ list = [], onChange, rowKey, children, activationConstraint }) {
	const sensors = useSensors(useSensor(PointerSensor, { activationConstraint }));
	const getRowKey = useCallback((row) => {
		if (isFunction(rowKey)) return rowKey(row);
		return row[rowKey];
	}, [rowKey]);
	const items = useMemo(() => {
		return list === null || list === void 0 ? void 0 : list.map((item) => getRowKey(item));
	}, [list, rowKey]);
	return /* @__PURE__ */ React.createElement(DndContext, {
		onDragEnd: ({ over, active }) => {
			if (active.id !== (over === null || over === void 0 ? void 0 : over.id)) {
				const activeIndex = list === null || list === void 0 ? void 0 : list.findIndex((i) => getRowKey(i) === active.id);
				const overIndex = list === null || list === void 0 ? void 0 : list.findIndex((i) => getRowKey(i) === (over === null || over === void 0 ? void 0 : over.id));
				onChange === null || onChange === void 0 || onChange(arrayMove(list, activeIndex, overIndex));
			}
		},
		sensors
	}, /* @__PURE__ */ React.createElement(SortableContext, { items }, list.map((item, index) => /* @__PURE__ */ React.createElement(SortableItem, {
		key: getRowKey(item),
		id: getRowKey(item),
		item,
		index,
		disabled: !!(item === null || item === void 0 ? void 0 : item.disabled)
	}, children))));
}
function SortableItem({ id, index, item, children, disabled }) {
	const { attributes, isSorting, listeners, setNodeRef, setActivatorNodeRef, transform, transition } = useSortable({
		id,
		disabled
	});
	return children({
		item,
		index,
		style: {
			transform: CSS.Transform.toString(transform && _objectSpread2(_objectSpread2({}, transform), {}, { scaleY: 1 })),
			transition
		},
		setActivatorNodeRef,
		listeners,
		attributes,
		props: { ref: setNodeRef }
	});
}
//#endregion
export { SortableItem, Sortable as default };
