"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get dateTimePickerConfig () {
        return dateTimePickerConfig;
    },
    get dateTimePickerRoot () {
        return dateTimePickerRoot;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _classnames = /*#__PURE__*/ _interop_require_default(require("classnames"));
const _hooks = require("../../hooks");
const _DateTimePickerstyles = require("./DateTimePicker.styles");
const _base = require("./variations/_size/base");
const _base1 = require("./variations/_view/base");
const _base2 = require("./variations/_hint-view/base");
const _base3 = require("./variations/_hint-size/base");
const _base4 = require("./variations/_tooltip-size/base");
const _base5 = require("./variations/_disabled/base");
const _base6 = require("./variations/_readonly/base");
const _ui = require("./ui");
const _DateTimePickertokens = require("./DateTimePicker.tokens");
const _useDateTimePicker = require("./hooks/useDateTimePicker");
const _useKeyboardNavigation = require("./hooks/useKeyboardNavigation");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const dateTimePickerRoot = (Root)=>/*#__PURE__*/ (0, _react.forwardRef)(({ className, value: outerValue, defaultDate = '', // variation props
    size, view, readOnly = false, disabled = false, appearance, hasClearDivider, // popover props
    opened: outerOpened, placement = [
        'top',
        'bottom'
    ], offset, frame = 'document', zIndex, closeOnOverlayClick = true, closeOnEsc = true, usePortal = false, // calendar props
    type = 'Days', lang = 'ru', dateFormat = 'DD.MM.YYYY', timeFormat = 'HH:mm:ss', dateTimeSeparator = ' ', maskWithFormat, min, max, renderFromDate, includeEdgeDates, isDouble, dateOnTimeSelectOnly, dateShortcuts, dateShortcutsPlacement = 'left', dateShortcutsWidth, eventTooltipOptions, eventList, disabledList, eventMonthList, disabledMonthList, eventQuarterList, disabledQuarterList, eventYearList, disabledYearList, calendarContainerWidth, calendarContainerHeight, stretched, // input props
    label, labelPlacement = 'outer', keepPlaceholder, requiredPlacement = 'right', required = false, hasRequiredIndicator = false, preserveInvalidOnBlur, placeholder, valueError, valueSuccess, valueEdited, leftHelper, contentLeft, contentRight, textBefore, textAfter, autoComplete, // hint
    hintTrigger = 'hover', hintText, hintView = 'default', hintSize = 'm', hintTargetIcon, hintTargetPlacement = 'outer', hintPlacement = 'auto', hintHasArrow, hintOffset = [
        0,
        0
    ], hintWidth, hintContentLeft, // callbacks
    onChangeValue, onCommitDate, onToggle, onFocus, onBlur, ...rest }, ref)=>{
        const inputRef = (0, _react.useRef)(null);
        const inputInnerRef = (0, _hooks.useForkRef)(inputRef, ref);
        const calendarOverlayRef = (0, _react.useRef)(null);
        const [isInnerOpen, setIsInnerOpen] = (0, _react.useState)(false);
        const openedValue = outerOpened ?? isInnerOpen;
        const { format, dateVisibleValue, calendarGridValue, inputValue, timeVisibleValue, timeColumnsCount, errorClass, successClass, editedClass, handleChangeValue, handleSearch, handleBlur, handleCalendarPick, handleTimePick, updateExternalDate, isDateEqualEdge } = (0, _useDateTimePicker.useDateTimePicker)({
            inputRef,
            outerValue,
            valueError,
            valueSuccess,
            valueEdited,
            type,
            lang,
            disabled,
            readOnly,
            maskWithFormat,
            dateTimeSeparator,
            dateFormat,
            timeFormat,
            max,
            min,
            includeEdgeDates,
            dateOnTimeSelectOnly,
            onChangeValue,
            onCommitDate,
            onBlur
        });
        const handleCalendarOverlayClick = (event)=>{
            if (disabled || readOnly) {
                return;
            }
            event.stopPropagation();
            if (isInnerOpen && stretched && event.target === calendarOverlayRef?.current) {
                setIsInnerOpen(false);
                if (onToggle) {
                    onToggle(false, event);
                }
            }
        };
        const handleToggle = (innerOpened, event)=>{
            if (disabled || readOnly) {
                return;
            }
            const isCalendarOpen = event?.target === inputRef?.current && event.code !== 'Escape' ? true : innerOpened;
            if (onToggle) {
                onToggle(isCalendarOpen, event);
            }
            setIsInnerOpen(isCalendarOpen);
        };
        const { onKeyDown } = (0, _useKeyboardNavigation.useKeyNavigation)({
            opened: isInnerOpen,
            closeOnEsc,
            onToggle: handleToggle
        });
        (0, _react.useEffect)(()=>{
            if (disabled || readOnly) {
                setIsInnerOpen(false);
                if (onToggle) {
                    onToggle(false);
                }
            }
        }, [
            disabled,
            readOnly
        ]);
        (0, _react.useLayoutEffect)(()=>{
            if (!dateVisibleValue) {
                updateExternalDate(defaultDate);
            }
        }, [
            defaultDate,
            format,
            lang
        ]);
        return /*#__PURE__*/ _react.default.createElement(Root, {
            view: view,
            size: size,
            className: (0, _classnames.default)(_DateTimePickertokens.classes.root, className, {
                [_DateTimePickertokens.classes.stretched]: stretched
            }),
            disabled: disabled,
            readOnly: !disabled && readOnly,
            eventTooltipSize: eventTooltipOptions?.size,
            ...hintText && {
                hintView,
                hintSize
            }
        }, /*#__PURE__*/ _react.default.createElement(_ui.Popover, {
            target: /*#__PURE__*/ _react.default.createElement(_ui.Input, {
                ref: inputInnerRef,
                className: (0, _classnames.default)(errorClass, successClass, editedClass),
                value: inputValue,
                size: size,
                label: label,
                labelPlacement: labelPlacement,
                keepPlaceholder: keepPlaceholder,
                requiredPlacement: requiredPlacement,
                required: required,
                hasRequiredIndicator: hasRequiredIndicator,
                preserveInvalidOnBlur: preserveInvalidOnBlur,
                placeholder: placeholder,
                valueError: valueError,
                valueSuccess: valueSuccess,
                valueEdited: valueEdited,
                contentLeft: contentLeft,
                contentRight: contentRight,
                textBefore: textBefore,
                textAfter: textAfter,
                autoComplete: autoComplete,
                readOnly: readOnly,
                disabled: disabled,
                hasClearDivider: appearance === 'clear' && hasClearDivider,
                onChange: handleChangeValue,
                onSearch: (e)=>handleSearch(e?.currentTarget?.value),
                onFocus: onFocus,
                onBlur: handleBlur,
                onKeyDown: onKeyDown,
                ...hintText ? {
                    hintText,
                    hintView,
                    hintSize,
                    hintTrigger,
                    hintTargetIcon,
                    hintPlacement,
                    hintHasArrow,
                    hintOffset,
                    hintWidth,
                    hintContentLeft,
                    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.default.createElement(_DateTimePickerstyles.CalendarContainerOverlay, {
            ref: calendarOverlayRef,
            onClick: handleCalendarOverlayClick
        }), /*#__PURE__*/ _react.default.createElement(Root, {
            view: view,
            size: size,
            className: (0, _classnames.default)(_DateTimePickertokens.classes.root, className, {
                [_DateTimePickertokens.classes.stretched]: stretched
            }),
            disabled: disabled,
            readOnly: !disabled && readOnly
        }, dateShortcuts?.length ? /*#__PURE__*/ _react.default.createElement(_ui.DateShortcutList, {
            items: dateShortcuts,
            setShortcutDate: handleCalendarPick,
            dateShortcutsWidth: dateShortcutsWidth,
            calendarContainerHeight: calendarContainerHeight,
            dateShortcutsPlacement: dateShortcutsPlacement
        }) : null, /*#__PURE__*/ _react.default.createElement(_ui.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.default.createElement(_ui.StyledSeparator, null), /*#__PURE__*/ _react.default.createElement(_ui.TimeGrid, {
            value: timeVisibleValue,
            format: timeFormat,
            columns: timeColumnsCount,
            calendarContainerWidth: calendarContainerWidth,
            calendarContainerHeight: calendarContainerHeight,
            dropdownHeight: calendarContainerHeight,
            onChange: handleTimePick,
            ...isDateEqualEdge(min) && {
                min
            },
            ...isDateEqualEdge(max) && {
                max
            }
        }))), leftHelper && /*#__PURE__*/ _react.default.createElement(_DateTimePickerstyles.LeftHelper, {
            className: (0, _classnames.default)(errorClass, successClass, editedClass)
        }, leftHelper));
    });
const dateTimePickerConfig = {
    name: 'DateTimePicker',
    tag: 'div',
    layout: dateTimePickerRoot,
    base: _DateTimePickerstyles.base,
    variations: {
        view: {
            css: _base1.base
        },
        size: {
            css: _base.base
        },
        hintView: {
            css: _base2.base
        },
        hintSize: {
            css: _base3.base
        },
        eventTooltipSize: {
            css: _base4.base
        },
        disabled: {
            css: _base5.base,
            attrs: true
        },
        readOnly: {
            css: _base6.base,
            attrs: true
        }
    },
    defaults: {
        size: 'm',
        view: 'default',
        hintView: 'default',
        hintSize: 'm'
    }
};
