"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, /**
 * @param {*} plugin The plugin instance.
 * @returns {object}
 */ "default", {
    enumerable: true,
    get: function() {
        return toggleMergeItem;
    }
});
const _constants = /*#__PURE__*/ _interop_require_wildcard(require("../../../i18n/constants"));
const _cellCoords = /*#__PURE__*/ _interop_require_default(require("../cellCoords"));
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
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 toggleMergeItem(plugin) {
    return {
        key: 'mergeCells',
        name () {
            const selection = this.getSelectedActive();
            if (selection) {
                const info = plugin.mergedCellsCollection.get(selection[0], selection[1]);
                if (info.row === selection[0] && info.col === selection[1] && info.row + info.rowspan - 1 === selection[2] && info.col + info.colspan - 1 === selection[3]) {
                    return this.getTranslatedPhrase(_constants.CONTEXTMENU_ITEMS_UNMERGE_CELLS);
                }
            }
            return this.getTranslatedPhrase(_constants.CONTEXTMENU_ITEMS_MERGE_CELLS);
        },
        callback () {
            const activeRange = this.getSelectedRangeActive();
            if (!activeRange) {
                return;
            }
            activeRange.setDirection(this.isRtl() ? 'NE-SW' : 'NW-SE');
            const { from, to } = activeRange;
            plugin.toggleMerge(activeRange);
            this.selectCell(from.row, from.col, to.row, to.col, false);
        },
        disabled () {
            const selection = this.getSelectedActive();
            if (!selection) {
                return true;
            }
            const isSingleCell = _cellCoords.default.isSingleCell({
                row: selection[0],
                col: selection[1],
                rowspan: selection[2] - selection[0] + 1,
                colspan: selection[3] - selection[1] + 1
            });
            return isSingleCell || this.selection.isSelectedByCorner();
        },
        hidden: false
    };
}
