"use strict";

exports.__esModule = true;
require("core-js/modules/es.error.cause.js");
var _utils = require("./utils");
function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); }
function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; }
function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; }
function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
function _classApplyDescriptorSet(receiver, descriptor, value) { if (descriptor.set) { descriptor.set.call(receiver, value); } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } descriptor.value = value; } }
/**
 * Helper class that supports navigating throughout the menu and submenus.
 *
 * @private
 * @class Navigator
 */
var _hotMenu = /*#__PURE__*/new WeakMap();
var _current = /*#__PURE__*/new WeakMap();
var _prev = /*#__PURE__*/new WeakMap();
var _visited = /*#__PURE__*/new WeakMap();
var _updateMenuSelection = /*#__PURE__*/new WeakSet();
class Navigator {
  constructor() {
    /**
     * Selects the menu based on the internal state of the class.
     */
    _classPrivateMethodInitSpec(this, _updateMenuSelection);
    /**
     * The instance of the Handsontable as menu.
     *
     * @type {Handsontable}
     */
    _classPrivateFieldInitSpec(this, _hotMenu, {
      writable: true,
      value: void 0
    });
    /**
     * The index of the current selected menu item.
     *
     * @type {number}
     */
    _classPrivateFieldInitSpec(this, _current, {
      writable: true,
      value: -1
    });
    /**
     * The index of the previously selected menu item.
     *
     * @type {number}
     */
    _classPrivateFieldInitSpec(this, _prev, {
      writable: true,
      value: -1
    });
    /**
     * The collection of the already visited menu item indexes. The collection allows for preventing
     * infinite loops in cases when all menu items are disabled, or there is only one item available
     * and the navigation is triggered.
     *
     * @type {Set<number>}
     */
    _classPrivateFieldInitSpec(this, _visited, {
      writable: true,
      value: new Set()
    });
  }
  /**
   * Sets the menu Handsontable instance.
   *
   * @param {Handsontable} hotMenu The menu Handsontable instance.
   */
  setMenu(hotMenu) {
    _classPrivateFieldSet(this, _hotMenu, hotMenu);
  }

  /**
   * Selects the first menu item in the menu.
   */
  selectFirst() {
    _classPrivateFieldGet(this, _visited).clear();
    _classPrivateFieldSet(this, _current, 0);
    _classPrivateFieldSet(this, _prev, _classPrivateFieldGet(this, _current) - 1);
    _classPrivateMethodGet(this, _updateMenuSelection, _updateMenuSelection2).call(this);
  }

  /**
   * Selects the last menu item in the menu.
   */
  selectLast() {
    _classPrivateFieldGet(this, _visited).clear();
    _classPrivateFieldSet(this, _current, _classPrivateFieldGet(this, _hotMenu).countRows() - 1);
    _classPrivateFieldSet(this, _prev, _classPrivateFieldGet(this, _current) + 1);
    _classPrivateMethodGet(this, _updateMenuSelection, _updateMenuSelection2).call(this);
  }

  /**
   * Selects the next menu item in the menu. The navigator tries to select the closes non-disabled menu
   * item. When all next items are disabled the first (or next non-disabled) menu item is selected.
   */
  selectNext() {
    _classPrivateFieldGet(this, _visited).clear();
    _classPrivateFieldSet(this, _current, _classPrivateFieldGet(this, _current) + 1);
    _classPrivateMethodGet(this, _updateMenuSelection, _updateMenuSelection2).call(this);
  }

  /**
   * Selects the previous menu item in the menu. The navigator tries to select the closes non-disabled menu
   * item. When all previous items are disabled the last (or previous non-disabled) menu item is selected.
   */
  selectPrev() {
    _classPrivateFieldGet(this, _visited).clear();
    _classPrivateFieldSet(this, _current, _classPrivateFieldGet(this, _current) - 1);
    _classPrivateMethodGet(this, _updateMenuSelection, _updateMenuSelection2).call(this);
  }

  /**
   * Clears the internal state to the initial values.
   */
  clear() {
    _classPrivateFieldGet(this, _visited).clear();
    _classPrivateFieldSet(this, _prev, -1);
    _classPrivateFieldSet(this, _current, -1);
  }
}
exports.Navigator = Navigator;
function _updateMenuSelection2() {
  const maxItems = _classPrivateFieldGet(this, _hotMenu).countRows() - 1;
  if (_classPrivateFieldGet(this, _current) < 0) {
    _classPrivateFieldSet(this, _current, maxItems);
    _classPrivateFieldSet(this, _prev, maxItems);
  }
  if (_classPrivateFieldGet(this, _current) > maxItems) {
    _classPrivateFieldSet(this, _current, 0);
    _classPrivateFieldSet(this, _prev, -1);
  }
  if (_classPrivateFieldGet(this, _visited).has(_classPrivateFieldGet(this, _current))) {
    return;
  }
  const cell = _classPrivateFieldGet(this, _hotMenu).getCell(_classPrivateFieldGet(this, _current), 0);
  _classPrivateFieldGet(this, _visited).add(_classPrivateFieldGet(this, _current));
  if (!cell || (0, _utils.isSeparator)(cell) || (0, _utils.isDisabled)(cell) || (0, _utils.isSelectionDisabled)(cell)) {
    _classPrivateFieldSet(this, _current, _classPrivateFieldGet(this, _current) + (_classPrivateFieldGet(this, _current) > _classPrivateFieldGet(this, _prev) ? 1 : -1));
    _classPrivateMethodGet(this, _updateMenuSelection, _updateMenuSelection2).call(this);
  } else {
    _classPrivateFieldGet(this, _hotMenu).selectCell(_classPrivateFieldGet(this, _current), 0);
  }
  _classPrivateFieldSet(this, _prev, _classPrivateFieldGet(this, _current));
}