"use strict";
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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ColumnsSettingsPopup = void 0;
var react_1 = __importStar(require("react"));
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var VirtualGroupedList_1 = require("../../../VirtualGroupedList");
var SelectionPopup_1 = require("../../../SelectionPopup");
var helpers_1 = require("../../helpers");
var useKeyboardNavigation_1 = require("../../../hooks/useKeyboardNavigation");
var ColumnListItem_1 = require("../ColumnListItem");
var ColumnsSettingsPopup_module_css_1 = __importDefault(require("./ColumnsSettingsPopup.module.css"));
var ITEM_HEIGHT = 32;
var MAX_LIST_CONTAINER_HEIGHT = 300;
var DEFAULT_MIN_CONTAINER_WIDTH = 308;
var CONTAINER_HEADER_HEIGHT = 108;
var MIN_CONTAINER_HEIGHT = 140;
var ColumnsSettingsPopup = function (_a) {
    var anchorEl = _a.anchorEl, onClose = _a.onClose, title = _a.title, inputPlaceholder = _a.inputPlaceholder, columnsData = _a.columnsData, onColumnClick = _a.onColumnClick, selectedColumns = _a.selectedColumns, hideCheckBox = _a.hideCheckBox;
    var _b = (0, react_1.useState)(''), filterText = _b[0], setFilterText = _b[1];
    var clearFilter = function () { return setFilterText(''); };
    var groupedItems = (0, react_1.useMemo)(function () { return (0, helpers_1.buildColumnItems)(columnsData, filterText); }, [columnsData, filterText]);
    var items = (0, react_1.useMemo)(function () { return (0, VirtualGroupedList_1.flattenGroupedItemsData)(groupedItems); }, [groupedItems]);
    var containerWidth = (0, mdm_sdk_1.getMaxItemWidth)('label')(groupedItems) || DEFAULT_MIN_CONTAINER_WIDTH;
    var handleSearchOnEnter = (0, react_1.useCallback)(function (_a) {
        var item = _a.item;
        return onColumnClick(item);
    }, [onColumnClick]);
    var _c = (0, useKeyboardNavigation_1.useKeyboardNavigation)({
        items: items,
        open: true,
        onSelectFocusedItem: handleSearchOnEnter,
        selectedItems: selectedColumns,
        onClose: onClose
    }), focusIndex = _c.focusIndex, handleKeyDown = _c.handleKeyDown;
    var handleClose = (0, react_1.useCallback)((0, ramda_1.pipe)(onClose, clearFilter), [onClose]);
    var renderColumnItem = function (style, _a, index) {
        var item = _a.item, _b = _a.level, level = _b === void 0 ? 0 : _b;
        return (react_1.default.createElement(ColumnListItem_1.ColumnListItem, { hideCheckBox: hideCheckBox, style: style, item: item, level: level, isFocused: focusIndex === index, checked: (0, helpers_1.isColumnSelected)(selectedColumns, item), isGroupItem: (0, helpers_1.isGroupColumn)(item), onClick: hideCheckBox ? (0, ramda_1.pipe)(onColumnClick, handleClose) : onColumnClick }));
    };
    var listHeight = Math.min(MAX_LIST_CONTAINER_HEIGHT, ITEM_HEIGHT * items.length);
    var containerHeight = Math.max(MIN_CONTAINER_HEIGHT, listHeight + CONTAINER_HEADER_HEIGHT);
    return (react_1.default.createElement(SelectionPopup_1.SelectionPopup, { className: ColumnsSettingsPopup_module_css_1.default.popup, open: !!anchorEl, anchorEl: anchorEl, onClose: handleClose, onSearch: setFilterText, inputPlaceholder: inputPlaceholder, title: title || ui_i18n_1.default.text('Show/hide columns'), containerWidth: containerWidth, containerHeight: containerHeight, searchInputOnKeyDown: handleKeyDown },
        react_1.default.createElement(VirtualGroupedList_1.VirtualGroupedList, { fixedTitle: false, height: listHeight, renderItem: renderColumnItem, items: items, getItemSize: function () { return ITEM_HEIGHT; }, focusIndex: focusIndex })));
};
exports.ColumnsSettingsPopup = ColumnsSettingsPopup;
