import { FiltersMap, OptionalFiltersMap } from '@wix/bex-core';
import {
  useWixPatternsContainer,
  useWixPatternsContainerOverrides,
  CollectionConfigWithConditionals,
  useCreateCollection,
} from '@wix/bex-core/react';
import { TableGridSwitchState } from '../state/TableGridSwitchState';
import { useOptionalCachedState } from './useOptionalCachedState';

export { TableGridSwitchState } from '../state/TableGridSwitchState';

export type TableGridSwitchType = <
  T,
  F extends FiltersMap = OptionalFiltersMap,
>(
  config: CollectionConfigWithConditionals<T, F>,
) => TableGridSwitchState<T, F>;

export const useTableGridSwitchCollection: TableGridSwitchType = <
  T,
  F extends FiltersMap = OptionalFiltersMap,
>(
  config: CollectionConfigWithConditionals<T, F>,
): TableGridSwitchState<T, F> => {
  const container = useWixPatternsContainer();
  const containerOverrides = useWixPatternsContainerOverrides();
  const createCollection = useCreateCollection();

  const state = useOptionalCachedState(
    () =>
      new TableGridSwitchState<T, F>({
        collection: createCollection<T, F>(config),
        container,
        containerOverrides,
      }),
  );

  return state;
};
