import { findParent } from '../../utils/ui';
/**
 * This class is used to track the nested level of a component
 * by using the target element and tracking element selector
 *
 * @property {Map<HTMLElement,{string:number}>} map used to record the nesting level
 */
class ComponentNestedLevelTracker {
  constructor() {
    this.map = new Map();
  }
  /**
   * If the element matches the selector, then set the level of the element to the level of the parent element + 1
   *
   * @param {HTMLElement} el - HTMLElement
   * @param {string} selector - The selector of the card that is being nested.
   * @returns {Nullable<number>} The nested level of the element.
   */
  setNestedLevel(el, selector) {
    if (!el.matches(selector))
      return undefined;
    const parentSameSelectorNode = findParent(el, selector);
    let level = 1;
    if (parentSameSelectorNode) {
      const entry = this.map.get(parentSameSelectorNode);
      if (entry) {
        level = entry[selector] + 1;
      }
    }
    this.setLevelToMap(el, selector, level);
    return level;
  }
  /**
   * Given an element and a selector, return the nested level of that selector
   *
   * @param {HTMLElement} el - HTMLElement
   * @param {string} selector - The selector to look for.
   * @returns {Nullable<number>} The nullable number of nested levels of the element.
   */
  getNestedLevel(el, selector) {
    var _a;
    return (_a = this.map.get(el)) === null || _a === void 0 ? void 0 : _a[selector];
  }
  /**
   * Set the level of a selector for a given element
   *
   * @param {HTMLElement} el - HTMLElement
   * @param {string} selector - The selector for the element.
   * @param {number} level - The level of the element.
   */
  setLevelToMap(el, selector, level) {
    if (!this.map.has(el)) {
      this.map.set(el, {});
    }
    const entry = this.map.get(el);
    if (entry) {
      entry[selector] = level;
    }
  }
}
export default new ComponentNestedLevelTracker();
