var MenuNavigation = /** @class */ (function () {
    function MenuNavigation(root, itemsContentDataTid) {
        var _this = this;
        this.tagsAndItems = new WeakMap();
        this.highlightedItem = null;
        this.items = [];
        this.getEnabledItems = function () {
            return _this.items.filter(function (item) { return item.isEnabled(); });
        };
        this.root = root;
        this.itemsContentDataTid = itemsContentDataTid;
    }
    MenuNavigation.prototype.update = function () {
        var _this = this;
        var _a;
        if ((_a = this.root) === null || _a === void 0 ? void 0 : _a.current) {
            var menuItems = Array.from(this.root.current.querySelectorAll("span[data-tid=\"".concat(this.itemsContentDataTid, "\"]")));
            this.items = menuItems.map(function (item) { return _this.tagsAndItems.get(item); }).filter(function (item) { return item !== undefined; });
        }
    };
    MenuNavigation.prototype.add = function (tag, item) {
        var _a;
        this.tagsAndItems.set(tag, item);
        if ((_a = this.root) === null || _a === void 0 ? void 0 : _a.current) {
            this.update();
        }
        else {
            this.items.push(item);
        }
    };
    MenuNavigation.prototype.remove = function (tag) {
        this.tagsAndItems.delete(tag);
        this.update();
    };
    MenuNavigation.prototype.move = function (step, isCyclic) {
        var enabledItems = this.getEnabledItems();
        var currentIndex = this.highlightedItem ? enabledItems.indexOf(this.highlightedItem) : -1;
        var minIndex = 0;
        var maxIndex = enabledItems.length - 1;
        var nextIndex = currentIndex + step;
        if (nextIndex < minIndex) {
            nextIndex = isCyclic ? maxIndex : minIndex;
        }
        if (nextIndex > maxIndex) {
            nextIndex = isCyclic ? minIndex : maxIndex;
        }
        var nextItem = enabledItems[nextIndex];
        this.highlight(nextItem);
        return nextIndex;
    };
    MenuNavigation.prototype.highlight = function (item) {
        var _this = this;
        var enabledItems = this.getEnabledItems();
        enabledItems.forEach(function (_item) {
            if (item === _item) {
                _item.highlight();
                _this.highlightedItem = item;
            }
            else {
                _item.unhighlight();
            }
        });
    };
    MenuNavigation.prototype.highlightByIndex = function (index) {
        var _this = this;
        this.items.forEach(function (_item, i) {
            if (index === i && _item.isEnabled()) {
                _item.highlight();
                _this.highlightedItem = _item;
            }
            else {
                _item.unhighlight();
            }
        });
    };
    MenuNavigation.prototype.unhighlight = function () {
        var _a;
        (_a = this.highlightedItem) === null || _a === void 0 ? void 0 : _a.unhighlight();
        this.highlightedItem = null;
    };
    MenuNavigation.prototype.select = function (event) {
        if (this.highlightedItem) {
            this.highlightedItem.select(event);
            return true;
        }
        return false;
    };
    MenuNavigation.prototype.reset = function () {
        this.highlight(null);
        this.highlightedItem = null;
    };
    return MenuNavigation;
}());
export { MenuNavigation };
//# sourceMappingURL=MenuNavigation.js.map