"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.ColumnsSettings = void 0;
var react_1 = __importStar(require("react"));
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var ramda_1 = require("ramda");
var Table_1 = __importDefault(require("../icons/Table"));
var SmallIconButton_1 = require("../SmallIconButton");
var ColumnsSettingsPopup_1 = require("./components/ColumnsSettingsPopup");
var helpers_1 = require("./helpers");
var ColumnsSettings = function (_a) {
    var columnsData = _a.columnsData, selectedColumns = _a.selectedColumns, onChangeColumns = _a.onChangeColumns, title = _a.title, tooltipTitle = _a.tooltipTitle, inputPlaceholder = _a.inputPlaceholder, disabled = _a.disabled;
    var _b = (0, react_1.useState)(null), anchorEl = _b[0], setAnchorEl = _b[1];
    var openPopup = function (event) { return setAnchorEl(event.currentTarget); };
    var closePopup = function () { return setAnchorEl(null); };
    var handleColumnClick = (0, react_1.useCallback)(function (column) {
        var changedColumns = (0, ramda_1.pipe)(helpers_1.getChildColumns, (0, ramda_1.pluck)('id'))(column);
        var wasSelected = (0, helpers_1.isColumnSelected)(selectedColumns, column);
        onChangeColumns(wasSelected
            ? (0, ramda_1.difference)(selectedColumns, changedColumns)
            : (0, ramda_1.uniq)((0, ramda_1.concat)(selectedColumns, changedColumns)));
    }, [selectedColumns, onChangeColumns]);
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(SmallIconButton_1.SmallIconButtonWithTooltip, { "data-reltio-id": "reltio-columns-settings-button", tooltipTitle: tooltipTitle || ui_i18n_1.default.text('Show/hide columns'), size: "L", icon: Table_1.default, onClick: openPopup, disabled: disabled }),
        react_1.default.createElement(ColumnsSettingsPopup_1.ColumnsSettingsPopup, { onColumnClick: handleColumnClick, anchorEl: anchorEl, columnsData: columnsData, onClose: closePopup, title: title, inputPlaceholder: inputPlaceholder, selectedColumns: selectedColumns })));
};
exports.ColumnsSettings = ColumnsSettings;
