import changeManager from '../../change-manager';
import { Observer } from '../../observer/observer';
import domNodesSubjectFactory from './dom-node-subject-factory';
/**
 * The function `domAcceptableHandle` always returns `true`.
 *
 * @returns {boolean} true
 */
const domAcceptableHandle = () => {
  return true;
};
/** The `DomNodeSubjectObserverFactory` class provides methods to add and remove observers to HTML element nodes. */
export class DomNodeSubjectObserverFactory {
  /**
   * add observer to a given HTML element node
   *
   * @param {HTMLElement} node the node ref
   * @param {VegaEventIDInterface} eventId The `eventId` parameter is of type `VegaEventIDInterface`. It represents the ID of a Vega event.
   * @param {(input?: unknown) => void} callback the callback handle
   * @param {(input?: unknown) => boolean} acceptableHandle the acceptable handle
   */
  static addUniqueObserverToNode(node, eventId, callback, acceptableHandle) {
    if (node) {
      const domNodesObserver = new Observer(typeof acceptableHandle === 'function' ? acceptableHandle.bind(null) : domAcceptableHandle, callback);
      DomNodeSubjectObserverFactory.removeUniqueObserverFromNode(node, eventId);
      changeManager.register(domNodesSubjectFactory.getSubject(node, eventId), domNodesObserver);
    }
  }
  /**
   * The function removes observers from a given HTML element node.
   *
   * @param {HTMLElement} node - The `node` parameter is an `HTMLElement` object representing an HTML element in the DOM (Document Object Model).
   * @param {VegaEventIDInterface} eventId The `eventId` parameter is of type `VegaEventIDInterface`. It represents the ID of a Vega event.
   */
  static removeUniqueObserverFromNode(node, eventId) {
    if (domNodesSubjectFactory.isExist(node, eventId)) {
      changeManager.clearObserversBySubject(domNodesSubjectFactory.getSubject(node, eventId));
    }
  }
}
