import { Observer } from '../change-manager/observer/observer';
import ChangeManager from '../change-manager/change-manager';
import domNodeSubjectFactory from '../change-manager/subject/dom-node-subject/dom-node-subject-factory';
import { VegaInternalComponentDidRender } from '../event-manager/event-id/vega-internal-event-id';
/**
 * Wait for the component has been rendered.
 *
 * @param {HTMLStencilElement} componentRef - The reference to the component that needs to be rendered.
 * @returns {Promise<unknown>} A promise that resolves when the component has been rendered.
 */
export const waitForComponentDidRender = async (componentRef) => {
  return new Promise((resolve) => {
    const observer = new Observer(isAcceptableComponentDidRender.bind(null), () => {
      ChangeManager.unregister(domNodeSubjectFactory.getSubject(componentRef, VegaInternalComponentDidRender), observer);
      resolve(null);
    });
    ChangeManager.register(domNodeSubjectFactory.getSubject(componentRef, VegaInternalComponentDidRender), observer);
  });
};
/**
 * Determines whether the observer should accept changes.
 *
 * @returns {boolean} Always returns true in the current implementation.
 */
const isAcceptableComponentDidRender = () => {
  return true;
};
