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);
};
import React, { useState, useRef } from 'react';
import classnames from 'classnames';
import i18n from 'ui-i18n';
import { always, equals, evolve, pipe, mergeRight, unless } from 'ramda';
import { isDateRangeValid } from '@reltio/mdm-sdk';
import FilterListIcon from '@mui/icons-material/FilterList';
import { ActivityFilterEditor } from '../ActivityFilterEditor';
import { SmallIconButtonWithTooltip } from '../../../SmallIconButton';
import { useStyles } from './styles';
export var ActivityFilterButton = function (_a) {
    var filter = _a.filter, entityType = _a.entityType, onChange = _a.onChange;
    var styles = useStyles();
    var buttonRef = useRef(null);
    var initialValue = useRef(filter);
    var _b = useState(false), isEditorOpen = _b[0], setIsEditorOpen = _b[1];
    var isApplied = !equals(filter, initialValue.current);
    var handleChange = function (value) {
        var newValue = pipe(mergeRight(initialValue.current), evolve({
            dateRange: unless(isDateRangeValid, always(initialValue.current.dateRange))
        }))(value);
        onChange(newValue);
        setIsEditorOpen(false);
    };
    return (React.createElement(React.Fragment, null,
        React.createElement(SmallIconButtonWithTooltip, { ref: buttonRef, icon: FilterListIcon, size: "L", onClick: function () { return setIsEditorOpen(true); }, tooltipTitle: i18n.text('Filter'), className: classnames(styles.icon, { activeIcon: isApplied }) }),
        React.createElement(ActivityFilterEditor, { value: filter, open: isEditorOpen, entityType: entityType, anchorEl: buttonRef.current, onApply: handleChange, onCancel: function () { return setIsEditorOpen(false); }, onClear: function () { return onChange(__assign({}, initialValue.current)); } })));
};
