"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get filterSeparators () {
        return filterSeparators;
    },
    get hasSubMenu () {
        return hasSubMenu;
    },
    get isDisabled () {
        return isDisabled;
    },
    get isItemCheckable () {
        return isItemCheckable;
    },
    get isItemDisabled () {
        return isItemDisabled;
    },
    get isItemHidden () {
        return isItemHidden;
    },
    get isItemSelectionDisabled () {
        return isItemSelectionDisabled;
    },
    get isItemSeparator () {
        return isItemSeparator;
    },
    get isItemSubMenu () {
        return isItemSubMenu;
    },
    get isSelectionDisabled () {
        return isSelectionDisabled;
    },
    get isSeparator () {
        return isSeparator;
    },
    get normalizeSelection () {
        return normalizeSelection;
    }
});
const _array = require("../../../helpers/array");
const _object = require("../../../helpers/object");
const _element = require("../../../helpers/dom/element");
const _predefinedItems = require("./../predefinedItems");
function normalizeSelection(selRanges) {
    return (0, _array.arrayMap)(selRanges, (range)=>({
            start: range.getTopStartCorner(),
            end: range.getBottomEndCorner()
        }));
}
function isItemSubMenu(itemToTest) {
    return (0, _object.hasOwnProperty)(itemToTest, 'submenu');
}
function isItemSeparator(itemToTest) {
    return new RegExp(_predefinedItems.SEPARATOR, 'i').test(itemToTest.name);
}
function isItemDisabled(itemToTest, hot) {
    return itemToTest.disabled === true || typeof itemToTest.disabled === 'function' && itemToTest.disabled.call(hot) === true;
}
function isItemSelectionDisabled(itemToTest) {
    return (0, _object.hasOwnProperty)(itemToTest, 'disableSelection');
}
function isSeparator(cell) {
    return (0, _element.hasClass)(cell, 'htSeparator');
}
function hasSubMenu(cell) {
    return (0, _element.hasClass)(cell, 'htSubmenu');
}
function isDisabled(cell) {
    return (0, _element.hasClass)(cell, 'htDisabled');
}
function isSelectionDisabled(cell) {
    return (0, _element.hasClass)(cell, 'htSelectionDisabled');
}
function isItemHidden(item, instance) {
    return !item.hidden || !(typeof item.hidden === 'function' && item.hidden.call(instance));
}
/**
 * @param {object[]} items The context menu items collection.
 * @param {string} separator The string which identifies the context menu separator item.
 * @returns {object[]}
 */ function shiftSeparators(items, separator) {
    const result = items.slice(0);
    for(let i = 0; i < result.length;){
        if (result[i].name === separator) {
            result.shift();
        } else {
            break;
        }
    }
    return result;
}
/**
 * @param {object[]} items The context menu items collection.
 * @param {string} separator The string which identifies the context menu separator item.
 * @returns {object[]}
 */ function popSeparators(items, separator) {
    let result = items.slice(0);
    result.reverse();
    result = shiftSeparators(result, separator);
    result.reverse();
    return result;
}
/**
 * Removes duplicated menu separators from the context menu items collection.
 *
 * @param {object[]} items The context menu items collection.
 * @returns {object[]}
 */ function removeDuplicatedSeparators(items) {
    const result = [];
    (0, _array.arrayEach)(items, (value, index)=>{
        if (index > 0) {
            if (result[result.length - 1].name !== value.name) {
                result.push(value);
            }
        } else {
            result.push(value);
        }
    });
    return result;
}
function filterSeparators(items, separator = _predefinedItems.SEPARATOR) {
    let result = items.slice(0);
    result = shiftSeparators(result, separator);
    result = popSeparators(result, separator);
    result = removeDuplicatedSeparators(result);
    return result;
}
function isItemCheckable(itemToTest) {
    return itemToTest.checkable === true;
}
