import { FiltersMap } from '@wix/bex-core';
import {
  useWixPatternsContainer,
  useWixPatternsContainerOverrides,
} from '@wix/bex-core/react';
import { useCallback } from 'react';
import { TableGridSwitchFoldersState } from '../state/TableGridSwitchFoldersState';
import { CollectionFoldersBaseConfig } from './CollectionFoldersBaseConfig';
import { useOptionalCachedState } from './useOptionalCachedState';

export interface TableGridSwitchFoldersConfig<
  T1,
  F1 extends FiltersMap,
  T2,
  F2 extends FiltersMap,
> extends CollectionFoldersBaseConfig<T1, F1, T2, F2> {}

export type TableGridSwitchFoldersFactory = <
  T1,
  F1 extends FiltersMap,
  T2,
  F2 extends FiltersMap,
>(
  config: TableGridSwitchFoldersConfig<T1, F1, T2, F2>,
) => TableGridSwitchFoldersState<T1, F1, T2, F2>;

export function useCreateTableGridSwitchFolders() {
  const container = useWixPatternsContainer();
  const containerOverrides = useWixPatternsContainerOverrides();
  return useCallback<TableGridSwitchFoldersFactory>(({ items, folders }) => {
    return new TableGridSwitchFoldersState({
      items,
      folders,
      container,
      containerOverrides,
    });
  }, []);
}

export function useTableGridSwitchFolders<
  T1,
  F1 extends FiltersMap,
  T2,
  F2 extends FiltersMap,
>(config: TableGridSwitchFoldersConfig<T1, F1, T2, F2>) {
  const factory = useCreateTableGridSwitchFolders();

  const state = useOptionalCachedState(() => factory<T1, F1, T2, F2>(config));

  return state;
}
