function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _iterable_to_array(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable;
            });
        }
        keys.push.apply(keys, symbols);
    }
    return keys;
}
function _object_spread_props(target, source) {
    source = source != null ? source : {};
    if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
        ownKeys(Object(source)).forEach(function(key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
    }
    return target;
}
function _object_without_properties(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys, key, i;
    if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
        sourceKeys = Reflect.ownKeys(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
        return target;
    }
    target = _object_without_properties_loose(source, excluded);
    if (Object.getOwnPropertySymbols) {
        sourceKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
    }
    return target;
}
function _object_without_properties_loose(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
        target[key] = source[key];
    }
    return target;
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _to_array(arr) {
    return _array_with_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import React, { createRef, forwardRef, useCallback, useEffect, useLayoutEffect, useRef, useState } from "react";
import cls from "classnames";
import { noop } from "../../../utils";
import { customDayjs } from "../../../utils/datejs";
import { getDateFormatDelimiter } from "../utils/dateHelper";
import { useDatePicker } from "../hooks/useDatePicker";
import { classes } from "../DatePicker.tokens";
import { keys, useKeyNavigation } from "../hooks/useKeyboardNavigation";
import { InputHidden } from "../DatePickerBase.styles";
import { getSortedValues } from "../../Calendar/utils";
import { getFormattedDates, invokeOnCommitDate } from "../utils";
import { base as sizeCSS } from "./variations/_size/base";
import { base as viewCSS } from "./variations/_view/base";
import { base as hintViewCSS } from "./variations/_hint-view/base";
import { base as hintSizeCSS } from "./variations/_hint-size/base";
import { base as eventTooltipSizeCSS } from "./variations/_tooltip-size/base";
import { base as disabledCSS } from "./variations/_disabled/base";
import { base as readOnlyCSS } from "./variations/_readonly/base";
import { StyledRange, base } from "./RangeDate.styles";
import { RangeDatePopover } from "./RangeDatePopover/RangeDatePopover";
export var datePickerRangeRoot = function datePickerRangeRoot(Root) {
    return /*#__PURE__*/ forwardRef(function(_0, _1) {
        var _ref = [
            _0,
            _1
        ], _ref1 = _to_array(_ref), _ref2 = _ref1[0], _rest = _ref1.slice(1), className = _ref2.className, name = _ref2.name, autoComplete = _ref2.autoComplete, _ref_isDoubleCalendar = _ref2.// controlled
        isDoubleCalendar, isDoubleCalendar = _ref_isDoubleCalendar === void 0 ? false : _ref_isDoubleCalendar, outerOpened = _ref2.opened, outerValue = _ref2.value, _ref_defaultFirstDate = _ref2.defaultFirstDate, defaultFirstDate = _ref_defaultFirstDate === void 0 ? '' : _ref_defaultFirstDate, _ref_defaultSecondDate = _ref2.defaultSecondDate, defaultSecondDate = _ref_defaultSecondDate === void 0 ? '' : _ref_defaultSecondDate, preserveInvalidOnBlur = _ref2.preserveInvalidOnBlur, // variations
        view = _ref2.view, size = _ref2.size, _ref_readOnly = _ref2.readOnly, readOnly = _ref_readOnly === void 0 ? false : _ref_readOnly, _ref_disabled = _ref2.disabled, disabled = _ref_disabled === void 0 ? false : _ref_disabled, appearance = _ref2.appearance, hasClearDivider = _ref2.hasClearDivider, stretched = _ref2.stretched, firstValueError = _ref2.firstValueError, secondValueError = _ref2.secondValueError, firstValueSuccess = _ref2.firstValueSuccess, secondValueSuccess = _ref2.secondValueSuccess, // layout
        required = _ref2.required, _ref_requiredPlacement = _ref2.requiredPlacement, requiredPlacement = _ref_requiredPlacement === void 0 ? 'right' : _ref_requiredPlacement, _ref_hasRequiredIndicator = _ref2.hasRequiredIndicator, hasRequiredIndicator = _ref_hasRequiredIndicator === void 0 ? false : _ref_hasRequiredIndicator, label = _ref2.label, leftHelper = _ref2.leftHelper, contentLeft = _ref2.contentLeft, contentRight = _ref2.contentRight, _ref_dividerVariant = _ref2.dividerVariant, dividerVariant = _ref_dividerVariant === void 0 ? 'dash' : _ref_dividerVariant, dividerIcon = _ref2.dividerIcon, firstPlaceholder = _ref2.firstPlaceholder, secondPlaceholder = _ref2.secondPlaceholder, firstTextfieldContentLeft = _ref2.firstTextfieldContentLeft, firstTextfieldContentRight = _ref2.firstTextfieldContentRight, secondTextfieldContentLeft = _ref2.secondTextfieldContentLeft, secondTextfieldContentRight = _ref2.secondTextfieldContentRight, firstTextfieldTextBefore = _ref2.firstTextfieldTextBefore, secondTextfieldTextBefore = _ref2.secondTextfieldTextBefore, firstTextfieldTextAfter = _ref2.firstTextfieldTextAfter, secondTextfieldTextAfter = _ref2.secondTextfieldTextAfter, _ref_hintTrigger = _ref2.// hint
        hintTrigger, hintTrigger = _ref_hintTrigger === void 0 ? 'hover' : _ref_hintTrigger, hintText = _ref2.hintText, _ref_hintView = _ref2.hintView, hintView = _ref_hintView === void 0 ? 'default' : _ref_hintView, _ref_hintSize = _ref2.hintSize, hintSize = _ref_hintSize === void 0 ? 'm' : _ref_hintSize, hintTargetIcon = _ref2.hintTargetIcon, _ref_hintTargetPlacement = _ref2.hintTargetPlacement, hintTargetPlacement = _ref_hintTargetPlacement === void 0 ? 'outer' : _ref_hintTargetPlacement, _ref_hintPlacement = _ref2.hintPlacement, hintPlacement = _ref_hintPlacement === void 0 ? 'auto' : _ref_hintPlacement, hintHasArrow = _ref2.hintHasArrow, _ref_hintOffset = _ref2.hintOffset, hintOffset = _ref_hintOffset === void 0 ? [
            0,
            0
        ] : _ref_hintOffset, hintWidth = _ref2.hintWidth, hintContentLeft = _ref2.hintContentLeft, _ref_frame = _ref2.// calendar-container
        frame, frame = _ref_frame === void 0 ? 'document' : _ref_frame, _ref_usePortal = _ref2.usePortal, usePortal = _ref_usePortal === void 0 ? false : _ref_usePortal, zIndex = _ref2.zIndex, _ref_placement = _ref2.placement, placement = _ref_placement === void 0 ? [
            'top',
            'bottom'
        ] : _ref_placement, _ref_closeOnOverlayClick = _ref2.closeOnOverlayClick, closeOnOverlayClick = _ref_closeOnOverlayClick === void 0 ? true : _ref_closeOnOverlayClick, _ref_closeOnEsc = _ref2.closeOnEsc, closeOnEsc = _ref_closeOnEsc === void 0 ? true : _ref_closeOnEsc, _ref_closeAfterDateSelect = _ref2.closeAfterDateSelect, closeAfterDateSelect = _ref_closeAfterDateSelect === void 0 ? true : _ref_closeAfterDateSelect, offset = _ref2.offset, _ref_format = _ref2.// calendar
        format, format = _ref_format === void 0 ? 'DD.MM.YYYY' : _ref_format, _ref_lang = _ref2.lang, lang = _ref_lang === void 0 ? 'ru' : _ref_lang, maskWithFormat = _ref2.maskWithFormat, min = _ref2.min, max = _ref2.max, renderFromDate = _ref2.renderFromDate, _ref_includeEdgeDates = _ref2.includeEdgeDates, includeEdgeDates = _ref_includeEdgeDates === void 0 ? false : _ref_includeEdgeDates, eventTooltipOptions = _ref2.eventTooltipOptions, eventList = _ref2.eventList, disabledList = _ref2.disabledList, eventMonthList = _ref2.eventMonthList, disabledMonthList = _ref2.disabledMonthList, eventQuarterList = _ref2.eventQuarterList, disabledQuarterList = _ref2.disabledQuarterList, eventYearList = _ref2.eventYearList, disabledYearList = _ref2.disabledYearList, _ref_type = _ref2.type, type = _ref_type === void 0 ? 'Days' : _ref_type, calendarContainerWidth = _ref2.calendarContainerWidth, calendarContainerHeight = _ref2.calendarContainerHeight, dateShortcuts = _ref2.dateShortcuts, _ref_dateShortcutsPlacement = _ref2.dateShortcutsPlacement, dateShortcutsPlacement = _ref_dateShortcutsPlacement === void 0 ? 'left' : _ref_dateShortcutsPlacement, dateShortcutsWidth = _ref2.dateShortcutsWidth, onToggle = _ref2.onToggle, onChange = _ref2.onChange, onChangeFirstValue = _ref2.onChangeFirstValue, onChangeSecondValue = _ref2.onChangeSecondValue, onCommitFirstDate = _ref2.onCommitFirstDate, onCommitSecondDate = _ref2.onCommitSecondDate, onFocusFirstTextfield = _ref2.onFocusFirstTextfield, onFocusSecondTextfield = _ref2.onFocusSecondTextfield, onBlurFirstTextfield = _ref2.onBlurFirstTextfield, onBlurSecondTextfield = _ref2.onBlurSecondTextfield, rest = _object_without_properties(_ref2, [
            "className",
            "name",
            "autoComplete",
            "isDoubleCalendar",
            "opened",
            "value",
            "defaultFirstDate",
            "defaultSecondDate",
            "preserveInvalidOnBlur",
            "view",
            "size",
            "readOnly",
            "disabled",
            "appearance",
            "hasClearDivider",
            "stretched",
            "firstValueError",
            "secondValueError",
            "firstValueSuccess",
            "secondValueSuccess",
            "required",
            "requiredPlacement",
            "hasRequiredIndicator",
            "label",
            "leftHelper",
            "contentLeft",
            "contentRight",
            "dividerVariant",
            "dividerIcon",
            "firstPlaceholder",
            "secondPlaceholder",
            "firstTextfieldContentLeft",
            "firstTextfieldContentRight",
            "secondTextfieldContentLeft",
            "secondTextfieldContentRight",
            "firstTextfieldTextBefore",
            "secondTextfieldTextBefore",
            "firstTextfieldTextAfter",
            "secondTextfieldTextAfter",
            "hintTrigger",
            "hintText",
            "hintView",
            "hintSize",
            "hintTargetIcon",
            "hintTargetPlacement",
            "hintPlacement",
            "hintHasArrow",
            "hintOffset",
            "hintWidth",
            "hintContentLeft",
            "frame",
            "usePortal",
            "zIndex",
            "placement",
            "closeOnOverlayClick",
            "closeOnEsc",
            "closeAfterDateSelect",
            "offset",
            "format",
            "lang",
            "maskWithFormat",
            "min",
            "max",
            "renderFromDate",
            "includeEdgeDates",
            "eventTooltipOptions",
            "eventList",
            "disabledList",
            "eventMonthList",
            "disabledMonthList",
            "eventQuarterList",
            "disabledQuarterList",
            "eventYearList",
            "disabledYearList",
            "type",
            "calendarContainerWidth",
            "calendarContainerHeight",
            "dateShortcuts",
            "dateShortcutsPlacement",
            "dateShortcutsWidth",
            "onToggle",
            "onChange",
            "onChangeFirstValue",
            "onChangeSecondValue",
            "onCommitFirstDate",
            "onCommitSecondDate",
            "onFocusFirstTextfield",
            "onFocusSecondTextfield",
            "onBlurFirstTextfield",
            "onBlurSecondTextfield"
        ]), _rest1 = _sliced_to_array(_rest, 1), ref = _rest1[0];
        var _ref3, _ref4;
        var _rangeRef_current, _rangeRef_current1;
        var rangeRef = ref && 'current' in ref ? ref : /*#__PURE__*/ createRef();
        var rootRef = useRef(null);
        var innerRefFirst = useRef(null);
        var innerRefSecond = useRef(null);
        var _useState = _sliced_to_array(useState(rangeRef === null || rangeRef === void 0 ? void 0 : (_rangeRef_current = rangeRef.current) === null || _rangeRef_current === void 0 ? void 0 : _rangeRef_current.firstTextField()), 2), firstInputRef = _useState[0], setFirstInputRef = _useState[1];
        var _useState1 = _sliced_to_array(useState(rangeRef === null || rangeRef === void 0 ? void 0 : (_rangeRef_current1 = rangeRef.current) === null || _rangeRef_current1 === void 0 ? void 0 : _rangeRef_current1.secondTextField()), 2), secondInputRef = _useState1[0], setSecondInputRef = _useState1[1];
        var _useState2 = _sliced_to_array(useState(false), 2), isInnerOpen = _useState2[0], setIsInnerOpen = _useState2[1];
        var openedValue = outerOpened !== null && outerOpened !== void 0 ? outerOpened : isInnerOpen;
        var dateFormatDelimiter = getDateFormatDelimiter(format);
        var _useState3 = _sliced_to_array(useState(defaultFirstDate || ''), 2), startInnerDate = _useState3[0], setStartInnerDate = _useState3[1];
        var startDateValue = (_ref3 = outerValue === null || outerValue === void 0 ? void 0 : outerValue[0]) !== null && _ref3 !== void 0 ? _ref3 : startInnerDate;
        var startInitialValues = getFormattedDates({
            value: startDateValue,
            delimiter: dateFormatDelimiter,
            lang: lang,
            format: format,
            includeEdgeDates: includeEdgeDates,
            min: min,
            max: max
        });
        var _useState4 = _sliced_to_array(useState(defaultSecondDate || ''), 2), endInnerDate = _useState4[0], setEndInnerDate = _useState4[1];
        var endDateValue = (_ref4 = outerValue === null || outerValue === void 0 ? void 0 : outerValue[1]) !== null && _ref4 !== void 0 ? _ref4 : endInnerDate;
        var endInitialValues = getFormattedDates({
            value: endDateValue,
            delimiter: dateFormatDelimiter,
            lang: lang,
            format: format,
            includeEdgeDates: includeEdgeDates,
            min: min,
            max: max
        });
        var _useState5 = _sliced_to_array(useState({
            calendar: startInitialValues.originalDate,
            input: startInitialValues.formattedDate
        }), 2), correctStartDates = _useState5[0], setCorrectStartDates = _useState5[1];
        var _useState6 = _sliced_to_array(useState({
            calendar: endInitialValues.originalDate,
            input: endInitialValues.formattedDate
        }), 2), correctEndDates = _useState6[0], setCorrectEndDates = _useState6[1];
        var calendarFirstValue = startInitialValues.originalDate;
        var inputFirstValue = startInitialValues.formattedDate;
        var calendarSecondValue = endInitialValues.originalDate;
        var inputSecondValue = endInitialValues.formattedDate;
        var fullDateEntered = Boolean(calendarFirstValue && calendarSecondValue);
        var _useState7 = _sliced_to_array(useState(false), 2), secondTextFieldClicked = _useState7[0], setSecondTextFieldClicked = _useState7[1];
        var handleInputChange = function handleInputChange(value) {
            if (onChange) {
                onChange({
                    target: {
                        value: value,
                        name: name
                    }
                });
            }
        };
        var setFirstInputValue = function setFirstInputValue(value) {
            setStartInnerDate(value);
            handleInputChange("".concat(value, " - ").concat(inputSecondValue));
        };
        var setSecondInputValue = function setSecondInputValue(value) {
            setEndInnerDate(value);
            handleInputChange("".concat(inputFirstValue, " - ").concat(value));
        };
        var commonUseDatePickerArgs = {
            format: format,
            lang: lang,
            disabled: disabled,
            readOnly: readOnly,
            maskWithFormat: maskWithFormat,
            name: name,
            type: type,
            min: min,
            max: max,
            includeEdgeDates: includeEdgeDates,
            dateFormatDelimiter: dateFormatDelimiter
        };
        var _useDatePicker = useDatePicker(_object_spread_props(_object_spread({
            currentValue: inputFirstValue,
            valueError: firstValueError,
            valueSuccess: firstValueSuccess
        }, commonUseDatePickerArgs), {
            setCorrectDates: setCorrectStartDates,
            setInnerDate: setFirstInputValue,
            onChangeValue: onChangeFirstValue,
            onCommitDate: onCommitFirstDate
        })), handleChangeFirstValue = _useDatePicker.handleChangeValue, handleFirstCalendarPick = _useDatePicker.handleCalendarPick, handleSearchFirst = _useDatePicker.handleSearch, updateExternalFirstDate = _useDatePicker.updateExternalDate, getFirstQuarterInfo = _useDatePicker.getQuarterInfo;
        var _useDatePicker1 = useDatePicker(_object_spread_props(_object_spread({
            currentValue: inputSecondValue,
            valueError: secondValueError,
            valueSuccess: secondValueSuccess
        }, commonUseDatePickerArgs), {
            setCorrectDates: setCorrectEndDates,
            setInnerDate: setSecondInputValue,
            onChangeValue: onChangeSecondValue,
            onCommitDate: onCommitSecondDate
        })), handleChangeSecondValue = _useDatePicker1.handleChangeValue, handleSecondCalendarPick = _useDatePicker1.handleCalendarPick, handleSearchSecond = _useDatePicker1.handleSearch, updateExternalSecondDate = _useDatePicker1.updateExternalDate, getSecondQuarterInfo = _useDatePicker1.getQuarterInfo;
        var handleToggle = function handleToggle(currentOpened, event) {
            var _firstInputRef_current, _secondInputRef_current;
            if (disabled || readOnly) {
                return;
            }
            var isCalendarOpen = ((firstInputRef === null || firstInputRef === void 0 ? void 0 : (_firstInputRef_current = firstInputRef.current) === null || _firstInputRef_current === void 0 ? void 0 : _firstInputRef_current.contains((event === null || event === void 0 ? void 0 : event.target) || null)) || (secondInputRef === null || secondInputRef === void 0 ? void 0 : (_secondInputRef_current = secondInputRef.current) === null || _secondInputRef_current === void 0 ? void 0 : _secondInputRef_current.contains((event === null || event === void 0 ? void 0 : event.target) || null))) && (event === null || event === void 0 ? void 0 : event.code) !== keys.Escape ? true : currentOpened;
            if (!isCalendarOpen) {
                setSecondTextFieldClicked(false);
            }
            if (onToggle) {
                onToggle(isCalendarOpen, event);
            }
            setIsInnerOpen(isCalendarOpen);
        };
        var handleFocusFirstTextField = function handleFocusFirstTextField(event) {
            onFocusFirstTextfield === null || onFocusFirstTextfield === void 0 ? void 0 : onFocusFirstTextfield(event);
            setSecondTextFieldClicked(false);
        };
        var handleFocusSecondTextField = function handleFocusSecondTextField(event) {
            onFocusSecondTextfield === null || onFocusSecondTextfield === void 0 ? void 0 : onFocusSecondTextfield(event);
            setSecondTextFieldClicked(true);
        };
        var handleBlur = function handleBlur(event, outerHandler) {
            if (!inputFirstValue || !inputSecondValue || preserveInvalidOnBlur) {
                outerHandler === null || outerHandler === void 0 ? void 0 : outerHandler(event);
                return;
            }
            var commonArgs = {
                lang: lang,
                format: format,
                includeEdgeDates: includeEdgeDates,
                min: min,
                max: max
            };
            var _getFormattedDates = getFormattedDates(_object_spread({
                value: inputFirstValue,
                delimiter: dateFormatDelimiter
            }, commonArgs)), startOriginalDate = _getFormattedDates.originalDate;
            var _getFormattedDates1 = getFormattedDates(_object_spread({
                value: inputSecondValue,
                delimiter: dateFormatDelimiter
            }, commonArgs)), endOriginalDate = _getFormattedDates1.originalDate;
            if (!calendarFirstValue && correctStartDates.calendar) {
                startOriginalDate = new Date(correctStartDates.calendar);
            }
            if (!calendarSecondValue && correctEndDates.calendar) {
                endOriginalDate = new Date(correctEndDates.calendar);
            }
            var _getSortedValues = _sliced_to_array(getSortedValues([
                startOriginalDate,
                endOriginalDate
            ]), 2), startValue = _getSortedValues[0], endValue = _getSortedValues[1];
            var _getFormattedDates2 = getFormattedDates(_object_spread({
                value: startValue,
                delimiter: dateFormatDelimiter
            }, commonArgs)), formattedFirstDate = _getFormattedDates2.formattedDate, originalFirstDate = _getFormattedDates2.originalDate, isoFirstDate = _getFormattedDates2.isoDate;
            var _getFormattedDates3 = getFormattedDates(_object_spread({
                value: endValue,
                delimiter: dateFormatDelimiter
            }, commonArgs)), formattedSecondDate = _getFormattedDates3.formattedDate, originalSecondDate = _getFormattedDates3.originalDate, isoSecondDate = _getFormattedDates3.isoDate;
            if (formattedFirstDate !== inputFirstValue) {
                if (onChangeFirstValue) {
                    onChangeFirstValue(event, formattedFirstDate, originalFirstDate, isoFirstDate);
                }
                if (onCommitFirstDate) {
                    var dateInfo = originalFirstDate ? getFirstQuarterInfo(originalFirstDate) : undefined;
                    invokeOnCommitDate({
                        callback: onCommitFirstDate,
                        value: formattedFirstDate,
                        formattedValues: {
                            error: false,
                            success: true,
                            dateInfo: dateInfo,
                            originalDate: originalFirstDate,
                            isoDate: isoFirstDate
                        }
                    });
                }
            }
            if (formattedSecondDate !== inputSecondValue) {
                if (onChangeSecondValue) {
                    onChangeSecondValue(event, formattedSecondDate, originalSecondDate, isoSecondDate);
                }
                if (onCommitSecondDate) {
                    var dateInfo1 = originalSecondDate ? getSecondQuarterInfo(originalSecondDate) : undefined;
                    invokeOnCommitDate({
                        callback: onCommitSecondDate,
                        value: formattedSecondDate,
                        formattedValues: {
                            error: false,
                            success: true,
                            dateInfo: dateInfo1,
                            originalDate: originalSecondDate,
                            isoDate: isoSecondDate
                        }
                    });
                }
            }
            setFirstInputValue(startValue);
            setSecondInputValue(endValue);
            outerHandler === null || outerHandler === void 0 ? void 0 : outerHandler(event);
        };
        var onKeyDown = useKeyNavigation({
            isCalendarOpen: isInnerOpen,
            format: format,
            maskWithFormat: maskWithFormat,
            delimiter: dateFormatDelimiter,
            closeOnEsc: closeOnEsc,
            onToggle: handleToggle
        }).onKeyDown;
        var handleShortcutDateSelect = function handleShortcutDateSelect(shortcutDate) {
            handleFirstCalendarPick(shortcutDate[0]);
            handleSecondCalendarPick(shortcutDate[1]);
        };
        var handleChangeStartOfRange = function handleChangeStartOfRange(chosenDate, dateInfo) {
            if (!fullDateEntered) {
                handleFirstCalendarPick(chosenDate, dateInfo);
                handleSecondCalendarPick(undefined);
                return;
            }
            var prevValue = secondTextFieldClicked ? calendarFirstValue : calendarSecondValue;
            var _getSortedValues = _sliced_to_array(getSortedValues([
                prevValue,
                chosenDate
            ]), 2), first = _getSortedValues[0], second = _getSortedValues[1];
            /**
                 * NOTE: проверяем совпадает ли новая дата с предыдущей
                 * Если нет, то вызываем handle{First,Second}CalendarPick
                 */ customDayjs.locale(lang);
            var firstFormatted = first ? customDayjs(first).format(format) : '';
            var secondFormatted = second ? customDayjs(second).format(format) : '';
            if (firstFormatted !== inputFirstValue) {
                handleFirstCalendarPick(first, dateInfo);
            }
            if (secondFormatted !== inputSecondValue) {
                handleSecondCalendarPick(second, dateInfo);
            }
            if (!firstValueError && !secondValueError && closeAfterDateSelect) {
                handleToggle(false);
            }
        };
        var handleChangeCalendarValue = function handleChangeCalendarValue(param, dateInfo) {
            var _param = _sliced_to_array(param, 2), firstDate = _param[0], secondDate = _param[1];
            /**
                 * NOTE: проверяем совпадает ли новая дата с предыдущей
                 * Если нет, то вызываем handle{First,Second}CalendarPick
                 */ customDayjs.locale(lang);
            var firstFormatted = firstDate ? customDayjs(firstDate).format(format) : '';
            var secondFormatted = secondDate ? customDayjs(secondDate).format(format) : '';
            if (firstFormatted !== inputFirstValue) {
                handleFirstCalendarPick(firstDate, dateInfo);
            }
            if (secondFormatted !== inputSecondValue) {
                handleSecondCalendarPick(secondDate, dateInfo);
            }
            if (firstDate && secondDate && !firstValueError && !secondValueError && closeAfterDateSelect) {
                handleToggle(false);
            }
        };
        var RangeComponent = /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement(StyledRange, {
            size: size,
            ref: rangeRef,
            autoComplete: autoComplete,
            dividerIcon: dividerIcon,
            dividerVariant: dividerVariant,
            label: label,
            leftHelper: leftHelper,
            required: required,
            requiredIndicatorPlacement: requiredPlacement,
            hasRequiredIndicator: hasRequiredIndicator,
            disabled: disabled,
            readOnly: !disabled && readOnly,
            firstPlaceholder: firstPlaceholder,
            secondPlaceholder: secondPlaceholder,
            contentLeft: contentLeft,
            contentRight: contentRight,
            firstTextfieldTextBefore: firstTextfieldTextBefore,
            secondTextfieldTextBefore: secondTextfieldTextBefore,
            firstTextfieldTextAfter: firstTextfieldTextAfter,
            secondTextfieldTextAfter: secondTextfieldTextAfter,
            firstTextfieldContentLeft: firstTextfieldContentLeft,
            firstTextfieldContentRight: firstTextfieldContentRight,
            secondTextfieldContentLeft: secondTextfieldContentLeft,
            secondTextfieldContentRight: secondTextfieldContentRight,
            firstValue: inputFirstValue,
            secondValue: inputSecondValue,
            firstValueError: firstValueError,
            secondValueError: secondValueError,
            firstValueSuccess: firstValueSuccess,
            secondValueSuccess: secondValueSuccess,
            onChangeFirstValue: handleChangeFirstValue,
            onChangeSecondValue: handleChangeSecondValue,
            name: name,
            onSearchFirstValue: function onSearchFirstValue(_, date) {
                handleSearchFirst(String(date));
                if (!calendarSecondValue || secondValueError) {
                    var _rangeRef_current_secondTextField_current, _rangeRef_current_secondTextField, _rangeRef_current;
                    rangeRef === null || rangeRef === void 0 ? void 0 : (_rangeRef_current = rangeRef.current) === null || _rangeRef_current === void 0 ? void 0 : (_rangeRef_current_secondTextField = _rangeRef_current.secondTextField()) === null || _rangeRef_current_secondTextField === void 0 ? void 0 : (_rangeRef_current_secondTextField_current = _rangeRef_current_secondTextField.current) === null || _rangeRef_current_secondTextField_current === void 0 ? void 0 : _rangeRef_current_secondTextField_current.focus();
                }
            },
            onSearchSecondValue: function onSearchSecondValue(_, date) {
                handleSearchSecond(String(date));
                if (!calendarFirstValue || firstValueError) {
                    var _rangeRef_current_firstTextField_current, _rangeRef_current_firstTextField, _rangeRef_current;
                    rangeRef === null || rangeRef === void 0 ? void 0 : (_rangeRef_current = rangeRef.current) === null || _rangeRef_current === void 0 ? void 0 : (_rangeRef_current_firstTextField = _rangeRef_current.firstTextField()) === null || _rangeRef_current_firstTextField === void 0 ? void 0 : (_rangeRef_current_firstTextField_current = _rangeRef_current_firstTextField.current) === null || _rangeRef_current_firstTextField_current === void 0 ? void 0 : _rangeRef_current_firstTextField_current.focus();
                }
            },
            onFocusFirstTextfield: handleFocusFirstTextField,
            onFocusSecondTextfield: handleFocusSecondTextField,
            onBlurFirstTextfield: function onBlurFirstTextfield1(event) {
                return handleBlur(event, onBlurFirstTextfield);
            },
            onBlurSecondTextfield: function onBlurSecondTextfield1(event) {
                return handleBlur(event, onBlurSecondTextfield);
            },
            onKeyDown: onKeyDown,
            appearance: appearance,
            hasClearDivider: hasClearDivider,
            hintText: hintText,
            hintView: hintView,
            hintSize: hintSize,
            hintTrigger: hintTrigger,
            hintTargetIcon: hintTargetIcon,
            hintPlacement: hintPlacement,
            hintTargetPlacement: hintTargetPlacement,
            hintHasArrow: hintHasArrow,
            hintOffset: hintOffset,
            hintWidth: hintWidth,
            hintContentLeft: hintContentLeft
        }));
        useEffect(function() {
            var _rangeRef_current, _rangeRef_current1;
            setFirstInputRef((_rangeRef_current = rangeRef.current) === null || _rangeRef_current === void 0 ? void 0 : _rangeRef_current.firstTextField());
            setSecondInputRef((_rangeRef_current1 = rangeRef.current) === null || _rangeRef_current1 === void 0 ? void 0 : _rangeRef_current1.secondTextField());
        }, [
            rangeRef.current
        ]);
        useLayoutEffect(function() {
            if (!startDateValue) {
                updateExternalFirstDate(defaultFirstDate);
            }
        }, [
            defaultFirstDate,
            format,
            lang
        ]);
        useLayoutEffect(function() {
            if (!endDateValue) {
                updateExternalSecondDate(defaultSecondDate);
            }
        }, [
            defaultSecondDate,
            format,
            lang
        ]);
        var RootWrapper = useCallback(/*#__PURE__*/ forwardRef(function(param, rootWrapperRef) {
            var children = param.children, rootWrapperClassName = param.className, onClick = param.onClick;
            return /*#__PURE__*/ React.createElement(Root, {
                ref: rootWrapperRef,
                view: view,
                size: size,
                eventTooltipSize: eventTooltipOptions === null || eventTooltipOptions === void 0 ? void 0 : eventTooltipOptions.size,
                className: rootWrapperClassName,
                disabled: disabled,
                readOnly: !disabled && readOnly,
                onClick: onClick
            }, children);
        }), [
            view,
            size,
            disabled,
            readOnly
        ]);
        return /*#__PURE__*/ React.createElement(Root, _object_spread({
            ref: rootRef,
            view: view,
            size: size,
            eventTooltipSize: eventTooltipOptions === null || eventTooltipOptions === void 0 ? void 0 : eventTooltipOptions.size,
            className: cls(classes.datePickerRoot, className, _define_property({}, classes.datePickerstretched, stretched)),
            disabled: disabled,
            readOnly: !disabled && readOnly
        }, hintText && {
            hintView: hintView,
            hintSize: hintSize
        }, rest), /*#__PURE__*/ React.createElement(RangeDatePopover, {
            calendarValue: [
                calendarFirstValue,
                calendarSecondValue
            ],
            target: RangeComponent,
            opened: openedValue,
            includeEdgeDates: includeEdgeDates,
            eventTooltipOptions: eventTooltipOptions,
            eventList: eventList,
            disabledList: disabledList,
            eventMonthList: eventMonthList,
            disabledMonthList: disabledMonthList,
            eventQuarterList: eventQuarterList,
            disabledQuarterList: disabledQuarterList,
            eventYearList: eventYearList,
            disabledYearList: disabledYearList,
            min: min,
            max: max,
            renderFromDate: renderFromDate,
            frame: frame,
            usePortal: usePortal,
            zIndex: zIndex,
            placement: placement,
            closeOnOverlayClick: closeOnOverlayClick,
            closeOnEsc: closeOnEsc,
            offset: offset,
            stretched: stretched,
            calendarContainerWidth: calendarContainerWidth,
            calendarContainerHeight: calendarContainerHeight,
            type: type,
            onToggle: handleToggle,
            lang: lang,
            isDoubleCalendar: isDoubleCalendar,
            rootWrapper: RootWrapper,
            onChangeStartOfRange: handleChangeStartOfRange,
            onChangeValue: handleChangeCalendarValue,
            setIsInnerOpen: setIsInnerOpen,
            dateShortcuts: dateShortcuts,
            dateShortcutsPlacement: dateShortcutsPlacement,
            dateShortcutsWidth: dateShortcutsWidth,
            onShortcutDateSelect: handleShortcutDateSelect
        }), /*#__PURE__*/ React.createElement(InputHidden, _object_spread({
            name: name,
            type: "hidden",
            datatype: "datepicker-double",
            "data-datepicker": "from",
            value: inputFirstValue,
            ref: innerRefFirst
        }, noop)), /*#__PURE__*/ React.createElement(InputHidden, _object_spread({
            name: name,
            type: "hidden",
            datatype: "datepicker-double",
            "data-datepicker": "to",
            value: inputSecondValue,
            ref: innerRefSecond
        }, noop)));
    });
};
export var datePickerRangeConfig = {
    name: 'DatePickerRange',
    tag: 'div',
    layout: datePickerRangeRoot,
    base: base,
    variations: {
        view: {
            css: viewCSS
        },
        size: {
            css: sizeCSS
        },
        eventTooltipSize: {
            css: eventTooltipSizeCSS
        },
        hintView: {
            css: hintViewCSS
        },
        hintSize: {
            css: hintSizeCSS
        },
        disabled: {
            css: disabledCSS,
            attrs: true
        },
        readOnly: {
            css: readOnlyCSS,
            attrs: true
        }
    },
    defaults: {
        size: 'm',
        view: 'default',
        eventTooltipSizeCSS: 'm'
    }
};
