import { useEffect } from 'react';

/**
 * Custom hook for handling element selection and deletion
 * @param {Object} params
 * @param {boolean} params.isSelected - Whether element is selected
 * @param {Function} params.onSelect - Callback when element is selected
 * @param {Function} params.onDelete - Callback when element is deleted
 * @param {string} params.toolMode - Current tool mode
 * @returns {Object} Selection handlers
 */
export function useElementSelection({ isSelected, onSelect, onDelete, toolMode }) {
  // Handle click for selection
  const handleClick = (e) => {
    // Only handle selection in select mode, let other modes bubble to canvas
    if (toolMode === 'select') {
      e.stopPropagation();
      onSelect(e);
    }
  };

  // Handle delete key press
  useEffect(() => {
    const handleKeyPress = (e) => {
      if (isSelected && e.key === 'Delete') {
        onDelete();
      }
    };

    document.addEventListener('keydown', handleKeyPress);
    return () => document.removeEventListener('keydown', handleKeyPress);
  }, [isSelected, onDelete]);

  return {
    handleClick
  };
}
