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,
    primaryActionButton,
    secondaryActions,
    moreActions,
    state,
  } = props;

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

  if (hasExplicitFeatures) {
    return true;
  }

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

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

  return hasDynamicFilters;
};
