import { FiltersMap } from '@wix/bex-core';
import { CollectionToolbarProps } from './CollectionToolbar';

export const useIsCollectionToolbarExists = <T, F extends FiltersMap>(
  props: CollectionToolbarProps<T, F>,
  {
    excludeSearch = false,
    isMobile,
  }: { excludeSearch?: boolean; isMobile?: boolean } = {},
): boolean => {
  const {
    title,
    tabs,
    views,
    customColumns,
    multiLevelSorting,
    search,
    filters,
    tableGridSwitchButton,
    exportButton,
    importButton,
    primaryActionButton,
    secondaryActions,
    moreActions,
    state,
  } = props;

  const hasExplicitFeatures = !!(
    (!isMobile && title) ||
    tabs ||
    views ||
    customColumns ||
    state.collectionFieldsSource ||
    multiLevelSorting ||
    exportButton ||
    importButton ||
    primaryActionButton ||
    secondaryActions ||
    moreActions ||
    tableGridSwitchButton ||
    filters
  );

  if (hasExplicitFeatures) {
    return true;
  }

  if (!excludeSearch && search !== false) {
    return true;
  }

  const hasDynamicFilters =
    (state.collectionFieldsSource?.buildFilters() || []).length > 0 ||
    !!state.tagsCollectionState?.buildTagsFilterWrapper();

  return hasDynamicFilters;
};
