"use strict";
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 __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 __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));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigureColumnsPopup = void 0;
var react_1 = __importStar(require("react"));
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var classnames_1 = __importDefault(require("classnames"));
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var Highlighter_1 = require("../Highlighter");
var SelectionPopup_1 = require("../SelectionPopup");
var AttributeListItem_1 = require("../AttributeListItem");
var VirtualGroupedList_1 = require("../VirtualGroupedList");
var useKeyboardNavigation_1 = require("../hooks/useKeyboardNavigation");
var styles_1 = require("./styles");
var ITEM_HEIGHT = 32;
var LIST_CONTAINER_HEIGHT = 300;
var DEFAULT_MIN_CONTAINER_WIDTH = 318;
var EMPTY_ARRAY = [];
var ConfigureColumnsPopup = function (_a) {
    var open = _a.open, anchorEl = _a.anchorEl, _b = _a.data, data = _b === void 0 ? EMPTY_ARRAY : _b, onSearch = _a.onSearch, onClose = _a.onClose, _c = _a.titleText, titleText = _c === void 0 ? ui_i18n_1.default.text('Show/hide columns') : _c, _d = _a.filterText, filterText = _d === void 0 ? '' : _d, onListItemClick = _a.onListItemClick, _e = _a.selected, selected = _e === void 0 ? EMPTY_ARRAY : _e, _f = _a.hideCheckBox, hideCheckBox = _f === void 0 ? false : _f, renderGroupTitle = _a.renderGroupTitle, otherProps = __rest(_a, ["open", "anchorEl", "data", "onSearch", "onClose", "titleText", "filterText", "onListItemClick", "selected", "hideCheckBox", "renderGroupTitle"]);
    var filterItem = (0, react_1.useCallback)(function (item) {
        return item.label.toLowerCase().includes(filterText.toLowerCase());
    }, [filterText]);
    var filterItemsTree = (0, react_1.useCallback)(function (item) { return filterItem(item) || (item.children || []).some(filterItemsTree); }, [filterItem]);
    var getItemSize = function () { return ITEM_HEIGHT; };
    var getAttributesData = (0, react_1.useMemo)(function () {
        var getAttributeGroupData = (0, ramda_1.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 ? ramda_1.map : ramda_1.chain)(getItemGroupData, filteredData);
        };
        return getAttributesData;
    }, [filterText, renderGroupTitle, filterItemsTree]);
    var getGroupsData = (0, react_1.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 = (0, react_1.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 = (0, react_1.useMemo)(function () { return (renderGroupTitle ? getGroupsData : getAttributesData)(data); }, [data, renderGroupTitle, getGroupsData, getAttributesData]);
    var items = (0, react_1.useMemo)(function () { return (0, VirtualGroupedList_1.flattenGroupedItemsData)(groupedItems); }, [groupedItems]);
    var _g = (0, useKeyboardNavigation_1.useKeyboardNavigation)({
        items: items,
        open: open,
        onSelectFocusedItem: handleSearchOnEnter,
        selectedItems: selected
    }), focusIndex = _g.focusIndex, handleKeyDown = _g.handleKeyDown;
    var focusIndexRef = (0, react_1.useRef)(null);
    focusIndexRef.current = focusIndex;
    var renderListItem = (0, react_1.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_1.default.createElement(AttributeListItem_1.AttributeListItem, { key: item.id, onClick: onListItemClick, checked: checked, level: level, data: item, label: react_1.default.createElement(Highlighter_1.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 = Math.max((0, mdm_sdk_1.getMaxItemWidth)('label')(groupedItems), DEFAULT_MIN_CONTAINER_WIDTH);
    var classes = (0, styles_1.useStyles)();
    return (react_1.default.createElement(SelectionPopup_1.SelectionPopup, __assign({ open: open, className: (0, classnames_1.default)(classes.container, classes.popupContainer), anchorEl: anchorEl, onClose: onClose, onSearch: onSearch, title: titleText, containerWidth: containerWidth, searchInputOnKeyDown: handleKeyDown }, otherProps),
        react_1.default.createElement(VirtualGroupedList_1.VirtualGroupedList, { height: LIST_CONTAINER_HEIGHT, renderItem: renderListItem, items: items, hideSubtitles: true, getItemSize: getItemSize, focusIndex: focusIndex, renderGroupTitle: renderGroupTitle })));
};
exports.ConfigureColumnsPopup = ConfigureColumnsPopup;
