import React, { useRef, useState, useEffect, useCallback, memo } from 'react';
import i18n from 'ui-i18n';
import Popover from '@mui/material/Popover';
import Button from '@mui/material/Button';
import FilterIcon from '@mui/icons-material/FilterList';
import Typography from '@mui/material/Typography';
import Divider from '@mui/material/Divider';
import classNames from 'classnames';
import { buildFilterQueryString, rejectEmptyFilters } from '@reltio/mdm-sdk';
import { AttributesFiltersBuilder } from '../AttributesFiltersBuilder';
import { ActionButtons } from './components/ActionButtons/';
import { useStyles } from './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 ? FilterIcon : _c, popoverAnchorRef = _a.popoverAnchorRef, title = _a.title, anchorOrigin = _a.anchorOrigin;
    var styles = useStyles();
    var buttonRef = useRef();
    var _d = useState(false), isOpen = _d[0], setIsOpen = _d[1];
    var _e = useState([]), filters = _e[0], setFilters = _e[1];
    var isFiltersApplied = isFiltersAppliedProp !== null && isFiltersAppliedProp !== void 0 ? isFiltersAppliedProp : !!buildFilterQueryString()(filtersProp);
    var buttonColor = isFiltersApplied ? 'primary' : 'inherit';
    var togglePopup = function () {
        if (onTogglePopup) {
            onTogglePopup(!isOpen);
        }
        setIsOpen(function (open) { return !open; });
    };
    useEffect(function () {
        if (isOpen) {
            setFilters(filtersProp);
        }
    }, [filtersProp, isOpen]);
    var applyButtonHandler = useCallback(function () {
        var processedFilters = 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 = useCallback(function () {
        onCancel === null || onCancel === void 0 ? void 0 : onCancel();
        togglePopup();
    }, [onCancel]);
    var clearAllButtonHandler = useCallback(function () {
        onClear === null || onClear === void 0 ? void 0 : onClear();
        setFilters([]);
    }, [onClear]);
    return (React.createElement(React.Fragment, null,
        FilterButton ? (React.createElement(FilterButton, { onClick: togglePopup, ref: buttonRef, dataReltioId: dataReltioId })) : (React.createElement(Button, { "data-reltio-id": dataReltioId, ref: buttonRef, color: buttonColor, classes: {
                root: classNames(styles.triggerButton, classes === null || classes === void 0 ? void 0 : classes.triggerButton),
                textPrimary: styles.changedFiltersIndicator
            }, onClick: togglePopup },
            React.createElement(ButtonIcon, { color: buttonColor }))),
        React.createElement(Popover, { open: isOpen, classes: { paper: classNames(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.createElement(React.Fragment, null,
                React.createElement(Typography, { variant: "h6", className: styles.title }, title !== null && title !== void 0 ? title : i18n.text('Filter')),
                React.createElement("div", { className: styles.filtersBuilderWrapper },
                    children,
                    React.createElement(AttributesFiltersBuilder, { attributesTypesGroups: attributesTypesGroups, attributesTypesGroupFilter: attributesTypesGroupFilter, filters: filters, onChange: setFilters, components: components, onFilterAttributeChange: onFilterAttributeChange, classes: classes })),
                React.createElement(Divider, { className: styles.divider }),
                React.createElement(ActionButtons, { onApply: applyButtonHandler, onCancel: cancelButtonHandler, onClear: clearAllButtonHandler })))));
};
export default memo(AttributesFiltersButton);
