"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.withColumnConfigurator = withColumnConfigurator;
var react_1 = __importStar(require("react"));
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var ConfigColumnPopup_1 = require("../../ConfigColumnPopup");
var ColumnsSettings_1 = require("../../ColumnsSettings");
var helpers_1 = require("./helpers");
function withColumnConfigurator(Table) {
    return function ColumnConfigurator(props) {
        var columns = props.columns, onChangeColumns = props.onChangeColumns, selectedColumns = props.selectedColumns, tableProps = __rest(props, ["columns", "onChangeColumns", "selectedColumns"]);
        var _a = (0, react_1.useState)(null), anchorEl = _a[0], setAnchorEl = _a[1];
        var _b = (0, react_1.useState)(null), configIdx = _b[0], setConfigIdx = _b[1];
        var _c = (0, react_1.useState)(null), popupMode = _c[0], setPopupMode = _c[1];
        var placeForNewRef = (0, react_1.useRef)(null);
        var availablelColumns = (0, react_1.useMemo)(function () { return (0, helpers_1.getAvailableColumns)(columns, selectedColumns); }, [columns, selectedColumns]);
        var closeAll = (0, react_1.useCallback)(function () {
            setAnchorEl(null);
            setConfigIdx(null);
            setPopupMode(null);
            placeForNewRef.current = null;
        }, []);
        var openConfigColumnPopup = (0, react_1.useCallback)(function (index, e) {
            setAnchorEl(e.currentTarget);
            setConfigIdx(index);
            setPopupMode('menu');
        }, []);
        var handleMenuAction = (0, react_1.useCallback)(function (action) {
            if (configIdx === null)
                return;
            switch (action) {
                case ConfigColumnPopup_1.INSERT_LEFT:
                    placeForNewRef.current = configIdx;
                    setPopupMode('add');
                    break;
                case ConfigColumnPopup_1.INSERT_RIGHT:
                    placeForNewRef.current = configIdx + 1;
                    setPopupMode('add');
                    break;
                case ConfigColumnPopup_1.HIDE: {
                    var next = __spreadArray(__spreadArray([], selectedColumns.slice(0, configIdx), true), selectedColumns.slice(configIdx + 1), true);
                    onChangeColumns(next);
                    closeAll();
                    break;
                }
            }
        }, [selectedColumns, configIdx, onChangeColumns, closeAll]);
        var handleAddColumn = (0, react_1.useCallback)(function (_a) {
            var id = _a.id;
            if (placeForNewRef.current === null)
                return;
            var next = __spreadArray(__spreadArray(__spreadArray([], selectedColumns.slice(0, placeForNewRef.current), true), [
                id
            ], false), selectedColumns.slice(placeForNewRef.current), true);
            onChangeColumns(next);
            closeAll();
        }, [selectedColumns, onChangeColumns, closeAll]);
        return (react_1.default.createElement(react_1.default.Fragment, null,
            react_1.default.createElement(Table, __assign({}, tableProps, { openConfigColumnPopup: openConfigColumnPopup })),
            react_1.default.createElement(ColumnsSettings_1.ColumnsSettingsPopup, { anchorEl: popupMode === 'add' ? anchorEl : null, onClose: closeAll, title: ui_i18n_1.default.text('Add column'), columnsData: availablelColumns, onColumnClick: handleAddColumn, selectedColumns: selectedColumns, hideCheckBox: true }),
            react_1.default.createElement(ConfigColumnPopup_1.ConfigColumnPopup, { open: popupMode === 'menu', anchorEl: anchorEl, onClose: closeAll, onClick: handleMenuAction, actions: configIdx !== null && configIdx > 0 ? [ConfigColumnPopup_1.INSERT_LEFT, ConfigColumnPopup_1.INSERT_RIGHT, ConfigColumnPopup_1.HIDE] : [ConfigColumnPopup_1.INSERT_RIGHT] })));
    };
}
