import React, { createContext, useContext, useMemo, useState } from "react";

interface DateRangePickerContextProps {
  isCustomRange: boolean;
  setIsCustomRange: React.Dispatch<React.SetStateAction<boolean>> | undefined;
}

interface DateRangePickerProviderProps {
  children: React.ReactNode;
}

const DateRangePickerContext = createContext<DateRangePickerContextProps>({
  isCustomRange: false,
  setIsCustomRange: undefined,
});

export const useDateRangePicker = () => {
  return useContext(DateRangePickerContext);
};

export const DateRangePickerProvider: React.FC<
  DateRangePickerProviderProps
> = ({ children }) => {
  const [isCustomRange, setIsCustomRange] = useState(false);

  const dateRangePickerContextValue = useMemo(
    () => ({ isCustomRange, setIsCustomRange }),
    [isCustomRange, setIsCustomRange]
  );

  return (
    <DateRangePickerContext.Provider value={dateRangePickerContextValue}>
      {children}
    </DateRangePickerContext.Provider>
  );
};
