"use strict";
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.ActivityFilterEditor = void 0;
var react_1 = __importStar(require("react"));
var ramda_1 = require("ramda");
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var Button_1 = __importDefault(require("@mui/material/Button"));
var Popover_1 = __importDefault(require("@mui/material/Popover"));
var Divider_1 = __importDefault(require("@mui/material/Divider"));
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 types_1 = require("../types");
var helpers_1 = require("./helpers");
var ActivitySelector_1 = require("./components/ActivitySelector");
var UserSelector_1 = require("../../../UserSelector");
var DateRangeSelector_1 = require("./components/DateRangeSelector");
var EntityChangedSelectors_1 = require("./components/EntityChangedSelectors");
var useDidUpdateEffect_1 = require("../../../hooks/useDidUpdateEffect");
var MdmModuleContext_1 = require("../../../contexts/MdmModuleContext");
var styles_1 = require("./styles");
var ActivityFilterEditor = function (_a) {
    var value = _a.value, open = _a.open, entityType = _a.entityType, anchorEl = _a.anchorEl, onApply = _a.onApply, onCancel = _a.onCancel, onClear = _a.onClear;
    var styles = (0, styles_1.useStyles)();
    var _b = (0, react_1.useState)(value), currentFilter = _b[0], setCurrentFilter = _b[1];
    var metadata = (0, MdmModuleContext_1.useMdmMetadata)();
    var clearCurrentFilter = function () { return setCurrentFilter(null); };
    var handlePropChange = (0, ramda_1.curry)(function (propName, value) {
        setCurrentFilter((0, ramda_1.pipe)((0, ramda_1.when)((0, ramda_1.always)(propName === 'entityTypes'), (0, ramda_1.assoc)('attributes', [])), (0, ramda_1.when)((0, ramda_1.always)(propName === 'activities'), (0, ramda_1.mergeLeft)({ attributes: [], entityTypes: [], sources: [] })), (0, ramda_1.assoc)(propName, value)));
    });
    (0, useDidUpdateEffect_1.useDidUpdateEffect)(function () {
        setCurrentFilter(value);
    }, [open, value]);
    var _c = currentFilter || {}, _d = _c.activities, activities = _d === void 0 ? [] : _d, _e = _c.dateRange, dateRange = _e === void 0 ? null : _e, _f = _c.users, users = _f === void 0 ? [] : _f;
    var isEntityChangedActivity = activities.length === 1 && activities.includes(types_1.ActivityTypes.ENTITY_CHANGED);
    return (react_1.default.createElement(Popover_1.default, { open: open, anchorEl: anchorEl, onClose: onCancel, classes: { paper: styles.container }, 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.editorFields },
            react_1.default.createElement(ActivitySelector_1.ActivitySelector, { activityTypes: (0, helpers_1.getSelectorActivityTypes)(metadata, entityType), selectedActivityTypes: activities, onChange: handlePropChange('activities') }),
            isEntityChangedActivity && (react_1.default.createElement(EntityChangedSelectors_1.EntityChangedSelectors, { filter: currentFilter, entityType: entityType, onPropChange: handlePropChange })),
            react_1.default.createElement(Divider_1.default, { light: true, className: styles.divider }),
            react_1.default.createElement(DateRangeSelector_1.DateRangeSelector, { value: dateRange, onChange: handlePropChange('dateRange') }),
            react_1.default.createElement(Divider_1.default, { light: true, className: styles.divider }),
            react_1.default.createElement(UserSelector_1.UserSelector, { selectedUsers: users, onChange: handlePropChange('users'), disableUnderline: true })),
        react_1.default.createElement(DialogActions_1.default, null,
            react_1.default.createElement(Button_1.default, { onClick: (0, ramda_1.pipe)(clearCurrentFilter, onClear), className: styles.clearButton }, ui_i18n_1.default.text('Clear all')),
            react_1.default.createElement(Button_1.default, { onClick: onCancel }, ui_i18n_1.default.text('Cancel')),
            react_1.default.createElement(Button_1.default, { color: "primary", onClick: function () { return onApply(currentFilter); } }, ui_i18n_1.default.text('Apply')))));
};
exports.ActivityFilterEditor = ActivityFilterEditor;
