"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.YearMonthDayPicker = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const ConfigProvider_1 = require("../../ConfigProvider");
const DayPicker_1 = require("../DayPicker");
const MonthPicker_1 = require("../MonthPicker");
const YearPicker_1 = require("../YearPicker");
var States;
(function (States) {
    States[States["Days"] = 0] = "Days";
    States[States["Months"] = 1] = "Months";
    States[States["Years"] = 2] = "Years";
})(States || (States = {}));
const YearMonthDayPicker = ({ onChange, date: initialDate, rangeDate, selectedDate, isMondayFirst, hoveredDayDate, onDayHover, isDoubleNavigationButtonsEnabled, }) => {
    const ref = (0, react_1.useRef)(null);
    const [currentState, setState] = (0, react_1.useState)(States.Days);
    const [date, setDate] = (0, react_1.useState)(initialDate);
    // т.к. смена пикера происходит при клике на кнопку внутри одной из вариаций,
    // фокус находился внутри на одной из кнопок, но после смены пикера,
    // браузер скидывает его на рандомный тег, который находится вне пикера,
    // что приводит к срабатыванию события потери фокуса,
    // и, следовательно, закрытию пикера.
    // для решения проблемы, вручную фокусируемся на враппере
    const focusRef = () => ref.current?.focus();
    const handleYearChange = (receivedDate) => {
        focusRef();
        setDate(receivedDate);
        setState(States.Months);
    };
    const handleMonthChange = (receivedDate) => {
        focusRef();
        setDate(receivedDate);
        setState(States.Days);
    };
    const handleMonthHeadBtnClick = () => {
        focusRef();
        setState(States.Years);
    };
    const handleDaysHeadBtnClick = () => {
        focusRef();
        setState(States.Months);
    };
    const { toYearPick, toMonthPick } = (0, react_1.useContext)(ConfigProvider_1.ConfigContext).datePickerLanguageMap;
    (0, react_1.useEffect)(() => {
        setDate(initialDate);
    }, [initialDate]);
    return ((0, jsx_runtime_1.jsxs)("div", { tabIndex: -1, ref: ref, children: [currentState === States.Years && ((0, jsx_runtime_1.jsx)(YearPicker_1.YearPicker, { date: date, selectedDate: selectedDate, onChange: handleYearChange, rangeDate: rangeDate })), currentState === States.Months && ((0, jsx_runtime_1.jsx)(MonthPicker_1.MonthPicker, { date: date, selectedDate: selectedDate, onChange: handleMonthChange, onHeadBtnClick: handleMonthHeadBtnClick, headBtnTitle: toYearPick.single, rangeDate: rangeDate })), currentState === States.Days && ((0, jsx_runtime_1.jsx)(DayPicker_1.DayPicker, { date: date, isDoubleNavigationButtonsEnabled: isDoubleNavigationButtonsEnabled, isMondayFirst: isMondayFirst, selectedDate: selectedDate, onHeadBtnClick: handleDaysHeadBtnClick, headBtnTitle: toMonthPick.single, onChange: onChange, rangeDate: rangeDate, hoveredDayDate: hoveredDayDate, onDayHover: onDayHover }))] }));
};
exports.YearMonthDayPicker = YearMonthDayPicker;
