//#region src/views/Environment/handle-drag.ts
/** Create environment DnD handlers */
function environmentDragHandlerFactory(activeWorkspaceCollections, collectionMutator) {
	/** Drag handler that mutates the collection's environments order */
	const handleDragEnd = (draggingItem, hoveredItem) => {
		if (!draggingItem || !hoveredItem) return;
		const { id: draggingUid, parentId: draggingParentUid } = draggingItem;
		const { id: hoveredUid, parentId: hoveredParentUid, offset } = hoveredItem;
		if (draggingParentUid !== hoveredParentUid) return;
		const collection = activeWorkspaceCollections.value.find((c) => c.uid === draggingParentUid);
		if (!collection || !collection["x-scalar-environments"]) return;
		const environments = collection["x-scalar-environments"];
		const envKeys = Object.keys(environments);
		({ ...environments });
		const draggedIndex = envKeys.findIndex((key) => key === draggingUid);
		envKeys.splice(draggedIndex, 1);
		const targetIndex = envKeys.findIndex((key) => key === hoveredUid) + (offset === 1 ? 1 : 0);
		envKeys.splice(targetIndex, 0, draggingUid);
		collection["x-scalar-environments"] = envKeys.reduce((acc, key) => {
			const env = environments[key];
			if (env) acc[key] = env;
			return acc;
		}, {});
		collectionMutator.edit(collection.uid, "x-scalar-environments", collection["x-scalar-environments"]);
	};
	/** Ensure only environments within the same collection can be dropped */
	const isDroppable = (draggingItem, hoveredItem) => {
		return draggingItem.parentId === hoveredItem.parentId;
	};
	return {
		handleDragEnd,
		isDroppable
	};
}
//#endregion
export { environmentDragHandlerFactory };

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