import { useState } from 'react';
import { addDays, addMonths, addYears } from 'date-fns';

export function useCalendar() {
  const [currentDate, setCurrentDate] = useState(new Date());
  const [viewType, setViewType] = useState('week');

  const navigateDate = (direction) => {
    const newDate = new Date(currentDate);
    const operations = {
      week: (date) =>
        direction === 'prev' ? addDays(date, -7) : addDays(date, 7),
      month: (date) =>
        direction === 'prev' ? addMonths(date, -1) : addMonths(date, 1),
      year: (date) =>
        direction === 'prev' ? addYears(date, -1) : addYears(date, 1),
    };

    setCurrentDate(operations[viewType](newDate));
  };

  return {
    currentDate,
    setCurrentDate,
    viewType,
    setViewType,
    navigateDate,
  };
}
