import { Ressources } from "@/class/class";
import { createContext, ReactNode, useContext, useMemo, useState } from "react";

type RessourcesContextType = {
  ressources: Ressources[];
  setRessources: (list: Ressources[]) => void;
};

const RessourcesContext = createContext<RessourcesContextType | null>(null);

type RessourcesProviderProps = {
  children: ReactNode;
};

export const RessourcesProvider = ({ children }: RessourcesProviderProps) => {
  const [ressources, setRessources] = useState<Ressources[]>([]);
  
  const value = useMemo(() => ({ ressources, setRessources }), [ressources]);
  
  return (
    <RessourcesContext.Provider value={value}>
      {children}
    </RessourcesContext.Provider>
  );
};

export const useRessources = () => {
  const context = useContext(RessourcesContext);
  if (!context) {
    throw new Error("useRessources doit être utilisé dans <RessourcesProvider>");
  }
  return context;
};