import { Data } from '../../../types';
import { ItemSelectEventArgs, ListSelectMode } from '../../List/List.model';
import { ResultCounts } from './useDataProvider';

/**
 * Sets PageHeader subtitle
 * @param mode current ListSelectMode
 * @param itemSelection current item selection
 * @param results total results
 */
export function useSubtitle<T extends Data>(
  mode: ListSelectMode,
  itemSelection: ItemSelectEventArgs<T>,
  results?: ResultCounts,
): {
  readonly resultsTitle: string;
} {
  let resultsTitle = ''; // default to an empty string while results is being fetched

  if (results !== undefined) {
    switch (true) {
      case results.total === 1 && results.filtered === 1:
        resultsTitle = `Showing 1 Element`;
        break;
      case results.total === 1 && results.filtered === 0:
        resultsTitle = `Showing 0 of 1 Element`;
        break;
      case results.filtered === results.total:
        resultsTitle = `Showing all of ${results.total} Elements`;
        break;
      case results.filtered === undefined:
        resultsTitle = `Showing ${results.total} Elements`;
        break;
      default:
        resultsTitle = `Showing ${results.filtered} of ${results.total} Elements`;
    }

    // Append Selected items if list selection is in Multi mode
    if (mode === ListSelectMode.Multi) {
      if (itemSelection.mode === 'SINGLE_ITEMS') {
        resultsTitle = `${resultsTitle}, Selected: ${
          itemSelection.items?.length ?? 0
        }`;
      } else {
        // Show filtered results as selected results if 'SELECT_ALL' is active
        resultsTitle = `${resultsTitle}, Selected: ${results.filtered}`;
      }
    }
  }

  return { resultsTitle } as const;
}
