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, useState, useMemo, useRef } from 'react';
import i18n from 'ui-i18n';
import { ConfigColumnPopup, HIDE, INSERT_LEFT, INSERT_RIGHT } from '../../ConfigColumnPopup';
import { ColumnsSettingsPopup } from '../../ColumnsSettings';
import { getAvailableColumns } from './helpers';
export 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 = useState(null), anchorEl = _a[0], setAnchorEl = _a[1];
        var _b = useState(null), configIdx = _b[0], setConfigIdx = _b[1];
        var _c = useState(null), popupMode = _c[0], setPopupMode = _c[1];
        var placeForNewRef = useRef(null);
        var availablelColumns = useMemo(function () { return getAvailableColumns(columns, selectedColumns); }, [columns, selectedColumns]);
        var closeAll = useCallback(function () {
            setAnchorEl(null);
            setConfigIdx(null);
            setPopupMode(null);
            placeForNewRef.current = null;
        }, []);
        var openConfigColumnPopup = useCallback(function (index, e) {
            setAnchorEl(e.currentTarget);
            setConfigIdx(index);
            setPopupMode('menu');
        }, []);
        var handleMenuAction = useCallback(function (action) {
            if (configIdx === null)
                return;
            switch (action) {
                case INSERT_LEFT:
                    placeForNewRef.current = configIdx;
                    setPopupMode('add');
                    break;
                case INSERT_RIGHT:
                    placeForNewRef.current = configIdx + 1;
                    setPopupMode('add');
                    break;
                case 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 = 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.createElement(React.Fragment, null,
            React.createElement(Table, __assign({}, tableProps, { openConfigColumnPopup: openConfigColumnPopup })),
            React.createElement(ColumnsSettingsPopup, { anchorEl: popupMode === 'add' ? anchorEl : null, onClose: closeAll, title: i18n.text('Add column'), columnsData: availablelColumns, onColumnClick: handleAddColumn, selectedColumns: selectedColumns, hideCheckBox: true }),
            React.createElement(ConfigColumnPopup, { open: popupMode === 'menu', anchorEl: anchorEl, onClose: closeAll, onClick: handleMenuAction, actions: configIdx !== null && configIdx > 0 ? [INSERT_LEFT, INSERT_RIGHT, HIDE] : [INSERT_RIGHT] })));
    };
}
