var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import React from 'react';
import classnames from 'classnames';
import { isNil, mergeDeepLeft } from 'ramda';
import { useTheme } from '@mui/styles';
import { getLocaleFormat } from '@reltio/mdm-sdk';
import { renderTimeViewClock } from '@mui/x-date-pickers/timeViewRenderers';
import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker';
import { useDateEditor } from '../hooks/useDateEditor';
import { useStyles } from './styles';
export var TimestampEditor = function (_a) {
    var _b;
    var _c;
    var minDate = _a.minDate, maxDate = _a.maxDate, value = _a.value, label = _a.label, slotProps = _a.slotProps, onChange = _a.onChange, format = _a.format, isCrossedOut = _a.isCrossedOut, otherProps = __rest(_a, ["minDate", "maxDate", "value", "label", "slotProps", "onChange", "format", "isCrossedOut"]);
    var styles = useStyles();
    var _d = useDateEditor({ minDate: minDate, maxDate: maxDate, value: value, onChange: onChange }), errorMessage = _d.errorMessage, editorValue = _d.editorValue, editorMaxDate = _d.editorMaxDate, editorMinDate = _d.editorMinDate, onError = _d.onError, handleChange = _d.onChange;
    var currentTheme = useTheme();
    var basicSlotProps = {
        textField: {
            size: 'small',
            variant: 'filled',
            helperText: errorMessage,
            error: Boolean(errorMessage),
            InputProps: {
                classes: {
                    root: classnames(styles.inputRoot, (_b = {}, _b[styles.isCrossedOut] = isCrossedOut, _b))
                },
                disableUnderline: isNil(editorValue)
            },
            InputLabelProps: { shrink: !isNil(label) }
        },
        popper: {
            placement: 'bottom-start',
            // eslint-disable-next-line @typescript-eslint/ban-ts-comment
            // @ts-ignore
            'data-mui-color-scheme': (_c = currentTheme === null || currentTheme === void 0 ? void 0 : currentTheme.palette) === null || _c === void 0 ? void 0 : _c.mode,
            modifiers: [
                {
                    name: 'viewHeightModifier',
                    enabled: true,
                    phase: 'beforeWrite',
                    fn: function (_a) {
                        var state = _a.state;
                        state.styles.popper.height = '358px';
                        if (state.placement.includes('top-start')) {
                            state.styles.popper = __assign(__assign({}, state.styles.popper), { display: 'flex', alignItems: 'flex-end' });
                        }
                        if (state.placement.includes('bottom')) {
                            state.styles.popper = __assign(__assign({}, state.styles.popper), { display: 'block' });
                        }
                    }
                }
            ]
        },
        inputAdornment: { position: 'end', className: styles.adornedEnd }
    };
    var editorSlotProps = mergeDeepLeft(slotProps, basicSlotProps);
    return (React.createElement(DateTimePicker, __assign({ format: format || getLocaleFormat(), slotProps: editorSlotProps, views: ['year', 'month', 'day', 'hours', 'minutes', 'seconds'], viewRenderers: {
            hours: renderTimeViewClock,
            minutes: renderTimeViewClock,
            seconds: renderTimeViewClock
        }, label: label, value: editorValue, minDate: editorMinDate, maxDate: editorMaxDate, onError: onError, onChange: handleChange, reduceAnimations: true }, otherProps)));
};
