import type { Module } from '../../types';
import type { RootState } from '..';

// Stable identity so `selectCategoryModules` returns the same reference for a
// category that has not loaded yet; a fresh `[]` would break memoisation and
// re-render every consumer.
const EMPTY_MODULES: Module[] = [];

export const selectModulesList = (state: RootState) => state.modules.list;
export const selectModulesTotal = (state: RootState) => state.modules.total;
export const selectModulesLoading = (state: RootState) => state.modules.loading;
export const selectCurrentModule = (state: RootState) => state.modules.current;
export const selectCategoryModules = (category: string) => (state: RootState) =>
  state.modules.categories[category] ?? EMPTY_MODULES;
