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;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
import React, { useCallback, useMemo, useRef } from 'react';
import i18n from 'ui-i18n';
import classnames from 'classnames';
import { chain, curry, map } from 'ramda';
import { getMaxItemWidth } from '@reltio/mdm-sdk';
import { Highlighter } from '../Highlighter';
import { SelectionPopup } from '../SelectionPopup';
import { AttributeListItem } from '../AttributeListItem';
import { VirtualGroupedList, flattenGroupedItemsData } from '../VirtualGroupedList';
import { useKeyboardNavigation } from '../hooks/useKeyboardNavigation';
import { useStyles } from './styles';
var ITEM_HEIGHT = 32;
var LIST_CONTAINER_HEIGHT = 300;
var DEFAULT_MIN_CONTAINER_WIDTH = 308;
export var ConfigureColumnsPopup = function (_a) {
    var open = _a.open, anchorEl = _a.anchorEl, _b = _a.data, data = _b === void 0 ? [] : _b, onSearch = _a.onSearch, onClose = _a.onClose, _c = _a.titleText, titleText = _c === void 0 ? i18n.text('Show/hide columns') : _c, _d = _a.filterText, filterText = _d === void 0 ? '' : _d, onListItemClick = _a.onListItemClick, _e = _a.selected, selected = _e === void 0 ? [] : _e, hideCheckBox = _a.hideCheckBox, renderGroupTitle = _a.renderGroupTitle, otherProps = __rest(_a, ["open", "anchorEl", "data", "onSearch", "onClose", "titleText", "filterText", "onListItemClick", "selected", "hideCheckBox", "renderGroupTitle"]);
    var filterItem = useCallback(function (item) {
        return item.label.toLowerCase().includes(filterText.toLowerCase());
    }, [filterText]);
    var filterItemsTree = useCallback(function (item) { return filterItem(item) || (item.children || []).some(filterItemsTree); }, [filterItem]);
    var getItemSize = function () { return ITEM_HEIGHT; };
    var getAttributesData = useMemo(function () {
        var getAttributeGroupData = curry(function (level, item, groupId, groupName) { return __spreadArray([
            {
                item: __assign(__assign({}, item), { filterText: filterText }),
                level: level,
                groupId: groupId,
                groupName: groupName
            }
        ], getAttributesData(item.children || [], groupId, groupName, level + 1), true); });
        var getAttributesData = function (data, groupId, groupName, level) {
            if (level === void 0) { level = 0; }
            var getItemGroupData = function (item) { return getAttributeGroupData(level, item, groupId, groupName); };
            var filteredData = data.filter(filterItemsTree);
            return (renderGroupTitle ? map : chain)(getItemGroupData, filteredData);
        };
        return getAttributesData;
    }, [filterText, renderGroupTitle, filterItemsTree]);
    var getGroupsData = useCallback(function (groups) {
        return groups
            .map(function (group) { return ({
            item: group,
            items: getAttributesData(group.attributes, group.id, group.label)
        }); })
            .filter(function (group) { return group.items.length; });
    }, [getAttributesData]);
    var handleSearchOnEnter = useCallback(function (_a) {
        var item = _a.item;
        onListItemClick(item, !selected.find(function (column) { return column.id === item.id || column.id.includes("".concat(item.id, ".")); }));
    }, [onListItemClick, selected]);
    var groupedItems = useMemo(function () { return (renderGroupTitle ? getGroupsData : getAttributesData)(data); }, [data, renderGroupTitle, getGroupsData, getAttributesData]);
    var items = useMemo(function () { return flattenGroupedItemsData(groupedItems); }, [groupedItems]);
    var _f = useKeyboardNavigation({
        items: items,
        open: open,
        onSelectFocusedItem: handleSearchOnEnter,
        selectedItems: selected
    }), focusIndex = _f.focusIndex, handleKeyDown = _f.handleKeyDown;
    var focusIndexRef = useRef(null);
    focusIndexRef.current = focusIndex;
    var renderListItem = useCallback(function (style, _a, index) {
        var item = _a.item, _b = _a.level, level = _b === void 0 ? 0 : _b;
        var checked = !!selected.find(function (column) { return column.id === item.id || column.id.includes("".concat(item.id, ".")); });
        var isFocused = focusIndexRef.current === index;
        return (React.createElement(AttributeListItem, { key: item.id, onClick: onListItemClick, checked: checked, level: level, data: item, label: React.createElement(Highlighter, { text: item.label, highlight: item.filterText }), labelInText: item.label, style: style, hideCheckBox: hideCheckBox, isFocused: isFocused, notSelectable: item.notSelectable, LogoIcon: item.LogoIcon }));
    }, [hideCheckBox, onListItemClick, selected]);
    var containerWidth = getMaxItemWidth('label')(groupedItems) || DEFAULT_MIN_CONTAINER_WIDTH;
    var classes = useStyles();
    return (React.createElement(SelectionPopup, __assign({ open: open, className: classnames(classes.container, classes.popupContainer), anchorEl: anchorEl, onClose: onClose, onSearch: onSearch, title: titleText, containerWidth: containerWidth, searchInputOnKeyDown: handleKeyDown }, otherProps),
        React.createElement(VirtualGroupedList, { height: LIST_CONTAINER_HEIGHT, renderItem: renderListItem, items: items, hideSubtitles: true, getItemSize: getItemSize, focusIndex: focusIndex, renderGroupTitle: renderGroupTitle })));
};
