import { renderHook } from '@testing-library/react-hooks';
import { describe, expect, it } from 'vitest';
import { ItemSelectEventArgs, ListSelectMode } from '../../List/List.model';
import { ResultCounts } from './useDataProvider';
import { useSubtitle } from './useSubtitle';

describe('useSubtitle', () => {
  it('should return "Showing 1 Element" when there is 1 total and 1 filtered result', () => {
    const mode = ListSelectMode.Single;
    const itemSelection: ItemSelectEventArgs<never> = {
      mode: 'SINGLE_ITEMS',
      items: [],
    };
    const results: ResultCounts = { total: 1, filtered: 1 };

    const { result } = renderHook(() =>
      useSubtitle(mode, itemSelection, results),
    );

    expect(result.current.resultsTitle).toBe('Showing 1 Element');
  });

  it('should return "Showing 0 of 1 Element" when there is 1 total and 0 filtered result', () => {
    const mode = ListSelectMode.Single;
    const itemSelection: ItemSelectEventArgs<never> = {
      mode: 'SINGLE_ITEMS',
      items: [],
    };
    const results: ResultCounts = { total: 1, filtered: 0 };

    const { result } = renderHook(() =>
      useSubtitle(mode, itemSelection, results),
    );

    expect(result.current.resultsTitle).toBe('Showing 0 of 1 Element');
  });

  it('should return "Showing all of X Elements" when filtered results equal total results', () => {
    const mode = ListSelectMode.Single;
    const itemSelection: ItemSelectEventArgs<never> = {
      mode: 'SINGLE_ITEMS',
      items: [],
    };
    const results: ResultCounts = { total: 5, filtered: 5 };

    const { result } = renderHook(() =>
      useSubtitle(mode, itemSelection, results),
    );

    expect(result.current.resultsTitle).toBe('Showing all of 5 Elements');
  });

  it('should return "Showing X Elements" when filtered results are undefined', () => {
    const mode = ListSelectMode.Single;
    const itemSelection: ItemSelectEventArgs<never> = {
      mode: 'SINGLE_ITEMS',
      items: [],
    };
    const results: ResultCounts = { total: 5, filtered: undefined };

    const { result } = renderHook(() =>
      useSubtitle(mode, itemSelection, results),
    );

    expect(result.current.resultsTitle).toBe('Showing 5 Elements');
  });

  it('should return "Showing X of Y Elements" when filtered results are less than total results', () => {
    const mode = ListSelectMode.Single;
    const itemSelection: ItemSelectEventArgs<never> = {
      mode: 'SINGLE_ITEMS',
      items: [],
    };
    const results: ResultCounts = { total: 10, filtered: 5 };

    const { result } = renderHook(() =>
      useSubtitle(mode, itemSelection, results),
    );

    expect(result.current.resultsTitle).toBe('Showing 5 of 10 Elements');
  });

  it('should append selected items count when mode is Multi and selection mode is SINGLE_ITEMS', () => {
    const mode = ListSelectMode.Multi;
    const itemSelection: ItemSelectEventArgs<{ id: number }> = {
      mode: 'SINGLE_ITEMS',
      items: [{ id: 1 }, { id: 2 }],
    };
    const results: ResultCounts = { total: 10, filtered: 5 };

    const { result } = renderHook(() =>
      useSubtitle(mode, itemSelection, results),
    );

    expect(result.current.resultsTitle).toBe(
      'Showing 5 of 10 Elements, Selected: 2',
    );
  });

  it('should append filtered results count as selected items when mode is Multi and selection mode is SELECT_ALL', () => {
    const mode = ListSelectMode.Multi;
    const itemSelection: ItemSelectEventArgs<never> = {
      mode: 'SELECT_ALL',
      items: [],
    };
    const results: ResultCounts = { total: 10, filtered: 5 };

    const { result } = renderHook(() =>
      useSubtitle(mode, itemSelection, results),
    );

    expect(result.current.resultsTitle).toBe(
      'Showing 5 of 10 Elements, Selected: 5',
    );
  });
});
