"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useMonthPickerContext = exports.MonthPickerProvider = void 0;
const date_fns_1 = require("date-fns");
const react_1 = __importDefault(require("react"));
const helpers_1 = require("../../utils/helpers");
const hooks_1 = require("../../utils/hooks");
const date_utils_1 = require("../date-utils");
const { Provider: MonthPickerContextProvider, useContext: useMonthPickerContext, } = (0, helpers_1.createStrictContext)({
    name: "MonthPickerContext",
    errorMessage: "useMonthPickerContext must be used within an MonthPickerContextProvider",
});
exports.useMonthPickerContext = useMonthPickerContext;
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] = (0, hooks_1.useControllableState)({
        defaultValue: (_b = (0, date_utils_1.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] = (0, hooks_1.useControllableState)({
        defaultValue: defaultSelected,
        value: selected,
        onChange: onMonthSelect,
    });
    const handleDisplayYearUpdate = (newYear) => {
        if ((0, date_fns_1.isSameYear)(newYear, displayYear)) {
            return;
        }
        setDisplayYear(newYear);
    };
    return (react_1.default.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));
};
exports.MonthPickerProvider = MonthPickerProvider;
//# sourceMappingURL=MonthPicker.context.js.map