"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 });
var react_1 = __importStar(require("react"));
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var Popover_1 = __importDefault(require("@mui/material/Popover"));
var Button_1 = __importDefault(require("@mui/material/Button"));
var FilterList_1 = __importDefault(require("@mui/icons-material/FilterList"));
var Typography_1 = __importDefault(require("@mui/material/Typography"));
var Divider_1 = __importDefault(require("@mui/material/Divider"));
var classnames_1 = __importDefault(require("classnames"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var AttributesFiltersBuilder_1 = require("../AttributesFiltersBuilder");
var ActionButtons_1 = require("./components/ActionButtons/");
var styles_1 = require("./styles");
var AttributesFiltersButton = function (_a) {
    var _b;
    var filtersProp = _a.filters, classes = _a.classes, attributesTypesGroups = _a.attributesTypesGroups, attributesTypesGroupFilter = _a.attributesTypesGroupFilter, onChange = _a.onChange, components = _a.components, children = _a.children, onTogglePopup = _a.onTogglePopup, isFiltersAppliedProp = _a.isFiltersApplied, dataReltioId = _a.dataReltioId, onFilterAttributeChange = _a.onFilterAttributeChange, onCancel = _a.onCancel, onClear = _a.onClear, onApply = _a.onApply, FilterButton = _a.FilterButton, _c = _a.ButtonIcon, ButtonIcon = _c === void 0 ? FilterList_1.default : _c, popoverAnchorRef = _a.popoverAnchorRef, title = _a.title, anchorOrigin = _a.anchorOrigin;
    var styles = (0, styles_1.useStyles)();
    var buttonRef = (0, react_1.useRef)();
    var _d = (0, react_1.useState)(false), isOpen = _d[0], setIsOpen = _d[1];
    var _e = (0, react_1.useState)([]), filters = _e[0], setFilters = _e[1];
    var isFiltersApplied = isFiltersAppliedProp !== null && isFiltersAppliedProp !== void 0 ? isFiltersAppliedProp : !!(0, mdm_sdk_1.buildFilterQueryString)()(filtersProp);
    var buttonColor = isFiltersApplied ? 'primary' : 'inherit';
    var togglePopup = function () {
        if (onTogglePopup) {
            onTogglePopup(!isOpen);
        }
        setIsOpen(function (open) { return !open; });
    };
    (0, react_1.useEffect)(function () {
        if (isOpen) {
            setFilters(filtersProp);
        }
    }, [filtersProp, isOpen]);
    var applyButtonHandler = (0, react_1.useCallback)(function () {
        var processedFilters = (0, mdm_sdk_1.rejectEmptyFilters)(filters);
        onChange(processedFilters);
        onApply === null || onApply === void 0 ? void 0 : onApply();
        togglePopup();
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [filters, onChange, onApply]);
    var cancelButtonHandler = (0, react_1.useCallback)(function () {
        onCancel === null || onCancel === void 0 ? void 0 : onCancel();
        togglePopup();
    }, [onCancel]);
    var clearAllButtonHandler = (0, react_1.useCallback)(function () {
        onClear === null || onClear === void 0 ? void 0 : onClear();
        setFilters([]);
    }, [onClear]);
    return (react_1.default.createElement(react_1.default.Fragment, null,
        FilterButton ? (react_1.default.createElement(FilterButton, { onClick: togglePopup, ref: buttonRef, dataReltioId: dataReltioId })) : (react_1.default.createElement(Button_1.default, { "data-reltio-id": dataReltioId, ref: buttonRef, color: buttonColor, classes: {
                root: (0, classnames_1.default)(styles.triggerButton, classes === null || classes === void 0 ? void 0 : classes.triggerButton),
                textPrimary: styles.changedFiltersIndicator
            }, onClick: togglePopup },
            react_1.default.createElement(ButtonIcon, { color: buttonColor }))),
        react_1.default.createElement(Popover_1.default, { open: isOpen, classes: { paper: (0, classnames_1.default)(styles.popupContent, classes === null || classes === void 0 ? void 0 : classes.filterPopup) }, anchorEl: (_b = popoverAnchorRef === null || popoverAnchorRef === void 0 ? void 0 : popoverAnchorRef.current) !== null && _b !== void 0 ? _b : buttonRef.current, onClose: togglePopup, anchorOrigin: anchorOrigin !== null && anchorOrigin !== void 0 ? anchorOrigin : {
                vertical: 'bottom',
                horizontal: (popoverAnchorRef === null || popoverAnchorRef === void 0 ? void 0 : popoverAnchorRef.current) ? 'left' : 'right'
            }, transformOrigin: {
                vertical: 'top',
                horizontal: 'left'
            } },
            react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement(Typography_1.default, { variant: "h6", className: styles.title }, title !== null && title !== void 0 ? title : ui_i18n_1.default.text('Filter')),
                react_1.default.createElement("div", { className: styles.filtersBuilderWrapper },
                    children,
                    react_1.default.createElement(AttributesFiltersBuilder_1.AttributesFiltersBuilder, { attributesTypesGroups: attributesTypesGroups, attributesTypesGroupFilter: attributesTypesGroupFilter, filters: filters, onChange: setFilters, components: components, onFilterAttributeChange: onFilterAttributeChange, classes: classes })),
                react_1.default.createElement(Divider_1.default, { className: styles.divider }),
                react_1.default.createElement(ActionButtons_1.ActionButtons, { onApply: applyButtonHandler, onCancel: cancelButtonHandler, onClear: clearAllButtonHandler })))));
};
exports.default = (0, react_1.memo)(AttributesFiltersButton);
