/**
 * @ai-summary Tests for useBulkSelection hook
 */

import { renderHook, act } from '@testing-library/react';
import { useBulkSelection } from '../../hooks/useBulkSelection';

describe('useBulkSelection', () => {
  const mockItems = [
    { id: '1', name: 'Item 1' },
    { id: '2', name: 'Item 2' },
    { id: '3', name: 'Item 3' },
  ];

  it('should initialize with empty selection', () => {
    const { result } = renderHook(() =>
      useBulkSelection({ items: mockItems })
    );

    expect(result.current.count).toBe(0);
    expect(result.current.selected.size).toBe(0);
  });

  it('should toggle individual item selection', () => {
    const { result } = renderHook(() =>
      useBulkSelection({ items: mockItems })
    );

    act(() => {
      result.current.toggleItem('1');
    });

    expect(result.current.count).toBe(1);
    expect(result.current.isSelected('1')).toBe(true);

    act(() => {
      result.current.toggleItem('1');
    });

    expect(result.current.count).toBe(0);
    expect(result.current.isSelected('1')).toBe(false);
  });

  it('should toggle all items', () => {
    const { result } = renderHook(() =>
      useBulkSelection({ items: mockItems })
    );

    act(() => {
      result.current.toggleAll(mockItems);
    });

    expect(result.current.count).toBe(3);
    expect(result.current.isSelected('1')).toBe(true);
    expect(result.current.isSelected('2')).toBe(true);
    expect(result.current.isSelected('3')).toBe(true);

    act(() => {
      result.current.toggleAll(mockItems);
    });

    expect(result.current.count).toBe(0);
  });

  it('should reset all selections', () => {
    const { result } = renderHook(() =>
      useBulkSelection({ items: mockItems })
    );

    act(() => {
      result.current.toggleItem('1');
      result.current.toggleItem('2');
    });

    expect(result.current.count).toBe(2);

    act(() => {
      result.current.reset();
    });

    expect(result.current.count).toBe(0);
  });

  it('should return selected items', () => {
    const { result } = renderHook(() =>
      useBulkSelection({ items: mockItems })
    );

    act(() => {
      result.current.toggleItem('1');
      result.current.toggleItem('3');
    });

    expect(result.current.selectedItems).toHaveLength(2);
    expect(result.current.selectedItems[0].id).toBe('1');
    expect(result.current.selectedItems[1].id).toBe('3');
  });

  // Note: Custom keyExtractor with non-id items is not supported by current types
  // The hook requires T extends { id: string } constraint
});
