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 domNodeSubjectFactory from '../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-factory';
import { VegaInternalVegaTableRenderInputChange, VegaInternalColumnBodyCellRenderInputRIC, VegaInternalColumnHeadCellRenderInputRIC, VegaInternalColumnHeadRowRenderInputRIC, } from '../../../../helpers/event-manager/event-id/vega-internal-event-id';
import remoteInvocationRegistry from '../../../../helpers/remote-invocation/remote-invocation-registry';
import changeManager from '../../../../helpers/change-manager/change-manager';
import { SynchronizeTaskQueue } from '../../../../helpers/queue/synchronize-task-queue';
import { ColumnRenderInputModel } from './dto/column-render-input-model';
/**
 * VegaTableRenderInputController
 *
 * Central controller that computes and provides render input data for table header rows,
 * header cells and body cells. Responsibilities:
 * - Build and cache shared header layout context (depth, width, pinned indices).
 * - Generate per-head-row and per-body-row render inputs used by head/row renderers.
 * - Expose render inputs to shadow DOM cells via Remote Invocation Call (RIC) registry.
 */
export class VegaTableRenderInputController extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.headRowRenderInputMap = new Map();
    this.headCellRenderInputMap = new Map();
    this.pinnedColumnBodyCellRenderInputMap = new Map();
    this.cellRenderInputChangeTaskQueue = new SynchronizeTaskQueue();
  }
  /**
   * Register remote invocation observers when controller is connected.
   * Called via component lifecycle mapping.
   */
  connectedCallback() {
    this.registerRenderInputRICObserver();
  }
  /**
   * Unregister remote invocation observers when controller is disconnected.
   * Called via component lifecycle mapping.
   */
  disconnectedCallback() {
    this.unRegisterRenderInputRICObserver();
  }
  /**
   * Initialize head context before component load.
   * Called via component lifecycle mapping.
   */
  componentWillLoad() {
    this.setRenderInputModel();
  }
  /**
   * Notify body cells about render input changes after the table has re-rendered.
   * This ensures ref callbacks have fired and the pinnedColumnBodyCellRenderInputMap is up-to-date.
   * Called via component lifecycle mapping.
   */
  async componentDidRender() {
    await this.cellRenderInputChangeTaskQueue.runTask();
  }
  /**
   * Recompute head context when the column structure model changes.
   * Called via watch mapping.
   */
  async watchColumnStructureModel() {
    this.setRenderInputModel();
    this.pinnedColumnBodyCellRenderInputMap.clear();
    await this.cellRenderInputChangeTaskQueue.enqueue({
      /**
       * Notify body-cell render-input changes after render completes.
       */
      run: async () => {
        changeManager.notify(domNodeSubjectFactory.getSubject(this.host, VegaInternalVegaTableRenderInputChange), {});
      },
    }, false);
  }
  /**
   * Return render input models for table body (flattened leaves).
   *
   * @returns {ColumnRenderInputModel[]} Flattened array of leaf render input models.
   */
  getLeafRenderInputs() {
    return this.leavesRenderInputModels;
  }
  /**
   * Return render input models for table head (root-level forest).
   *
   * @returns {ColumnRenderInputModel[]} Root-level column render input models.
   */
  getHeadRenderInputs() {
    return this.columnRenderInputModels;
  }
  /**
   * Store head row render input mapping for remote invocation.
   *
   * @param {HTMLElement} headRow - The head row element.
   * @param {ColumnRenderInputModel[]} renderInputs - Associated render input models.
   */
  addHeadRowRenderInputMap(headRow, renderInputs) {
    this.headRowRenderInputMap.set(headRow, renderInputs);
  }
  /**
   * Store head cell render input mapping for remote invocation.
   *
   * @param {HTMLElement} headCell - The head cell element.
   * @param {ColumnRenderInputModel} renderInput - Associated render input model.
   */
  addHeadCellRenderInputMap(headCell, renderInput) {
    this.headCellRenderInputMap.set(headCell, renderInput);
  }
  /**
   * Store body cell render input mapping for pinned cells (used to update sticky positions).
   *
   * Only stores cells that require sticky positioning (left/right or no-pinned-end).
   *
   * @param {HTMLElement} cell - The body cell element.
   * @param {ColumnRenderInputModel} renderInput - Associated render input model.
   */
  addCellRenderInputMap(cell, renderInput) {
    if ((cell && ['left', 'right'].includes(renderInput.pinned)) || renderInput.isNoPinnedEnd) {
      this.pinnedColumnBodyCellRenderInputMap.set(cell, renderInput);
    }
  }
  /**
   * Expose the internal head-cell -> render-input mapping.
   *
   * @typedef {import('../../../../types/components').HTMLVegaTableHeadCellElement} HTMLVegaTableHeadCellElement
   * @returns {Map<HTMLVegaTableHeadCellElement, ColumnRenderInputModel>} Map of head cell elements to render input.
   */
  getHeadCellRenderInputMap() {
    return this.headCellRenderInputMap;
  }
  /**
   * Expose the internal map of pinned body cells to their render inputs.
   *
   * @typedef {import('../../../../types/components').HTMLVegaTableCellElement} HTMLVegaTableCellElement
   * @returns {Map<HTMLVegaTableCellElement, ColumnRenderInputModel>} Map of body cell hosts requiring sticky positioning.
   */
  getPinnedColumnBodyCellRenderInputMap() {
    return this.pinnedColumnBodyCellRenderInputMap;
  }
  /**
   * Register remote invocation callbacks to allow DOM nodes to request render inputs.
   */
  registerRenderInputRICObserver() {
    remoteInvocationRegistry.register(domNodeSubjectFactory.getSubject(this.host, VegaInternalColumnHeadRowRenderInputRIC), (headRow) => {
      return this.headRowRenderInputMap.get(headRow);
    });
    remoteInvocationRegistry.register(domNodeSubjectFactory.getSubject(this.host, VegaInternalColumnHeadCellRenderInputRIC), (headCell) => {
      return this.headCellRenderInputMap.get(headCell);
    });
    remoteInvocationRegistry.register(domNodeSubjectFactory.getSubject(this.host, VegaInternalColumnBodyCellRenderInputRIC), (cell) => {
      return this.pinnedColumnBodyCellRenderInputMap.get(cell);
    });
  }
  /**
   * Unregister remote invocation callbacks previously registered.
   */
  unRegisterRenderInputRICObserver() {
    remoteInvocationRegistry.unRegister(domNodeSubjectFactory.getSubject(this.host, VegaInternalColumnHeadRowRenderInputRIC));
    remoteInvocationRegistry.unRegister(domNodeSubjectFactory.getSubject(this.host, VegaInternalColumnHeadCellRenderInputRIC));
    remoteInvocationRegistry.unRegister(domNodeSubjectFactory.getSubject(this.host, VegaInternalColumnBodyCellRenderInputRIC));
  }
  /**
   * Build the ColumnRenderInputModel forest from the current ColumnStructureModel.
   */
  setRenderInputModel() {
    this.columnRenderInputModels = ColumnRenderInputModel.from(this.columnStructureModel);
    this.leavesRenderInputModels = this.columnRenderInputModels.flatMap((model) => model.getLeaves());
  }
}
__decorate([
  MapToComponentField()
], VegaTableRenderInputController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaTableRenderInputController.prototype, "columnStructureModel", void 0);
__decorate([
  MapToComponentField()
], VegaTableRenderInputController.prototype, "vegaTableColGroupRenderer", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], VegaTableRenderInputController.prototype, "connectedCallback", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], VegaTableRenderInputController.prototype, "disconnectedCallback", null);
__decorate([
  MapToComponentMethod('componentWillLoad')
], VegaTableRenderInputController.prototype, "componentWillLoad", null);
__decorate([
  MapToComponentMethod('componentDidRender')
], VegaTableRenderInputController.prototype, "componentDidRender", null);
__decorate([
  MapToComponentMethod('watchColumnStructureModel')
], VegaTableRenderInputController.prototype, "watchColumnStructureModel", null);
