"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, /**
 * @param {HiddenColumns} hiddenColumnsPlugin The plugin instance.
 * @returns {object}
 */ "default", {
    enumerable: true,
    get: function() {
        return showColumnItem;
    }
});
const _array = require("../../../helpers/array");
const _constants = /*#__PURE__*/ _interop_require_wildcard(require("../../../i18n/constants"));
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
function showColumnItem(hiddenColumnsPlugin) {
    const columns = [];
    return {
        key: 'hidden_columns_show',
        name () {
            const pluralForm = columns.length > 1 ? 1 : 0;
            return this.getTranslatedPhrase(_constants.CONTEXTMENU_ITEMS_SHOW_COLUMN, pluralForm);
        },
        callback () {
            if (columns.length === 0) {
                return;
            }
            let startVisualColumn = columns[0];
            let endVisualColumn = columns[columns.length - 1];
            // Add to the selection one more visual column on the left.
            startVisualColumn = this.columnIndexMapper.getNearestNotHiddenIndex(startVisualColumn - 1, -1) ?? 0;
            // Add to the selection one more visual column on the right.
            endVisualColumn = this.columnIndexMapper.getNearestNotHiddenIndex(endVisualColumn + 1, 1) ?? this.countCols() - 1;
            hiddenColumnsPlugin.showColumns(columns);
            // We render columns at first. It was needed for getting fixed columns.
            // Please take a look at #6864 for broader description.
            this.view.adjustElementsSize();
            this.render();
            const allColumnsSelected = endVisualColumn - startVisualColumn + 1 === this.countCols();
            // When all headers needs to be selected then do nothing. The header selection is
            // automatically handled by corner click.
            if (!allColumnsSelected) {
                this.selectColumns(startVisualColumn, endVisualColumn);
            }
        },
        disabled: false,
        hidden () {
            const hiddenPhysicalColumns = (0, _array.arrayMap)(hiddenColumnsPlugin.getHiddenColumns(), (visualColumnIndex)=>{
                return this.toPhysicalColumn(visualColumnIndex);
            });
            if (!(this.selection.isSelectedByColumnHeader() || this.selection.isSelectedByCorner()) || hiddenPhysicalColumns.length < 1) {
                return true;
            }
            columns.length = 0;
            const selectedRangeActive = this.getSelectedRangeActive();
            const visualStartColumn = selectedRangeActive.getTopStartCorner().col;
            const visualEndColumn = selectedRangeActive.getBottomEndCorner().col;
            const columnIndexMapper = this.columnIndexMapper;
            const renderableStartColumn = columnIndexMapper.getRenderableFromVisualIndex(visualStartColumn);
            const renderableEndColumn = columnIndexMapper.getRenderableFromVisualIndex(visualEndColumn);
            const notTrimmedColumnIndexes = columnIndexMapper.getNotTrimmedIndexes();
            const physicalColumnIndexes = [];
            if (visualStartColumn !== visualEndColumn) {
                const visualColumnsInRange = visualEndColumn - visualStartColumn + 1;
                const renderedColumnsInRange = renderableEndColumn - renderableStartColumn + 1;
                // Collect not trimmed columns if there are some hidden columns in the selection range.
                if (visualColumnsInRange > renderedColumnsInRange) {
                    const physicalIndexesInRange = notTrimmedColumnIndexes.slice(visualStartColumn, visualEndColumn + 1);
                    physicalColumnIndexes.push(...physicalIndexesInRange.filter((physicalIndex)=>hiddenPhysicalColumns.includes(physicalIndex)));
                }
            // Handled column is the first rendered index and there are some visual indexes before it.
            } else if (renderableStartColumn === 0 && renderableStartColumn < visualStartColumn) {
                // not trimmed indexes -> array of mappings from visual (native array's index) to physical indexes (value).
                physicalColumnIndexes.push(...notTrimmedColumnIndexes.slice(0, visualStartColumn)); // physical indexes
            // When all columns are hidden and the context menu is triggered using top-left corner.
            } else if (renderableStartColumn === null) {
                // Show all hidden columns.
                physicalColumnIndexes.push(...notTrimmedColumnIndexes.slice(0, this.countCols()));
            } else {
                const lastVisualIndex = this.countCols() - 1;
                const lastRenderableIndex = columnIndexMapper.getRenderableFromVisualIndex(columnIndexMapper.getNearestNotHiddenIndex(lastVisualIndex, -1));
                // Handled column is the last rendered index and there are some visual indexes after it.
                if (renderableEndColumn === lastRenderableIndex && lastVisualIndex > visualEndColumn) {
                    physicalColumnIndexes.push(...notTrimmedColumnIndexes.slice(visualEndColumn + 1));
                }
            }
            (0, _array.arrayEach)(physicalColumnIndexes, (physicalColumnIndex)=>{
                columns.push(this.toVisualColumn(physicalColumnIndex));
            });
            return columns.length === 0;
        }
    };
}
