/**
 * Canvas Selection Subscription Hook
 *
 * Provides reactive access to canvas selection state.
 * Use this to respond to selection changes in your components.
 *
 * @since 0.2.0
 * @example
 * ```tsx
 * function SelectionInfo() {
 *   const { selectedNodeIds, selectedEdgeId, hasSelection } = useCanvasSelection();
 *   return <div>Selected: {selectedNodeIds.size} nodes</div>;
 * }
 * ```
 */

import { useAtomValue } from 'jotai';
import { selectedNodeIdsAtom, selectedEdgeIdAtom, selectedNodesCountAtom, hasSelectionAtom } from '../core/selection-store';
import type { NodeId } from '../core/types';

export interface CanvasSelectionState {
	/** Set of currently selected node IDs (reactive) */
	selectedNodeIds: Set<NodeId>;
	/** Currently selected edge ID (null if no edge selected) */
	selectedEdgeId: string | null;
	/** Number of selected nodes (derived atom for performance) */
	count: number;
	/** Whether any nodes are selected */
	hasSelection: boolean;
	/** Whether an edge is selected */
	hasEdgeSelection: boolean;
}

/**
 * Subscribe to canvas selection state.
 *
 * This hook provides a convenient way to access selection state
 * without needing to know which atoms to import. All returned values
 * are reactive and will trigger re-renders when selection changes.
 */
export function useCanvasSelection(): CanvasSelectionState {
	const selectedNodeIds = useAtomValue(selectedNodeIdsAtom);
	const selectedEdgeId = useAtomValue(selectedEdgeIdAtom);
	const count = useAtomValue(selectedNodesCountAtom);
	const hasSelection = useAtomValue(hasSelectionAtom);

	return {
		selectedNodeIds,
		selectedEdgeId,
		count,
		hasSelection,
		hasEdgeSelection: selectedEdgeId !== null,
	};
}
