import { c as MapToComponentField, M as MapToComponentMethod, V as VegaSlimmer } from './global-slimmer-registry-17c4efd4.js';
import { O as Observer } from './observer-3959f9dd.js';
import { C as ChangeManager } from './change-manager-6a7eb88c.js';
import { b as isParent } from './ui-64815a12.js';

var __decorate = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
/** The `ChildNodesNotifyObserverSlimmer` class is a TypeScript class that acts as a slimmed-down version of a notify observer for child nodes, allowing for add and remove of observers and handling of observer payloads. */
class ChildNodesNotifyObserverSlimmer extends VegaSlimmer {
  /**
   * The constructor function initializes the subject and notifyObserverEntry properties of the class.
   *
   * @param {Subject} subject - The `subject` parameter is an instance of the `Subject` class. It represents the subject that the observer will be observing.
   * @param {NotifyObserverCallback} callback - It is typically called by the subject when it undergoes a change and needs to notify its observers.
   * @param {CustomAcceptableType} [customAcceptableHandle] - It is typically called by the subject when it undergoes a change and needs to notify its observers.
   */
  constructor(subject, callback, customAcceptableHandle) {
    super();
    this.acceptableResultMap = new WeakMap();
    /**
     * It is used to determine whether a given observer payload is acceptable for the observer.
     *
     * @param {NotifyObserverPayload<unknown>} input input payload
     * @returns {boolean} true or false
     */
    this.isAcceptableObserverPayload = (input) => {
      let result = false;
      if (typeof this.customAcceptableHandle === 'function') {
        result = this.customAcceptableHandle(input);
      }
      else if (this.host && input.host) {
        if (this.acceptableResultMap.has(input.host)) {
          result = !!this.acceptableResultMap.get(input.host);
        }
        else {
          result = this.host === input.host ? false : isParent(input.host, this.host);
          this.acceptableResultMap.set(input.host, result);
        }
      }
      return result;
    };
    /**
     * trigger the callback function
     *
     * @param {NotifyObserverPayload<unknown>} input input payload
     */
    this.handleObserver = (input) => {
      if (typeof this.callback === 'function') {
        this.callback(input);
      }
    };
    this.subject = subject;
    this.callback = callback;
    this.customAcceptableHandle = customAcceptableHandle;
  }
  /**
   * Initialize the notify observer.
   */
  connectedCallback() {
    if (this.subject) {
      this.notifyObserver = new Observer(this.isAcceptableObserverPayload.bind(this), this.handleObserver);
      ChangeManager.register(this.subject, this.notifyObserver);
    }
  }
  /**
   * Unregister the observer when disconnectedCallback
   */
  disconnectedCallback() {
    if (this.subject) {
      ChangeManager.unregister(this.subject, this.notifyObserver);
    }
  }
}
__decorate([
  MapToComponentField()
], ChildNodesNotifyObserverSlimmer.prototype, "host", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], ChildNodesNotifyObserverSlimmer.prototype, "connectedCallback", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], ChildNodesNotifyObserverSlimmer.prototype, "disconnectedCallback", null);

export { ChildNodesNotifyObserverSlimmer as C };
