"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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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.withDateRangeSelector = void 0;
var react_1 = __importStar(require("react"));
var moment_1 = __importDefault(require("moment"));
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var MenuItem_1 = __importDefault(require("@mui/material/MenuItem"));
var Popover_1 = __importDefault(require("@mui/material/Popover"));
var Typography_1 = __importDefault(require("@mui/material/Typography"));
var Menu_1 = __importDefault(require("@mui/material/Menu"));
var DateRangePicker_1 = require("../../DateRangePicker");
var helpers_1 = require("./helpers");
var styles_1 = require("./styles");
var dateFormat = moment_1.default.localeData().longDateFormat('L');
var withDateRangeSelector = function (Component) {
    var WithDateRangeSelector = function (props) {
        var values = props.values, options = props.options, _a = props.getValuesDateRange, getValuesDateRange = _a === void 0 ? helpers_1.convertValuesToDate : _a, onChange = props.onChange, _b = props.dataTypeDefinition, dataTypeDefinition = _b === void 0 ? { type: mdm_sdk_1.DataTypes.TYPE_DATE } : _b, DateRangePickerProps = props.DateRangePickerProps, MenuProps = props.MenuProps, componentProps = __rest(props, ["values", "options", "getValuesDateRange", "onChange", "dataTypeDefinition", "DateRangePickerProps", "MenuProps"]);
        var rangeOptions = (0, react_1.useMemo)(function () {
            return options ||
                Object.values(mdm_sdk_1.AllDateRangeValues).map(function (option) { return ({
                    value: option,
                    label: (0, mdm_sdk_1.getRangeOptionLabel)(option)
                }); });
        }, [options]);
        var inputRef = (0, react_1.useRef)(null);
        var _c = (0, react_1.useState)(false), isPickingDate = _c[0], setIsPickingDate = _c[1];
        var _d = (0, react_1.useState)(false), open = _d[0], setOpen = _d[1];
        var styles = (0, styles_1.useStyles)();
        var onItemClick = function (value) {
            if (value === mdm_sdk_1.AllDateRangeValues.PICK_DATE) {
                setIsPickingDate(true);
            }
            else {
                setOpen(false);
                setIsPickingDate(false);
                onChange(value);
            }
        };
        var handleToggle = (0, react_1.useCallback)(function () { return setOpen(function (open) { return !open; }); }, []);
        var onCancel = function () {
            setIsPickingDate(false);
            setOpen(false);
        };
        var isTimestamp = dataTypeDefinition.type === mdm_sdk_1.DataTypes.TYPE_TIMESTAMP;
        var enhancePlaceHolder = isTimestamp ? function (timeFormat) { return timeFormat.replace(/^[hH]:/, 'HH:'); } : ramda_1.identity;
        var format = isTimestamp ? (0, mdm_sdk_1.getLocaleFormat)() : dateFormat;
        var editorPlaceholder = "".concat(enhancePlaceHolder(format), " \u2014 ").concat(enhancePlaceHolder(format)).toUpperCase();
        var editorLabel = isPickingDate
            ? (0, mdm_sdk_1.getRangeOptionLabel)(mdm_sdk_1.AllDateRangeValues.PICK_DATE)
            : (0, helpers_1.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_1.default.createElement(react_1.default.Fragment, null,
            react_1.default.createElement(Component, __assign({}, componentProps, { ref: inputRef, editorLabel: editorLabel, editorPlaceholder: editorPlaceholder, onMenuToggle: handleToggle, isMenuOpen: open, isPickingDate: isPickingDate })),
            isPickingDate ? (react_1.default.createElement(Popover_1.default, __assign({ open: open, anchorEl: inputRef.current, onClose: onCancel, transformOrigin: transformOrigin, anchorOrigin: anchorOrigin, elevation: 2 }, MenuProps),
                react_1.default.createElement(DateRangePicker_1.DateRangePicker, __assign({ values: getValuesDateRange(values), onApply: onItemClick, type: dataTypeDefinition.type }, DateRangePickerProps)))) : (react_1.default.createElement(Menu_1.default, __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: (0, ramda_1.prop)('clientWidth', inputRef.current)
                    }
                } }, MenuProps), rangeOptions.map(function (option) { return (react_1.default.createElement(MenuItem_1.default, { key: option.value, value: option.value, onClick: function () { return onItemClick(option.value); }, className: styles.menuItem },
                react_1.default.createElement(Typography_1.default, { classes: { root: styles.menuText } }, option.label))); })))));
    };
    WithDateRangeSelector.displayName = 'WithDateRangeSelector';
    return WithDateRangeSelector;
};
exports.withDateRangeSelector = withDateRangeSelector;
