'use strict';

var React = require('react');
var getDates = require('./helpers/getDates.cjs');
var getDays = require('./helpers/getDays.cjs');
var getDisplayMonths = require('./helpers/getDisplayMonths.cjs');
var getInitialMonth = require('./helpers/getInitialMonth.cjs');
var getMonths = require('./helpers/getMonths.cjs');
var getNavMonth = require('./helpers/getNavMonth.cjs');
var getNextMonth = require('./helpers/getNextMonth.cjs');
var getPreviousMonth = require('./helpers/getPreviousMonth.cjs');
var getWeeks = require('./helpers/getWeeks.cjs');
var useControlledValue = require('./helpers/useControlledValue.cjs');

/** @private */
function useCalendar(props, dateLib) {
    const [navStart, navEnd] = getNavMonth.getNavMonths(props, dateLib);
    const { startOfMonth, endOfMonth } = dateLib;
    const initialMonth = getInitialMonth.getInitialMonth(props, dateLib);
    const [firstMonth, setFirstMonth] = useControlledValue.useControlledValue(initialMonth, 
    // initialMonth is always computed from props.month if provided
    props.month ? initialMonth : undefined);
    React.useEffect(() => {
        const newInitialMonth = getInitialMonth.getInitialMonth(props, dateLib);
        setFirstMonth(newInitialMonth);
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [props.timeZone]);
    /** The months displayed in the calendar. */
    const displayMonths = getDisplayMonths.getDisplayMonths(firstMonth, navEnd, props, dateLib);
    /** The dates displayed in the calendar. */
    const dates = getDates.getDates(displayMonths, props.endMonth ? endOfMonth(props.endMonth) : undefined, props, dateLib);
    /** The Months displayed in the calendar. */
    const months = getMonths.getMonths(displayMonths, dates, props, dateLib);
    /** The Weeks displayed in the calendar. */
    const weeks = getWeeks.getWeeks(months);
    /** The Days displayed in the calendar. */
    const days = getDays.getDays(months);
    const previousMonth = getPreviousMonth.getPreviousMonth(firstMonth, navStart, props, dateLib);
    const nextMonth = getNextMonth.getNextMonth(firstMonth, navEnd, props, dateLib);
    const { disableNavigation, onMonthChange } = props;
    const isDayInCalendar = (day) => weeks.some((week) => week.days.some((d) => d.isEqualTo(day)));
    const goToMonth = (date) => {
        if (disableNavigation) {
            return;
        }
        let newMonth = startOfMonth(date);
        // if month is before start, use the first month instead
        if (navStart && newMonth < startOfMonth(navStart)) {
            newMonth = startOfMonth(navStart);
        }
        // if month is after endMonth, use the last month instead
        if (navEnd && newMonth > startOfMonth(navEnd)) {
            newMonth = startOfMonth(navEnd);
        }
        setFirstMonth(newMonth);
        onMonthChange?.(newMonth);
    };
    const goToDay = (day) => {
        // is this check necessary?
        if (isDayInCalendar(day)) {
            return;
        }
        goToMonth(day.date);
    };
    const calendar = {
        months,
        weeks,
        days,
        navStart,
        navEnd,
        previousMonth,
        nextMonth,
        goToMonth,
        goToDay
    };
    return calendar;
}

exports.useCalendar = useCalendar;
//# sourceMappingURL=useCalendar.cjs.map
