require("../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
let _dnd_kit_sortable = require("@dnd-kit/sortable");
//#region src/components/pisellDraggable/utilities.ts
const iOS = /iPad|iPhone|iPod/.test(navigator.platform);
function getDragDepth(offset, indentationWidth) {
	return Math.round(offset / indentationWidth);
}
function getProjection(items, activeId, overId, dragOffset, indentationWidth) {
	const overItemIndex = items.findIndex(({ id }) => id === overId);
	const activeItemIndex = items.findIndex(({ id }) => id === activeId);
	const activeItem = items[activeItemIndex];
	const newItems = (0, _dnd_kit_sortable.arrayMove)(items, activeItemIndex, overItemIndex);
	const previousItem = newItems[overItemIndex - 1];
	const nextItem = newItems[overItemIndex + 1];
	const dragDepth = getDragDepth(dragOffset, indentationWidth);
	const projectedDepth = activeItem.depth + dragDepth;
	const maxDepth = getMaxDepth({ previousItem });
	const minDepth = getMinDepth({ nextItem });
	let depth = projectedDepth;
	if (projectedDepth >= maxDepth) depth = maxDepth;
	else if (projectedDepth < minDepth) depth = minDepth;
	return {
		depth,
		maxDepth,
		minDepth,
		parentId: getParentId()
	};
	function getParentId() {
		var _newItems$slice$rever;
		if (depth === 0 || !previousItem) return null;
		if (depth === previousItem.depth) return previousItem.parentId;
		if (depth > previousItem.depth) return previousItem.id;
		const newParent = (_newItems$slice$rever = newItems.slice(0, overItemIndex).reverse().find((item) => item.depth === depth)) === null || _newItems$slice$rever === void 0 ? void 0 : _newItems$slice$rever.parentId;
		return newParent !== null && newParent !== void 0 ? newParent : null;
	}
}
function getMaxDepth({ previousItem }) {
	if (previousItem) return previousItem.depth + 1;
	return 0;
}
function getMinDepth({ nextItem }) {
	if (nextItem) return nextItem.depth;
	return 0;
}
function flatten(items, parentId = null, depth = 0) {
	return items.reduce((acc, item, index) => {
		var _item$children;
		return [
			...acc,
			require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, {
				parentId,
				depth,
				index
			}),
			...flatten((_item$children = item === null || item === void 0 ? void 0 : item.children) !== null && _item$children !== void 0 ? _item$children : [], item.id, depth + 1)
		];
	}, []);
}
function flattenTree(items) {
	return flatten(items);
}
function buildTree(flattenedItems) {
	const root = {
		id: "root",
		children: []
	};
	const nodes = { [root.id]: root };
	const items = flattenedItems.map((item) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, { children: [] }));
	for (const item of items) {
		var _item$parentId, _nodes$parentId;
		const { id, children } = item;
		const parentId = (_item$parentId = item.parentId) !== null && _item$parentId !== void 0 ? _item$parentId : root.id;
		const parent = (_nodes$parentId = nodes[parentId]) !== null && _nodes$parentId !== void 0 ? _nodes$parentId : findItem(items, parentId);
		nodes[id] = {
			id,
			children
		};
		parent.children.push(item);
	}
	return root.children;
}
function findItem(items, itemId) {
	return items.find(({ id }) => id === itemId);
}
function findItemDeep(items, itemId) {
	for (const item of items) {
		const { id, children } = item;
		if (id === itemId) return item;
		if (children === null || children === void 0 ? void 0 : children.length) {
			const child = findItemDeep(children, itemId);
			if (child) return child;
		}
	}
}
function removeItem(items, id) {
	const newItems = [];
	for (const item of items) {
		var _item$children2;
		if (item.id === id) continue;
		if ((_item$children2 = item.children) === null || _item$children2 === void 0 ? void 0 : _item$children2.length) item.children = removeItem(item.children, id);
		newItems.push(item);
	}
	return newItems;
}
function setProperty(items, id, property, setter) {
	for (const item of items) {
		var _item$children3;
		if (item.id === id) {
			item[property] = setter(item[property]);
			continue;
		}
		if ((_item$children3 = item.children) === null || _item$children3 === void 0 ? void 0 : _item$children3.length) item.children = setProperty(item.children, id, property, setter);
	}
	return [...items];
}
function countChildren(items, count = 0) {
	return items.reduce((acc, { children }) => {
		if (children === null || children === void 0 ? void 0 : children.length) return countChildren(children, acc + 1);
		return acc + 1;
	}, count);
}
function getChildCount(items, id) {
	const item = findItemDeep(items, id);
	return item ? countChildren(item.children) : 0;
}
function removeChildrenOf(items, ids) {
	const excludeParentIds = [...ids];
	return items.filter((item) => {
		if (item.parentId && excludeParentIds.includes(item.parentId)) {
			var _item$children4;
			if ((_item$children4 = item.children) === null || _item$children4 === void 0 ? void 0 : _item$children4.length) excludeParentIds.push(item.id);
			return false;
		}
		return true;
	});
}
//#endregion
exports.buildTree = buildTree;
exports.flattenTree = flattenTree;
exports.getChildCount = getChildCount;
exports.getProjection = getProjection;
exports.iOS = iOS;
exports.removeChildrenOf = removeChildrenOf;
exports.removeItem = removeItem;
exports.setProperty = setProperty;
