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, { useMemo, useRef, useState, useCallback } from 'react';
import moment from 'moment';
import { identity, prop } from 'ramda';
import { DataTypes, AllDateRangeValues, getRangeOptionLabel, getLocaleFormat } from '@reltio/mdm-sdk';
import MenuItem from '@mui/material/MenuItem';
import Popover from '@mui/material/Popover';
import Typography from '@mui/material/Typography';
import Menu from '@mui/material/Menu';
import { DateRangePicker } from '../../DateRangePicker';
import { convertValuesToDate, getEditorLabel } from './helpers';
import { useStyles } from './styles';
var dateFormat = moment.localeData().longDateFormat('L');
export var withDateRangeSelector = function (Component) {
    var WithDateRangeSelector = function (props) {
        var values = props.values, options = props.options, _a = props.getValuesDateRange, getValuesDateRange = _a === void 0 ? convertValuesToDate : _a, onChange = props.onChange, _b = props.dataTypeDefinition, dataTypeDefinition = _b === void 0 ? { type: DataTypes.TYPE_DATE } : _b, DateRangePickerProps = props.DateRangePickerProps, MenuProps = props.MenuProps, componentProps = __rest(props, ["values", "options", "getValuesDateRange", "onChange", "dataTypeDefinition", "DateRangePickerProps", "MenuProps"]);
        var rangeOptions = useMemo(function () {
            return options ||
                Object.values(AllDateRangeValues).map(function (option) { return ({
                    value: option,
                    label: getRangeOptionLabel(option)
                }); });
        }, [options]);
        var inputRef = useRef(null);
        var _c = useState(false), isPickingDate = _c[0], setIsPickingDate = _c[1];
        var _d = useState(false), open = _d[0], setOpen = _d[1];
        var styles = useStyles();
        var onItemClick = function (value) {
            if (value === AllDateRangeValues.PICK_DATE) {
                setIsPickingDate(true);
            }
            else {
                setOpen(false);
                setIsPickingDate(false);
                onChange(value);
            }
        };
        var handleToggle = useCallback(function () { return setOpen(function (open) { return !open; }); }, []);
        var onCancel = function () {
            setIsPickingDate(false);
            setOpen(false);
        };
        var isTimestamp = dataTypeDefinition.type === DataTypes.TYPE_TIMESTAMP;
        var enhancePlaceHolder = isTimestamp ? function (timeFormat) { return timeFormat.replace(/^[hH]:/, 'HH:'); } : identity;
        var format = isTimestamp ? getLocaleFormat() : dateFormat;
        var editorPlaceholder = "".concat(enhancePlaceHolder(format), " \u2014 ").concat(enhancePlaceHolder(format)).toUpperCase();
        var editorLabel = isPickingDate
            ? getRangeOptionLabel(AllDateRangeValues.PICK_DATE)
            : getEditorLabel(format, dataTypeDefinition, values, rangeOptions);
        var _e = MenuProps || {}, _f = _e.anchorOrigin, anchorOrigin = _f === void 0 ? { vertical: 'bottom', horizontal: 'left' } : _f, _g = _e.transformOrigin, transformOrigin = _g === void 0 ? { vertical: 'top', horizontal: 'left' } : _g;
        return (React.createElement(React.Fragment, null,
            React.createElement(Component, __assign({}, componentProps, { ref: inputRef, editorLabel: editorLabel, editorPlaceholder: editorPlaceholder, onMenuToggle: handleToggle, isMenuOpen: open, isPickingDate: isPickingDate })),
            isPickingDate ? (React.createElement(Popover, __assign({ open: open, anchorEl: inputRef.current, onClose: onCancel, transformOrigin: transformOrigin, anchorOrigin: anchorOrigin, elevation: 2 }, MenuProps),
                React.createElement(DateRangePicker, __assign({ values: getValuesDateRange(values), onApply: onItemClick, type: dataTypeDefinition.type }, DateRangePickerProps)))) : (React.createElement(Menu, __assign({ variant: "menu", open: open, autoFocus: false, classes: { paper: styles.paper }, anchorEl: inputRef.current, onClose: onCancel, anchorOrigin: anchorOrigin, transformOrigin: transformOrigin, elevation: 2, MenuListProps: {
                    autoFocusItem: open,
                    style: {
                        width: prop('clientWidth', inputRef.current)
                    }
                } }, MenuProps), rangeOptions.map(function (option) { return (React.createElement(MenuItem, { key: option.value, value: option.value, onClick: function () { return onItemClick(option.value); }, className: styles.menuItem },
                React.createElement(Typography, { classes: { root: styles.menuText } }, option.label))); })))));
    };
    WithDateRangeSelector.displayName = 'WithDateRangeSelector';
    return WithDateRangeSelector;
};
