import { useEffect, useMemo, useCallback } from 'react';
import { useRecipes } from './useRecipes.js';

export function useRecipeNameLookup() {
  const { recipes, fetchRecipes } = useRecipes();

  useEffect(() => {
    void fetchRecipes();
  }, [fetchRecipes]);

  const recipeNames = useMemo(() => {
    const map = new Map<string, string>();
    for (const r of recipes) map.set(r.id, r.name);
    return map;
  }, [recipes]);

  const recipeName = useCallback(
    (id: string) => recipeNames.get(id) ?? null,
    [recipeNames],
  );

  return { recipeName };
}
