import { useQuery } from '@tanstack/react-query';
import { catalogApi } from '../apis';
import { catalogKeys } from '../keys';
import { CATALOG_REFERENCE_GC_TIME, CATALOG_REFERENCE_STALE_TIME } from '../constants';

/**
 * A category's applicable option groups (its own + every cross-category group), each with its
 * values. Reference data — cached per category for the whole session, so re-selecting a category in
 * the builder never refetches. Disabled until a category is chosen. An **empty list is valid** (a
 * category with no dimensions yet), not an error.
 */
export function useCategoryOptionGroups(categoryId: number | null | undefined) {
  return useQuery({
    queryKey: catalogKeys.categoryOptionGroups(categoryId),
    queryFn: () => catalogApi.getCategoryOptionGroups(categoryId as number),
    enabled: categoryId != null,
    staleTime: CATALOG_REFERENCE_STALE_TIME,
    gcTime: CATALOG_REFERENCE_GC_TIME,
  });
}
