import { FiltersMap } from '@wix/bex-core';
import { CollectionViewsDropdownProps } from '../components/CollectionViewsDropdown';
import { Category, PREDEFINED_VIEWS_ID, View } from '../model';

function isCategoryPresets<F extends FiltersMap>(
  presets: CollectionViewsDropdownProps<F>['presets'],
): presets is Category<F>[] {
  return !!presets?.length && 'views' in presets[0];
}

export function presetsToCategories<F extends FiltersMap>(
  presets: CollectionViewsDropdownProps<F>['presets'],
): Category<F>[] {
  return presets == null
    ? []
    : isCategoryPresets(presets)
    ? (presets as Category<F>[])
    : ([
        {
          id: PREDEFINED_VIEWS_ID,
          name: '',
          views: presets as View<F>[],
        },
      ] as Category<F>[]);
}
