const require_runtime = require("../../../../../_virtual/_rolldown/runtime.js");
const require_useSharedState = require("../../../../../hooks/useSharedState.js");
const require_asyncToGenerator = require("../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_model = require("../../../model.js");
const require_utils = require("../../utils.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_modifiers = require("@dnd-kit/modifiers");
require("./index.less");
//#region src/components/table/Table/tableConfig/SortRow/index.tsx
const SortRow = ({ children, rowKey, onSortChange }) => {
	const { state, dispatch } = require_useSharedState.default(require_model.Context);
	const { originDataSource } = state;
	const oldOriginDataSource = (0, react.useRef)(null);
	const onDragEnd = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* ({ active, over }) {
			if (active.id !== (over === null || over === void 0 ? void 0 : over.id)) {
				const activeIndex = originDataSource.findIndex((i) => require_utils.getIdByRowKey(i, rowKey) === active.id);
				const overIndex = originDataSource.findIndex((i) => require_utils.getIdByRowKey(i, rowKey) === (over === null || over === void 0 ? void 0 : over.id));
				const newData = (0, _dnd_kit_sortable.arrayMove)(originDataSource, activeIndex, overIndex);
				dispatch({
					type: "setOriginDataSource",
					payload: newData
				});
				if ((0, _pisell_utils.isFunction)(onSortChange)) {
					oldOriginDataSource.current = originDataSource;
					if (yield onSortChange({
						fromIndex: activeIndex,
						toIndex: overIndex,
						dataSource: newData,
						fromId: active.id,
						toId: over === null || over === void 0 ? void 0 : over.id,
						fromItem: originDataSource[activeIndex],
						toItem: originDataSource[overIndex]
					})) dispatch({
						type: "setOriginDataSource",
						payload: oldOriginDataSource.current
					});
					else oldOriginDataSource.current = null;
				}
			}
		});
		return function onDragEnd(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	return /* @__PURE__ */ react.default.createElement("div", { className: "grid-view-table-wrap" }, /* @__PURE__ */ react.default.createElement(_dnd_kit_core.DndContext, {
		modifiers: [_dnd_kit_modifiers.restrictToVerticalAxis],
		onDragEnd
	}, /* @__PURE__ */ react.default.createElement(_dnd_kit_sortable.SortableContext, {
		items: originDataSource.map((i) => require_utils.getIdByRowKey(i, rowKey)),
		strategy: _dnd_kit_sortable.verticalListSortingStrategy
	}, children)));
};
//#endregion
exports.default = SortRow;
