/**
 * Hook for managing node selection state
 */

import { useAtom, useSetAtom } from 'jotai';
import type { NodeId } from '../core/types';
import {
	selectedNodeIdsAtom,
	selectSingleNodeAtom,
	toggleNodeInSelectionAtom,
} from '../core/selection-store';

/**
 * Hook to manage selection for a specific node
 *
 * @param nodeId - The ID of the node
 * @returns Selection state and actions for the node
 */
export function useNodeSelection(nodeId: NodeId) {
	const [selectedIds] = useAtom(selectedNodeIdsAtom);
	const selectSingle = useSetAtom(selectSingleNodeAtom);
	const toggleNode = useSetAtom(toggleNodeInSelectionAtom);

	return {
		/** Whether this node is currently selected */
		isSelected: selectedIds.has(nodeId),
		/** Select only this node (clears other selections) */
		selectNode: () => selectSingle(nodeId),
		/** Toggle this node's selection state */
		toggleNode: () => toggleNode(nodeId),
	};
}
