import { useLayout } from "../../hooks/useLayout.js";
//#region src/views/Request/handle-drag.ts
/** Create DnD handlers */
function dragHandlerFactory(activeWorkspace, { collections, collectionMutators, tags, tagMutators, workspaceMutators }) {
	const { layout } = useLayout();
	/** Mutate tag OR collection */
	const mutateTagOrCollection = (parent, childUids) => {
		if (parent.type === "collection") collectionMutators.edit(parent.uid, "children", childUids);
		else if (parent.type === "tag") tagMutators.edit(parent.uid, "children", childUids);
	};
	/** Drag handler that mutates depending on the entity types */
	const handleDragEnd = (draggingItem, hoveredItem) => {
		if (!draggingItem || !hoveredItem) return;
		const { id: draggingUid, parentId: draggingParentUid } = draggingItem;
		const { id: hoveredUid, parentId: hoveredParentUid, offset } = hoveredItem;
		if (!draggingParentUid) workspaceMutators.edit(activeWorkspace.value?.uid, "collections", activeWorkspace.value?.collections.filter((uid) => uid !== draggingUid) ?? []);
		else if (collections[draggingParentUid]) collectionMutators.edit(draggingParentUid, "children", collections[draggingParentUid].children.filter((uid) => uid !== draggingUid));
		else if (tags[draggingParentUid]) tagMutators.edit(draggingParentUid, "children", tags[draggingParentUid].children.filter((uid) => uid !== draggingUid));
		if (offset === 2) {
			const parent = collections[hoveredUid] || tags[hoveredUid];
			if (parent) mutateTagOrCollection(parent, [...parent.children ?? [], draggingUid]);
		} else if (!hoveredParentUid) {
			const newChildUids = [...activeWorkspace.value?.collections ?? []];
			const hoveredIndex = newChildUids.findIndex((uid) => hoveredUid === uid) ?? 0;
			newChildUids.splice(hoveredIndex + offset, 0, draggingUid);
			workspaceMutators.edit(activeWorkspace.value?.uid, "collections", newChildUids);
		} else {
			const parent = collections[hoveredParentUid] || tags[hoveredParentUid];
			if (!parent) return;
			const newChildUids = [...parent.children ?? []];
			const hoveredIndex = newChildUids.findIndex((uid) => hoveredUid === uid) ?? 0;
			newChildUids.splice(hoveredIndex + offset, 0, draggingUid);
			mutateTagOrCollection(parent, newChildUids);
		}
	};
	/** Ensure only collections are allowed at the top level OR resources dropped INTO (offset 2) */
	const isDroppable = (draggingItem, hoveredItem) => {
		if (layout === "modal") return false;
		if (!collections[draggingItem.id] && hoveredItem.offset !== 2) return false;
		if (collections[draggingItem.id] && collections[hoveredItem.id]?.info?.title === "Drafts") return false;
		return true;
	};
	return {
		handleDragEnd,
		isDroppable
	};
}
//#endregion
export { dragHandlerFactory };

//# sourceMappingURL=handle-drag.js.map