import { useCategories } from "@/api/categories";
import { CategoryResponse } from "@/api/types";
import { ReactNode, useMemo } from "react";
import TaxonomyContext from "./context";

interface TaxonomyProviderProps {
  children: ReactNode;
}

const TaxonomyProvider = ({children}: TaxonomyProviderProps) => {
  const { data: categories } = useCategories();

  const cats = useMemo<CategoryResponse[]>(() => {
    if(categories)
      return categories;

    return [];
  }, [categories])

  return (
    <TaxonomyContext.Provider value={{
      categories: cats
    }}>
      {children}
    </TaxonomyContext.Provider>
  )
}

export default TaxonomyProvider;