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 __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import React, { useCallback, useRef } from 'react';
import ListItem from '@mui/material/ListItem';
import { isSelectedFacet } from '../../helpers';
import { AttributeListItem } from '../../../AttributeListItem';
import { VirtualGroupedList } from '../../../VirtualGroupedList';
import { Highlighter } from '../../../Highlighter';
import { AttributeGroupIcon } from '../../../AttributeGroupIcon';
import { FACET_LIST_ITEM_HEIGHT } from '../../constants';
import { useMdmMetadata } from '../../../contexts/MdmModuleContext';
import { ExpandedValueTooltip } from '../../../ExpandedValueTooltip';
import styles from './FacetsList.module.css';
var ITEM_GROUP_TITLE_HEIGHT = 40;
export var FacetsList = function (_a) {
    var onListItemClick = _a.onListItemClick, focusIndex = _a.focusIndex, selectedFacetsFieldNames = _a.selectedFacetsFieldNames, otherProps = __rest(_a, ["onListItemClick", "focusIndex", "selectedFacetsFieldNames"]);
    var focusIndexRef = useRef(null);
    focusIndexRef.current = focusIndex;
    var metadata = useMdmMetadata();
    var renderItem = useCallback(function (style, _a, index) {
        var item = _a.item, level = _a.level, groupId = _a.groupId;
        var checked = isSelectedFacet({ facet: item, selectedFacetsFieldNames: selectedFacetsFieldNames });
        var isFocused = focusIndexRef.current === index;
        return (React.createElement(AttributeListItem, { key: "item-".concat(groupId, "-").concat(item.fieldName), onClick: onListItemClick, data: item, label: React.createElement(Highlighter, { text: item.title, highlight: item.filterText }), labelInText: item.title, level: level, style: style, checked: checked, isFocused: isFocused, groupId: groupId, LogoIcon: item.LogoIcon }));
    }, [onListItemClick, selectedFacetsFieldNames]);
    var renderGroupTitle = useCallback(function (style, item) { return (React.createElement(ListItem, { component: 'div', className: styles.subHeader, style: style, key: "group-".concat(item.item.id) },
        item.item.needGroupIcon && React.createElement(AttributeGroupIcon, { metadata: metadata, group: item.item }),
        React.createElement(ExpandedValueTooltip, { value: item.item.title },
            React.createElement("div", { className: styles.subHeaderTitle }, item.item.title)))); }, [metadata]);
    var getItemSize = useCallback(function (i, item) { return (item.items ? ITEM_GROUP_TITLE_HEIGHT : FACET_LIST_ITEM_HEIGHT); }, []);
    return (React.createElement(VirtualGroupedList, __assign({ getItemSize: getItemSize, renderGroupTitle: renderGroupTitle, renderItem: renderItem, focusIndex: focusIndex }, otherProps, { disableHorizontalScrollbar: true })));
};
