"use strict";
// This component resolves the folowing bug: https://issues.redhat.com/browse/MGMT-18567
// Once this issues is addressed in PatternFly, this component should be removed.
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomToolbarFilter = void 0;
const tslib_1 = require("tslib");
const react_core_1 = require("@patternfly/react-core");
const React = tslib_1.__importStar(require("react"));
const ReactDOM = tslib_1.__importStar(require("react-dom"));
class CustomToolbarFilter extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            isMounted: false,
        };
    }
    componentDidMount() {
        const { categoryName, labels } = this.props;
        this.context.updateNumberFilters(typeof categoryName !== 'string' && categoryName.hasOwnProperty('key')
            ? categoryName.key
            : categoryName.toString(), labels ? labels.length : 0);
        this.setState({ isMounted: true });
    }
    componentDidUpdate() {
        const { categoryName, labels } = this.props;
        this.context.updateNumberFilters(typeof categoryName !== 'string' && categoryName.hasOwnProperty('key')
            ? categoryName.key
            : categoryName.toString(), labels ? labels.length : 0);
    }
    render() {
        var _a;
        const _b = this.props, { children, labels, deleteLabelGroup, deleteLabel, labelGroupExpandedText, labelGroupCollapsedText, categoryName, showToolbarItem, isExpanded, expandableLabelContainerRef } = _b, props = tslib_1.__rest(_b, ["children", "labels", "deleteLabelGroup", "deleteLabel", "labelGroupExpandedText", "labelGroupCollapsedText", "categoryName", "showToolbarItem", "isExpanded", "expandableLabelContainerRef"]);
        const { isExpanded: managedIsExpanded, labelGroupContentRef } = this.context;
        const _isExpanded = isExpanded !== undefined ? isExpanded : managedIsExpanded;
        const categoryKey = typeof categoryName !== 'string' && categoryName.hasOwnProperty('key')
            ? categoryName.key
            : categoryName.toString();
        const labelGroup = labels !== undefined && labels.length ? (React.createElement(react_core_1.ToolbarItem, { variant: "label-group" },
            React.createElement(react_core_1.LabelGroup, { key: categoryKey, categoryName: typeof categoryName === 'string' ? categoryName : categoryName.name, isClosable: deleteLabelGroup !== undefined, onClick: () => deleteLabelGroup && deleteLabelGroup(categoryName), collapsedText: labelGroupCollapsedText, expandedText: labelGroupExpandedText }, labels.map((label) => typeof label === 'string' ? (React.createElement(react_core_1.Label, { variant: "outline", key: label, onClose: () => deleteLabel && deleteLabel(categoryKey, label) }, label)) : (React.createElement(react_core_1.Label, { variant: "outline", key: label.key, onClose: () => deleteLabel && deleteLabel(categoryKey, label) }, label.node)))))) : null;
        if (!_isExpanded && this.state.isMounted) {
            return (React.createElement(React.Fragment, null,
                showToolbarItem && React.createElement(react_core_1.ToolbarItem, Object.assign({}, props), children),
                ((_a = labelGroupContentRef === null || labelGroupContentRef === void 0 ? void 0 : labelGroupContentRef.current) === null || _a === void 0 ? void 0 : _a.firstElementChild)
                    ? ReactDOM.createPortal(labelGroup, labelGroupContentRef.current.firstElementChild)
                    : null));
        }
        return (React.createElement(react_core_1.ToolbarContentContext.Consumer, null, ({ labelContainerRef }) => (React.createElement(React.Fragment, null,
            showToolbarItem && React.createElement(react_core_1.ToolbarItem, Object.assign({}, props), children),
            labelContainerRef.current &&
                ReactDOM.createPortal(labelGroup, labelContainerRef.current),
            expandableLabelContainerRef &&
                expandableLabelContainerRef.current &&
                ReactDOM.createPortal(labelGroup, expandableLabelContainerRef.current)))));
    }
}
exports.CustomToolbarFilter = CustomToolbarFilter;
CustomToolbarFilter.displayName = 'CustomToolbarFilter';
CustomToolbarFilter.contextType = react_core_1.ToolbarContext;
CustomToolbarFilter.defaultProps = {
    labels: [],
    showToolbarItem: true,
};
//# sourceMappingURL=CustomToolbarFilter.js.map