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 _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, { forwardRef, useEffect, useLayoutEffect, useRef, useState } from "react";
import cls from "classnames";
import { useForkRef } from "../../hooks";
import { base, CalendarContainerOverlay, LeftHelper } from "./DateTimePicker.styles";
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 { CalendarGrid, DateShortcutList, Input, Popover, StyledSeparator, TimeGrid } from "./ui";
import { classes } from "./DateTimePicker.tokens";
import { useDateTimePicker } from "./hooks/useDateTimePicker";
import { useKeyNavigation } from "./hooks/useKeyboardNavigation";
// changes to start tests
export var dateTimePickerRoot = function dateTimePickerRoot(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, outerValue = _ref2.value, _ref_defaultDate = _ref2.defaultDate, defaultDate = _ref_defaultDate === void 0 ? '' : _ref_defaultDate, // variation props
        size = _ref2.size, view = _ref2.view, _ref_readOnly = _ref2.readOnly, readOnly = _ref_readOnly === void 0 ? false : _ref_readOnly, _ref_disabled = _ref2.disabled, disabled = _ref_disabled === void 0 ? false : _ref_disabled, // popover props
        outerOpened = _ref2.opened, _ref_placement = _ref2.placement, placement = _ref_placement === void 0 ? [
            'top',
            'bottom'
        ] : _ref_placement, offset = _ref2.offset, _ref_frame = _ref2.frame, frame = _ref_frame === void 0 ? 'document' : _ref_frame, zIndex = _ref2.zIndex, _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_usePortal = _ref2.usePortal, usePortal = _ref_usePortal === void 0 ? false : _ref_usePortal, _ref_type = _ref2.// calendar props
        type, type = _ref_type === void 0 ? 'Days' : _ref_type, _ref_lang = _ref2.lang, lang = _ref_lang === void 0 ? 'ru' : _ref_lang, _ref_dateFormat = _ref2.dateFormat, dateFormat = _ref_dateFormat === void 0 ? 'DD.MM.YYYY' : _ref_dateFormat, _ref_timeFormat = _ref2.timeFormat, timeFormat = _ref_timeFormat === void 0 ? 'HH:mm:ss' : _ref_timeFormat, _ref_dateTimeSeparator = _ref2.dateTimeSeparator, dateTimeSeparator = _ref_dateTimeSeparator === void 0 ? ' ' : _ref_dateTimeSeparator, maskWithFormat = _ref2.maskWithFormat, min = _ref2.min, max = _ref2.max, renderFromDate = _ref2.renderFromDate, includeEdgeDates = _ref2.includeEdgeDates, isDouble = _ref2.isDouble, dateOnTimeSelectOnly = _ref2.dateOnTimeSelectOnly, dateShortcuts = _ref2.dateShortcuts, _ref_dateShortcutsPlacement = _ref2.dateShortcutsPlacement, dateShortcutsPlacement = _ref_dateShortcutsPlacement === void 0 ? 'left' : _ref_dateShortcutsPlacement, dateShortcutsWidth = _ref2.dateShortcutsWidth, 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, calendarContainerWidth = _ref2.calendarContainerWidth, calendarContainerHeight = _ref2.calendarContainerHeight, stretched = _ref2.stretched, // input props
        label = _ref2.label, _ref_labelPlacement = _ref2.labelPlacement, labelPlacement = _ref_labelPlacement === void 0 ? 'outer' : _ref_labelPlacement, keepPlaceholder = _ref2.keepPlaceholder, _ref_requiredPlacement = _ref2.requiredPlacement, requiredPlacement = _ref_requiredPlacement === void 0 ? 'right' : _ref_requiredPlacement, _ref_required = _ref2.required, required = _ref_required === void 0 ? false : _ref_required, _ref_hasRequiredIndicator = _ref2.hasRequiredIndicator, hasRequiredIndicator = _ref_hasRequiredIndicator === void 0 ? false : _ref_hasRequiredIndicator, preserveInvalidOnBlur = _ref2.preserveInvalidOnBlur, placeholder = _ref2.placeholder, valueError = _ref2.valueError, valueSuccess = _ref2.valueSuccess, leftHelper = _ref2.leftHelper, contentLeft = _ref2.contentLeft, contentRight = _ref2.contentRight, textBefore = _ref2.textBefore, textAfter = _ref2.textAfter, autoComplete = _ref2.autoComplete, _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, // callbacks
        onChangeValue = _ref2.onChangeValue, onCommitDate = _ref2.onCommitDate, onToggle = _ref2.onToggle, onFocus = _ref2.onFocus, onBlur = _ref2.onBlur, rest = _object_without_properties(_ref2, [
            "className",
            "value",
            "defaultDate",
            "size",
            "view",
            "readOnly",
            "disabled",
            "opened",
            "placement",
            "offset",
            "frame",
            "zIndex",
            "closeOnOverlayClick",
            "closeOnEsc",
            "usePortal",
            "type",
            "lang",
            "dateFormat",
            "timeFormat",
            "dateTimeSeparator",
            "maskWithFormat",
            "min",
            "max",
            "renderFromDate",
            "includeEdgeDates",
            "isDouble",
            "dateOnTimeSelectOnly",
            "dateShortcuts",
            "dateShortcutsPlacement",
            "dateShortcutsWidth",
            "eventTooltipOptions",
            "eventList",
            "disabledList",
            "eventMonthList",
            "disabledMonthList",
            "eventQuarterList",
            "disabledQuarterList",
            "eventYearList",
            "disabledYearList",
            "calendarContainerWidth",
            "calendarContainerHeight",
            "stretched",
            "label",
            "labelPlacement",
            "keepPlaceholder",
            "requiredPlacement",
            "required",
            "hasRequiredIndicator",
            "preserveInvalidOnBlur",
            "placeholder",
            "valueError",
            "valueSuccess",
            "leftHelper",
            "contentLeft",
            "contentRight",
            "textBefore",
            "textAfter",
            "autoComplete",
            "hintTrigger",
            "hintText",
            "hintView",
            "hintSize",
            "hintTargetIcon",
            "hintTargetPlacement",
            "hintPlacement",
            "hintHasArrow",
            "hintOffset",
            "hintWidth",
            "hintContentLeft",
            "onChangeValue",
            "onCommitDate",
            "onToggle",
            "onFocus",
            "onBlur"
        ]), _rest1 = _sliced_to_array(_rest, 1), ref = _rest1[0];
        var inputRef = useRef(null);
        var inputInnerRef = useForkRef(inputRef, ref);
        var calendarOverlayRef = useRef(null);
        var _useState = _sliced_to_array(useState(false), 2), isInnerOpen = _useState[0], setIsInnerOpen = _useState[1];
        var openedValue = outerOpened !== null && outerOpened !== void 0 ? outerOpened : isInnerOpen;
        var _useDateTimePicker = useDateTimePicker({
            inputRef: inputRef,
            outerValue: outerValue,
            valueError: valueError,
            valueSuccess: valueSuccess,
            type: type,
            lang: lang,
            disabled: disabled,
            readOnly: readOnly,
            maskWithFormat: maskWithFormat,
            dateTimeSeparator: dateTimeSeparator,
            dateFormat: dateFormat,
            timeFormat: timeFormat,
            max: max,
            min: min,
            includeEdgeDates: includeEdgeDates,
            dateOnTimeSelectOnly: dateOnTimeSelectOnly,
            onChangeValue: onChangeValue,
            onCommitDate: onCommitDate,
            onBlur: onBlur
        }), format = _useDateTimePicker.format, dateVisibleValue = _useDateTimePicker.dateVisibleValue, calendarGridValue = _useDateTimePicker.calendarGridValue, inputValue = _useDateTimePicker.inputValue, timeVisibleValue = _useDateTimePicker.timeVisibleValue, timeColumnsCount = _useDateTimePicker.timeColumnsCount, errorClass = _useDateTimePicker.errorClass, successClass = _useDateTimePicker.successClass, handleChangeValue = _useDateTimePicker.handleChangeValue, handleSearch = _useDateTimePicker.handleSearch, handleBlur = _useDateTimePicker.handleBlur, handleCalendarPick = _useDateTimePicker.handleCalendarPick, handleTimePick = _useDateTimePicker.handleTimePick, updateExternalDate = _useDateTimePicker.updateExternalDate, isDateEqualEdge = _useDateTimePicker.isDateEqualEdge;
        var handleCalendarOverlayClick = function handleCalendarOverlayClick(event) {
            if (disabled || readOnly) {
                return;
            }
            event.stopPropagation();
            if (isInnerOpen && stretched && event.target === (calendarOverlayRef === null || calendarOverlayRef === void 0 ? void 0 : calendarOverlayRef.current)) {
                setIsInnerOpen(false);
                if (onToggle) {
                    onToggle(false, event);
                }
            }
        };
        var handleToggle = function handleToggle(innerOpened, event) {
            if (disabled || readOnly) {
                return;
            }
            var isCalendarOpen = (event === null || event === void 0 ? void 0 : event.target) === (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) && event.code !== 'Escape' ? true : innerOpened;
            if (onToggle) {
                onToggle(isCalendarOpen, event);
            }
            setIsInnerOpen(isCalendarOpen);
        };
        var onKeyDown = useKeyNavigation({
            opened: isInnerOpen,
            closeOnEsc: closeOnEsc,
            onToggle: handleToggle
        }).onKeyDown;
        useEffect(function() {
            if (disabled || readOnly) {
                setIsInnerOpen(false);
                if (onToggle) {
                    onToggle(false);
                }
            }
        }, [
            disabled,
            readOnly
        ]);
        useLayoutEffect(function() {
            if (!dateVisibleValue) {
                updateExternalDate(defaultDate);
            }
        }, [
            defaultDate,
            format,
            lang
        ]);
        return /*#__PURE__*/ React.createElement(Root, _object_spread({
            view: view,
            size: size,
            className: cls(classes.root, className, _define_property({}, classes.stretched, stretched)),
            disabled: disabled,
            readOnly: !disabled && readOnly,
            eventTooltipSize: eventTooltipOptions === null || eventTooltipOptions === void 0 ? void 0 : eventTooltipOptions.size
        }, hintText && {
            hintView: hintView,
            hintSize: hintSize
        }), /*#__PURE__*/ React.createElement(Popover, {
            target: /*#__PURE__*/ React.createElement(Input, _object_spread({
                ref: inputInnerRef,
                className: cls(errorClass, successClass),
                value: inputValue,
                size: size,
                label: label,
                labelPlacement: labelPlacement,
                keepPlaceholder: keepPlaceholder,
                requiredPlacement: requiredPlacement,
                required: required,
                hasRequiredIndicator: hasRequiredIndicator,
                preserveInvalidOnBlur: preserveInvalidOnBlur,
                placeholder: placeholder,
                valueError: valueError,
                valueSuccess: valueSuccess,
                contentLeft: contentLeft,
                contentRight: contentRight,
                textBefore: textBefore,
                textAfter: textAfter,
                autoComplete: autoComplete,
                readOnly: readOnly,
                disabled: disabled,
                onChange: handleChangeValue,
                onSearch: function onSearch(e) {
                    var _e_currentTarget;
                    return handleSearch(e === null || e === void 0 ? void 0 : (_e_currentTarget = e.currentTarget) === null || _e_currentTarget === void 0 ? void 0 : _e_currentTarget.value);
                },
                onFocus: onFocus,
                onBlur: handleBlur,
                onKeyDown: onKeyDown
            }, hintText ? {
                hintText: hintText,
                hintView: hintView,
                hintSize: hintSize,
                hintTrigger: hintTrigger,
                hintTargetIcon: hintTargetIcon,
                hintPlacement: hintPlacement,
                hintHasArrow: hintHasArrow,
                hintOffset: hintOffset,
                hintWidth: hintWidth,
                hintContentLeft: hintContentLeft,
                hintTargetPlacement: hintTargetPlacement
            } : {
                hintText: undefined
            }, rest)),
            opened: openedValue,
            usePortal: usePortal,
            frame: frame,
            offset: offset,
            placement: placement,
            closeOnOverlayClick: closeOnOverlayClick,
            closeOnEsc: closeOnEsc,
            zIndex: zIndex,
            calendarContainerWidth: calendarContainerWidth,
            calendarContainerHeight: calendarContainerHeight,
            onToggle: handleToggle
        }, /*#__PURE__*/ React.createElement(CalendarContainerOverlay, {
            ref: calendarOverlayRef,
            onClick: handleCalendarOverlayClick
        }), /*#__PURE__*/ React.createElement(Root, {
            view: view,
            size: size,
            className: cls(classes.root, className, _define_property({}, classes.stretched, stretched)),
            disabled: disabled,
            readOnly: !disabled && readOnly
        }, (dateShortcuts === null || dateShortcuts === void 0 ? void 0 : dateShortcuts.length) ? /*#__PURE__*/ React.createElement(DateShortcutList, {
            items: dateShortcuts,
            setShortcutDate: handleCalendarPick,
            dateShortcutsWidth: dateShortcutsWidth,
            calendarContainerHeight: calendarContainerHeight,
            dateShortcutsPlacement: dateShortcutsPlacement
        }) : null, /*#__PURE__*/ React.createElement(CalendarGrid, {
            value: calendarGridValue,
            isDouble: isDouble,
            calendarContainerWidth: calendarContainerWidth,
            calendarContainerHeight: calendarContainerHeight,
            type: type,
            eventTooltipOptions: eventTooltipOptions,
            eventList: eventList,
            disabledList: disabledList,
            eventMonthList: eventMonthList,
            disabledMonthList: disabledMonthList,
            eventQuarterList: eventQuarterList,
            disabledQuarterList: disabledQuarterList,
            eventYearList: eventYearList,
            disabledYearList: disabledYearList,
            min: min,
            max: max,
            renderFromDate: renderFromDate,
            includeEdgeDates: includeEdgeDates,
            lang: lang,
            handleCalendarPick: handleCalendarPick
        }), /*#__PURE__*/ React.createElement(StyledSeparator, null), /*#__PURE__*/ React.createElement(TimeGrid, _object_spread({
            value: timeVisibleValue,
            format: timeFormat,
            columns: timeColumnsCount,
            calendarContainerWidth: calendarContainerWidth,
            calendarContainerHeight: calendarContainerHeight,
            dropdownHeight: calendarContainerHeight,
            onChange: handleTimePick
        }, isDateEqualEdge(min) && {
            min: min
        }, isDateEqualEdge(max) && {
            max: max
        })))), leftHelper && /*#__PURE__*/ React.createElement(LeftHelper, {
            className: cls(errorClass, successClass)
        }, leftHelper));
    });
};
export var dateTimePickerConfig = {
    name: 'DateTimePicker',
    tag: 'div',
    layout: dateTimePickerRoot,
    base: base,
    variations: {
        view: {
            css: viewCSS
        },
        size: {
            css: sizeCSS
        },
        hintView: {
            css: hintViewCSS
        },
        hintSize: {
            css: hintSizeCSS
        },
        eventTooltipSize: {
            css: eventTooltipSizeCSS
        },
        disabled: {
            css: disabledCSS,
            attrs: true
        },
        readOnly: {
            css: readOnlyCSS,
            attrs: true
        }
    },
    defaults: {
        size: 'm',
        view: 'default',
        hintView: 'default',
        hintSize: 'm'
    }
};
