var __decorate = (this && this.__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;
};
import { MapToComponentField, MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
import changeManager from '../change-manager/change-manager';
import { Observer } from '../change-manager/observer/observer';
import domNodeSubjectFactory from '../change-manager/subject/dom-node-subject/dom-node-subject-factory';
import remoteInvocationRegistry from '../remote-invocation/remote-invocation-registry';
import { findParent } from '../../utils/ui';
import { VegaInternalVegaTableRenderInputChange, } from '../event-manager/event-id/vega-internal-event-id';
/**
 * Shared render-input consumer APIs for vega-table child components.
 *
 * This slimmer centralizes remote invocation and change-manager wiring
 * for table render-input retrieval/updates.
 *
 * The slimmer automatically registers and removes the change observer during
 * connected/disconnected lifecycle hooks via {@link MapToComponentMethod}.
 */
export class VegaTableChildRenderInputConsumerSlimmer extends VegaSlimmer {
  constructor(subject) {
    super();
    this.observer = null;
    this.table = null;
    this.observerSubject = subject;
  }
  /**
   * Fetch and update render input before each render cycle.
   */
  componentWillRenderInput() {
    this.renderInput = this.fetchRenderInput();
  }
  /**
   * Register render-input change observer when the component connects.
   */
  connectRenderInputObserver() {
    this.table = findParent(this.host, 'vega-table');
    if (!this.table)
      return;
    this.registerRenderInputChangeObserver();
  }
  /**
   * Unregister render-input change observer when the component disconnects.
   */
  disconnectRenderInputObserver() {
    if (!this.table || !this.observer)
      return;
    this.unregisterRenderInputChangeObserver();
    this.observer = null;
    this.table = null;
  }
  /**
   * Fetch render input for a table child node through remote invocation.
   *
   * @template TRenderInput
   * @returns {Nullable<TRenderInput>} The render input associated with the host, or null when table is not found.
   */
  fetchRenderInput() {
    if (!this.table)
      return null;
    return remoteInvocationRegistry.call(domNodeSubjectFactory.getSubject(this.table, this.observerSubject), this.host);
  }
  /**
   * Create and register a render-input change observer on the given table.
   */
  registerRenderInputChangeObserver() {
    this.observer = new Observer((_input) => true, () => {
      this.renderInput = this.fetchRenderInput();
    });
    changeManager.register(domNodeSubjectFactory.getSubject(this.table, VegaInternalVegaTableRenderInputChange), this.observer);
  }
  /**
   * Remove a previously registered render-input change observer from the given table.
   */
  unregisterRenderInputChangeObserver() {
    changeManager.unregister(domNodeSubjectFactory.getSubject(this.table, VegaInternalVegaTableRenderInputChange), this.observer);
  }
}
__decorate([
  MapToComponentField()
], VegaTableChildRenderInputConsumerSlimmer.prototype, "host", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaTableChildRenderInputConsumerSlimmer.prototype, "renderInput", void 0);
__decorate([
  MapToComponentMethod('componentWillRender')
], VegaTableChildRenderInputConsumerSlimmer.prototype, "componentWillRenderInput", null);
__decorate([
  MapToComponentMethod('connectedCallback')
], VegaTableChildRenderInputConsumerSlimmer.prototype, "connectRenderInputObserver", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], VegaTableChildRenderInputConsumerSlimmer.prototype, "disconnectRenderInputObserver", null);
