import type { DefaultViewMonthProps } from '../calendar.types'

/**
 * Calculates the default view date, either using selected date, current date or boundaries
 * Used when initializing, after that viewDate will be set
 * Memoized so will only recalculate when boundaries or selectedDate changes
 */
export const useDefaultViewMonth = ({ boundaries, selectedDate, isStartDate }: DefaultViewMonthProps) => {
  if (selectedDate) return selectedDate

  const currentDate = new Date()
  if (!boundaries) return currentDate

  if (boundaries.endDate && currentDate > boundaries.endDate && !isStartDate) return boundaries.endDate
  if (boundaries.startDate && currentDate > boundaries.startDate && isStartDate) return boundaries.startDate

  return currentDate
}
