import { isSameYear } from "date-fns";
import React from "react";
import { createStrictContext } from "../../utils/helpers/index.js";
import { useControllableState } from "../../utils/hooks/index.js";
import { clampDisplayYear } from "../date-utils/index.js";
const { Provider: MonthPickerContextProvider, useContext: useMonthPickerContext, } = createStrictContext({
    name: "MonthPickerContext",
    errorMessage: "useMonthPickerContext must be used within an MonthPickerContextProvider",
});
const MonthPickerProvider = ({ children, disabled = [], year, onYearChange, fromDate, toDate, selected, defaultSelected, onMonthSelect, dropdownCaption, locale, }) => {
    var _a, _b;
    /**
     * Controlls current displayed year in MonthPicker
     */
    const [displayYear, setDisplayYear] = useControllableState({
        defaultValue: (_b = clampDisplayYear({
            month: (_a = year !== null && year !== void 0 ? year : selected) !== null && _a !== void 0 ? _a : defaultSelected,
            start: fromDate,
            end: toDate,
        })) !== null && _b !== void 0 ? _b : new Date(),
        value: year,
        onChange: onYearChange,
    });
    /**
     * Allows both controlled and uncontrolled use of MonthPicker
     */
    const [month, setMonth] = useControllableState({
        defaultValue: defaultSelected,
        value: selected,
        onChange: onMonthSelect,
    });
    const handleDisplayYearUpdate = (newYear) => {
        if (isSameYear(newYear, displayYear)) {
            return;
        }
        setDisplayYear(newYear);
    };
    return (React.createElement(MonthPickerContextProvider, { caption: dropdownCaption && fromDate && toDate ? "dropdown" : "label", disabled: disabled, selected: month, onMonthSelect: setMonth, year: displayYear, onYearChange: handleDisplayYearUpdate, locale: locale, fromDate: fromDate, toDate: toDate }, children));
};
export { MonthPickerProvider, useMonthPickerContext };
//# sourceMappingURL=MonthPicker.context.js.map