import { CollectionViewsState } from './CollectionViewsState';
import { presetsToCategories } from './presetsToCategories';

import { FiltersMap } from '@wix/bex-core';
import { ToolbarCollectionState } from './ToolbarCollectionState';
import { ViewsProps } from '../index';

export function setViewsState<T, F extends FiltersMap>(
  toolbar: ToolbarCollectionState<T, F>,
  viewsProps: ViewsProps<F>,
) {
  const {
    presets,
    onSaveView,
    onSaveViewChanges,
    onDeleteView,
    onRenameView,
    onSetAsDefaultView,
    allItemsViewProps,
    learnMoreLink,
    viewNamePlaceholder,
  } = viewsProps;

  const { container } = toolbar;

  if (container.isPanelLayout) {
    return null;
  }

  return (toolbar.viewsState ??= new CollectionViewsState({
    table: toolbar,
    container,
    presets: presetsToCategories(presets),
    allItemsViewProps,
    onDeleteView,
    onSaveView,
    onSaveViewChanges,
    onSetAsDefaultView,
    onRenameView,
    learnMoreLink,
    viewNamePlaceholder,
  }));
}
