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

type ReservationContextType = {
  reservations: Reservation[];
  setReservations: (list: Reservation[]) => void;
};

const ReservationsContext = createContext<ReservationContextType | null>(null);

type ReservationsProviderProps = {
  children: ReactNode;
};

export const ReservationsProvider = ({ children }: ReservationsProviderProps) => {
  const [reservations, setReservations] = useState<Reservation[]>([]);
  
  const value = useMemo(() => ({ reservations, setReservations }), [reservations]);
  
  return (
    <ReservationsContext.Provider value={value}>
      {children}
    </ReservationsContext.Provider>
  );
};

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