"use client";

import * as React from "react";

// 1. Create the context
type DateContextType = {
  date: Date | undefined;
  setDate: (date: Date | undefined) => void;
};

const DateContext = React.createContext<DateContextType | undefined>(undefined);

// 2. Create a custom hook to use the context
export function useDateContext() {
  const context = React.useContext(DateContext);
  if (context === undefined) {
    throw new Error("useDateContext must be used within a DateProvider");
  }
  return context;
}

// 3. Create the provider component
export function DateProvider({ children }: { children: React.ReactNode }) {
  const [date, setDate] = React.useState<Date | undefined>(new Date());

  const value = React.useMemo(() => {
    return { date, setDate };
  }, [date]);

  return <DateContext.Provider value={value}>{children}</DateContext.Provider>;
}
