"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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HistoryFilterButton = void 0;
var react_1 = __importStar(require("react"));
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var ramda_1 = require("ramda");
var FilterList_1 = __importDefault(require("@mui/icons-material/FilterList"));
var Button_1 = __importDefault(require("@mui/material/Button"));
var Popover_1 = __importDefault(require("@mui/material/Popover"));
var DialogActions_1 = __importDefault(require("@mui/material/DialogActions"));
var DialogContent_1 = __importDefault(require("@mui/material/DialogContent"));
var DialogTitle_1 = __importDefault(require("@mui/material/DialogTitle"));
var Divider_1 = __importDefault(require("@mui/material/Divider"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var SmallIconButton_1 = require("../../../SmallIconButton");
var UserSelector_1 = require("../../../UserSelector");
var AttributeSelector_1 = require("../../../AttributeSelector");
var DateRangeSelector_1 = require("../DateRangeSelector");
var HistoryActivitySelector_1 = require("../HistoryActivitySelector");
var filters_1 = require("../helpers/filters");
var constants_1 = require("../constants");
var styles_1 = require("./styles");
var HistoryFilterButton = function (_a) {
    var className = _a.className, filter = _a.filter, onApplyFilter = _a.onApplyFilter, entityType = _a.entityType;
    var styles = (0, styles_1.useStyles)();
    var buttonRef = (0, react_1.useRef)();
    var _b = (0, react_1.useState)(false), isOpen = _b[0], setIsOpen = _b[1];
    var _c = (0, react_1.useState)(filter), currentValue = _c[0], setCurrentValue = _c[1];
    var _d = currentValue || {}, _e = _d.activities, activities = _e === void 0 ? [] : _e, _f = _d.attributes, attributes = _f === void 0 ? [] : _f, _g = _d.dateRange, dateRange = _g === void 0 ? null : _g, _h = _d.users, users = _h === void 0 ? [] : _h;
    var buttonColor = (0, filters_1.isDefaultFilter)(filter) ? 'inherit' : 'primary';
    (0, react_1.useEffect)(function () {
        if (isOpen) {
            setCurrentValue(filter);
        }
    }, [filter, isOpen]);
    var handlePropChange = (0, ramda_1.curry)(function (propName, value) {
        setCurrentValue(function (filterValue) {
            var _a;
            return (__assign(__assign({}, filterValue), (_a = {}, _a[propName] = value, _a)));
        });
    });
    var togglePopup = (0, react_1.useCallback)(function () {
        setIsOpen(function (open) { return !open; });
    }, []);
    var applyButtonHandler = (0, react_1.useCallback)(function () {
        if (dateRange && (0, mdm_sdk_1.isDateRangeValid)(dateRange)) {
            onApplyFilter(currentValue);
        }
        else {
            onApplyFilter((0, ramda_1.dissoc)('dateRange', currentValue));
        }
        togglePopup();
    }, [currentValue, dateRange, onApplyFilter, togglePopup]);
    var clearAllButtonHandler = (0, react_1.useCallback)(function () {
        setCurrentValue(__assign({}, constants_1.DEFAULT_HISTORY_FILTER));
    }, []);
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(SmallIconButton_1.SmallIconButton, { ref: buttonRef, color: buttonColor, icon: FilterList_1.default, size: "L", onClick: togglePopup, className: className, "data-reltio-id": "history-filter-button" }),
        react_1.default.createElement(Popover_1.default, { classes: { paper: styles.popup }, open: isOpen, anchorEl: buttonRef.current, onClose: togglePopup, anchorOrigin: {
                vertical: 'bottom',
                horizontal: 'right'
            }, transformOrigin: {
                vertical: 'top',
                horizontal: 'right'
            } },
            react_1.default.createElement(DialogTitle_1.default, null, ui_i18n_1.default.text('Filter')),
            react_1.default.createElement(DialogContent_1.default, { dividers: true, className: styles.filterContainer, "data-reltio-id": "history-filter-container" },
                react_1.default.createElement(DateRangeSelector_1.DateRangeSelector, { value: dateRange, onChange: handlePropChange('dateRange'), className: styles.filterItem }),
                react_1.default.createElement(Divider_1.default, { light: true, className: styles.divider }),
                react_1.default.createElement(UserSelector_1.UserSelector, { selectedUsers: users, onChange: handlePropChange('users'), className: styles.filterItem, disableUnderline: true }),
                react_1.default.createElement(Divider_1.default, { light: true, className: styles.divider }),
                react_1.default.createElement(HistoryActivitySelector_1.HistoryActivitySelector, { selectedHistoryActivityTypes: activities, onChange: handlePropChange('activities'), className: styles.filterItem, disableUnderline: true }),
                react_1.default.createElement(Divider_1.default, { light: true, className: styles.divider }),
                react_1.default.createElement(AttributeSelector_1.AttributeSelector, { selectedAttributes: attributes, entityType: entityType, onChange: handlePropChange('attributes'), className: styles.filterItem, title: ui_i18n_1.default.text('Attribute updated'), disableUnderline: true })),
            react_1.default.createElement(DialogActions_1.default, null,
                react_1.default.createElement(Button_1.default, { onClick: clearAllButtonHandler, className: styles.clearButton }, ui_i18n_1.default.text('Clear all')),
                react_1.default.createElement(Button_1.default, { onClick: togglePopup }, ui_i18n_1.default.text('Cancel')),
                react_1.default.createElement(Button_1.default, { color: "primary", onClick: applyButtonHandler }, ui_i18n_1.default.text('Apply'))))));
};
exports.HistoryFilterButton = HistoryFilterButton;
