"use strict";
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TimestampEditor = void 0;
var react_1 = __importDefault(require("react"));
var classnames_1 = __importDefault(require("classnames"));
var ramda_1 = require("ramda");
var styles_1 = require("@mui/styles");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var timeViewRenderers_1 = require("@mui/x-date-pickers/timeViewRenderers");
var DateTimePicker_1 = require("@mui/x-date-pickers/DateTimePicker");
var useDateEditor_1 = require("../hooks/useDateEditor");
var styles_2 = require("./styles");
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 = (0, styles_2.useStyles)();
    var _d = (0, useDateEditor_1.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 = (0, styles_1.useTheme)();
    var basicSlotProps = {
        textField: {
            size: 'small',
            variant: 'filled',
            helperText: errorMessage,
            error: Boolean(errorMessage),
            InputProps: {
                classes: {
                    root: (0, classnames_1.default)(styles.inputRoot, (_b = {}, _b[styles.isCrossedOut] = isCrossedOut, _b))
                },
                disableUnderline: (0, ramda_1.isNil)(editorValue)
            },
            InputLabelProps: { shrink: !(0, ramda_1.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 = (0, ramda_1.mergeDeepLeft)(slotProps, basicSlotProps);
    return (react_1.default.createElement(DateTimePicker_1.DateTimePicker, __assign({ format: format || (0, mdm_sdk_1.getLocaleFormat)(), slotProps: editorSlotProps, views: ['year', 'month', 'day', 'hours', 'minutes', 'seconds'], viewRenderers: {
            hours: timeViewRenderers_1.renderTimeViewClock,
            minutes: timeViewRenderers_1.renderTimeViewClock,
            seconds: timeViewRenderers_1.renderTimeViewClock
        }, label: label, value: editorValue, minDate: editorMinDate, maxDate: editorMaxDate, onError: onError, onChange: handleChange, reduceAnimations: true }, otherProps)));
};
exports.TimestampEditor = TimestampEditor;
