const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _dnd_kit_core = require("@dnd-kit/core");
let _dnd_kit_sortable = require("@dnd-kit/sortable");
let _dnd_kit_utilities = require("@dnd-kit/utilities");
//#region src/components/DragSortList/index.tsx
function Sortable({ list = [], onChange, rowKey, children, activationConstraint }) {
	const sensors = (0, _dnd_kit_core.useSensors)((0, _dnd_kit_core.useSensor)(_dnd_kit_core.PointerSensor, { activationConstraint }));
	const getRowKey = (0, react.useCallback)((row) => {
		if ((0, _pisell_utils.isFunction)(rowKey)) return rowKey(row);
		return row[rowKey];
	}, [rowKey]);
	const items = (0, react.useMemo)(() => {
		return list === null || list === void 0 ? void 0 : list.map((item) => getRowKey(item));
	}, [list, rowKey]);
	return /* @__PURE__ */ react.default.createElement(_dnd_kit_core.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((0, _dnd_kit_sortable.arrayMove)(list, activeIndex, overIndex));
			}
		},
		sensors
	}, /* @__PURE__ */ react.default.createElement(_dnd_kit_sortable.SortableContext, { items }, list.map((item, index) => /* @__PURE__ */ react.default.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 } = (0, _dnd_kit_sortable.useSortable)({
		id,
		disabled
	});
	return children({
		item,
		index,
		style: {
			transform: _dnd_kit_utilities.CSS.Transform.toString(transform && require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, transform), {}, { scaleY: 1 })),
			transition
		},
		setActivatorNodeRef,
		listeners,
		attributes,
		props: { ref: setNodeRef }
	});
}
//#endregion
exports.SortableItem = SortableItem;
exports.default = Sortable;
