const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_index = require("../../locales/index.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_index$1 = require("../DragSortList/index.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");
require("./index.less");
const DefaultRenderContainer = (props) => {
	const { id, children, renderContainer } = props;
	const { setNodeRef } = (0, _dnd_kit_sortable.useSortable)({ id });
	if ((0, _pisell_utils.isFunction)(renderContainer)) return renderContainer({
		children,
		ref: setNodeRef,
		id
	});
	return /* @__PURE__ */ react.default.createElement("div", { ref: setNodeRef }, children);
};
const MultipleContainersSortList = (props) => {
	const { wrapStyle, list, children, onChange, renderContainer, rowKey = "id", quickFilterMaxLength } = props;
	const [containers, setContainers] = (0, react.useState)([]);
	const getRowKey = (0, react.useCallback)((row) => {
		if ((0, _pisell_utils.isFunction)(rowKey)) return rowKey(row);
		return row[rowKey];
	}, [rowKey]);
	(0, react.useEffect)(() => {
		setContainers(Object.keys(list));
	}, [list]);
	const findContainer = (id) => {
		if (id in list) return id;
		return Object.keys(list).find((key) => list[key].map((item) => getRowKey(item)).includes(id));
	};
	const handleDragOver = ({ active, over }) => {
		const overId = over === null || over === void 0 ? void 0 : over.id;
		if (overId == null || overId === "void" || active.id in list) return;
		const overContainer = findContainer(overId);
		const activeContainer = findContainer(active.id);
		if (!overContainer || !activeContainer) return;
		if (activeContainer !== overContainer) {
			const activeItems = list[activeContainer];
			const overItems = list[overContainer];
			const overIndex = overItems.findIndex((item) => getRowKey(item) === overId);
			const activeIndex = activeItems.findIndex((item) => getRowKey(item) === active.id);
			let newIndex;
			if (overId in list) newIndex = overItems.length + 1;
			else {
				const modifier = over && active.rect.current.translated && active.rect.current.translated.top > over.rect.top + over.rect.height ? 1 : 0;
				newIndex = overIndex >= 0 ? overIndex + modifier : overItems.length + 1;
			}
			let newList = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, list), {}, {
				[activeContainer]: list[activeContainer].filter((item) => getRowKey(item) !== active.id),
				[overContainer]: [
					...list[overContainer].slice(0, newIndex),
					list[activeContainer][activeIndex],
					...list[overContainer].slice(newIndex, list[overContainer].length)
				]
			});
			if (overContainer === "quickFilter" && newList[overContainer].length > quickFilterMaxLength) newList = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, newList), {}, {
				[overContainer]: newList[overContainer].slice(0, quickFilterMaxLength),
				[activeContainer]: [...newList[overContainer].slice(quickFilterMaxLength), ...newList[activeContainer]]
			});
			onChange === null || onChange === void 0 || onChange(newList);
		}
	};
	const handleDragEnd = ({ active, over }) => {
		const overId = over === null || over === void 0 ? void 0 : over.id;
		const overContainer = findContainer(overId);
		const activeContainer = findContainer(active.id);
		if (!activeContainer) return;
		if (overContainer) {
			const activeIndex = list[activeContainer].findIndex((item) => getRowKey(item) === active.id);
			const overIndex = list[overContainer].findIndex((item) => getRowKey(item) === overId);
			if (activeIndex !== overIndex) {
				const newList = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, list), {}, { [overContainer]: (0, _dnd_kit_sortable.arrayMove)(list[overContainer], activeIndex, overIndex) });
				onChange === null || onChange === void 0 || onChange(newList);
			}
		}
	};
	return /* @__PURE__ */ react.default.createElement(_dnd_kit_core.DndContext, {
		onDragOver: handleDragOver,
		onDragEnd: handleDragEnd
	}, /* @__PURE__ */ react.default.createElement("div", { style: wrapStyle }, containers.map((container) => /* @__PURE__ */ react.default.createElement(DefaultRenderContainer, {
		id: container,
		key: container,
		renderContainer
	}, /* @__PURE__ */ react.default.createElement(_dnd_kit_sortable.SortableContext, {
		key: container,
		items: list[container].map((item) => getRowKey(item)),
		strategy: _dnd_kit_sortable.verticalListSortingStrategy
	}, list[container].length ? list[container].map((item, index) => !(item === null || item === void 0 ? void 0 : item.isHidden) ? /* @__PURE__ */ react.default.createElement(require_index$1.SortableItem, {
		key: getRowKey(item),
		id: getRowKey(item),
		item,
		index
	}, children) : null) : /* @__PURE__ */ react.default.createElement("span", { className: "materials-sort-list-empty" }, require_index.getText("table-action-filter-no-filter")))))));
};
//#endregion
exports.default = MultipleContainersSortList;
