import { useState, useCallback } from 'react';

export interface UseRowSelectionResult {
  selectedRowKeys: React.Key[];
  setSelectedRowKeys: React.Dispatch<React.SetStateAction<React.Key[]>>;
  rowSelection: {
    selectedRowKeys: React.Key[];
    onChange: (keys: React.Key[]) => void;
  };
  hasSelected: boolean;
  clearSelection: () => void;
}

/**
 * Hook for managing table row selection state
 */
export function useRowSelection(): UseRowSelectionResult {
  const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);

  const onSelectChange = useCallback((keys: React.Key[]) => {
    setSelectedRowKeys(keys);
  }, []);

  const clearSelection = useCallback(() => {
    setSelectedRowKeys([]);
  }, []);

  return {
    selectedRowKeys,
    setSelectedRowKeys,
    rowSelection: {
      selectedRowKeys,
      onChange: onSelectChange,
    },
    hasSelected: selectedRowKeys.length > 0,
    clearSelection,
  };
}
