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

type UserContextType = {
  user: User[];
  setUser: (list: User[]) => void;
};

const UserContext = createContext<UserContextType | null>(null);

type UserProviderProps = {
  children: ReactNode;
};

export const UserProvider = ({ children }: UserProviderProps) => {
  const [user, setUser] = useState<User[]>([]);
  
  const value = useMemo(() => ({ user, setUser }), [user]);
  
  return (
    <UserContext.Provider value={value}>
      {children}
    </UserContext.Provider>
  );
};

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