import { VegaSlimmer } from 'vega-slimmer/core';
import ChangeManager from '../../change-manager/change-manager';
import { Observer } from '../../change-manager/observer/observer';
import { VegaInternalSubStatePull, VegaInternalSubStatePush, } from '../../event-manager/event-id/vega-internal-event-id';
import DomNodeSubjectFactory from '../../change-manager/subject/dom-node-subject/dom-node-subject-factory';
/**
 * Sub state notify slimmer
 */
export class SubStateNotifySlimmerBase extends VegaSlimmer {
  // eslint-disable-next-line jsdoc/require-jsdoc
  constructor({ propName }) {
    super();
    /**
     * Checks if the input `host` matches the current `host` and `stateName` matches the current `propName`.
     *
     * @param {unknown} input - The `input` parameter is of type `unknown`, which means it can be any type.
     * @returns {boolean} a boolean value indicating whether the input is an acceptable table observer.
     */
    this.isAcceptableObserverPayload = (input) => {
      return input.host === this.host && input.stateName === this.propName;
    };
    this.propName = propName;
  }
  /**
   * Register the observer in connectedCallback
   */
  connectedCallback() {
    this.subStateObserver = new Observer(this.isAcceptableObserverPayload.bind(this), (payload) => {
      payload.callback(this.propValue);
    });
    ChangeManager.register(DomNodeSubjectFactory.getSubject(this.host, VegaInternalSubStatePull), this.subStateObserver);
  }
  /**
   * unregister the observer in disconnectedCallback
   */
  disconnectedCallback() {
    ChangeManager.unregister(DomNodeSubjectFactory.getSubject(this.host, VegaInternalSubStatePull), this.subStateObserver);
  }
  /**
   * Notify the children component which prop has been changed in the parent component
   */
  subStateNotify() {
    ChangeManager.notify(DomNodeSubjectFactory.getSubject(this.host, VegaInternalSubStatePush), {
      host: this.host,
      stateName: this.propName,
      value: this.propValue,
    });
  }
}
