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 { InjectVegaSlimmer, MapToComponentField, MapToComponentMethod, methodPlaceholder, VegaSlimmer, } from 'vega-slimmer/core';
import ChangeManager from '../../../../helpers/change-manager/change-manager';
import { TABLE_ROW_SELECTION_ALL } from '../../../../helpers/change-manager/subject/static-subject/static-subject-title';
import { MutationObserverSlimmer } from '../../../../helpers/slimmers/mutation-observer/mutation-observer-slimmer';
import { TagNameMutationFilter } from '../../../../helpers/slimmers/mutation-observer/mutation-observer-filters';
/**
 * The controller for the table children components.
 */
export class VegaTableChildrenController extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.currentDataObserver = new MutationObserverSlimmer({
      // eslint-disable-next-line jsdoc/require-jsdoc
      target: () => this.host,
      options: { childList: true, subtree: true },
      /*
       * The below method is e2e-test covered in
       * @see{module:should-render-striped-rows-and-not-show-empty-message-when-lazy-render-table-body-with-table-rows}
       */
      mutationFilter: TagNameMutationFilter('VEGA-TABLE-BODY', 'VEGA-TABLE-ROW'),
      callback: 
      /*
       * The below method is e2e-test covered in
       * @see{module:should-not-render-empty-message-when-async-render-table-rows}
       */
      /* istanbul ignore next */
      // eslint-disable-next-line jsdoc/require-jsdoc
      () => {
        this.syncUpTableCurrentData();
        this.syncUpTableHeadRowProps();
      },
    });
    this.rowStripedObserver = new MutationObserverSlimmer({
      // eslint-disable-next-line jsdoc/require-jsdoc
      target: () => this.host,
      options: { childList: true, subtree: true },
      /*
       * The below method is e2e-test covered in
       * @see{module:should-render-striped-rows-and-not-show-empty-message-when-lazy-render-table-body-with-table-rows}
       */
      mutationFilter: TagNameMutationFilter('VEGA-TABLE-BODY', 'VEGA-TABLE-ROW', 'VEGA-TABLE-EXPAND-ROW'),
      callback: 
      /*
       * The below method is e2e-test covered in
       * @see{module:should-render-striped-row-when-async-render-table-rows}
       * @see{module:should-render-striped-row-and-expanded-row-when-async-render-table-rows}
       */
      /* istanbul ignore next */
      // eslint-disable-next-line jsdoc/require-jsdoc
      () => {
        this.syncRowStates();
      },
    });
    /**
     * Syncs up the properties of vega-table-row
     */
    this.syncUpTableCurrentData = () => {
      if (!this.isTemplateRenderMode)
        return;
      const currentData = Array.from(this.host.querySelectorAll('vega-table-row')).map((row) => {
        return { key: row.rowKey };
      });
      this.vegaTableDataSourceController.setCurrentData(currentData);
    };
    /**
     * Sync up the properties of vega-table-expand-row
     */
    this.syncRowStates = () => {
      const rows = this.host.querySelectorAll('vega-table-row');
      rows.forEach((row, index) => {
        this.updateRowStriped(row, index);
        this.updateRowDivider(row, index === rows.length - 1);
      });
      const expandRows = this.host.querySelectorAll('vega-table-expand-row');
      expandRows.forEach((row, index) => {
        this.updateRowStriped(row, index);
      });
    };
    /**
     * Syncs up the properties of vega-table-head-row
     */
    this.syncUpTableHeadRowProps = () => {
      var _a;
      if (!this.isTemplateRenderMode)
        return;
      const headRow = this.host.querySelector('vega-table-head-row');
      if (headRow) {
        headRow.hideSelectAll = headRow.hideSelectAll || ((_a = this.rowSelection) === null || _a === void 0 ? void 0 : _a.hideSelectAll);
      }
    };
  }
  /*
   * The below method is e2e-test covered in
   * @see module:should-render-striped-row-as-expected-when-component-re-attached
   */
  /**
   * Used by rowStripedObserver{@link rowStripedObserver} and currentDataObserver{@link currentDataObserver}
   */
  connectedCallback() {
    methodPlaceholder();
  }
  /*
   * The below method is e2e-test covered in
   * @see module:should-render-striped-row-as-expected-when-component-re-attached
   */
  /**
   * Used by rowStripedObserver{@link rowStripedObserver} and currentDataObserver{@link currentDataObserver}
   */
  disconnectedCallback() {
    methodPlaceholder();
  }
  /**
   * Component lifecycle - [componentWillLoad]{@link VegaTable.componentWillLoad}
   */
  componentWillLoad() {
    this.syncUpTableCurrentData();
    this.syncUpTableHeadRowProps();
    this.syncRowStates();
    this.syncUpTableFootVisibility();
  }
  /**
   * Component lifecycle - [componentDidLoad]{@link VegaTable.componentDidLoad}
   */
  componentDidLoad() {
    methodPlaceholder();
  }
  /**
   * The callback to handle [clearSelection]{@link VegaTable.clearSelection}
   */
  clearSelection() {
    ChangeManager.notify(TABLE_ROW_SELECTION_ALL, {
      host: this.host,
      checked: false,
      noneChecked: true,
    });
  }
  /**
   * The callback to handle [watchCurrentData]{@link VegaTable.watchCurrentData}
   */
  watchCurrentData() {
    this.syncUpTableFootVisibility();
  }
  /**
   * {@link VegaTable.watchStriped}
   */
  watchStriped() {
    this.syncRowStates();
  }
  /**
   * {@link VegaTable.watchRowDivider}
   */
  watchRowDivider() {
    this.syncRowStates();
  }
  /**
   * Update the row striped attr
   *
   * @param {HTMLElement} row - row or expand row element
   * @param {number} index - index of the row
   */
  updateRowStriped(row, index) {
    row.striped = this.striped && index % 2 === 1;
  }
  /**
   * Update the row striped attr
   *
   * @param {HTMLElement} row - row or expand row element
   * @param {boolean} isLastRow - Is last row
   */
  updateRowDivider(row, isLastRow) {
    row.rowDivider = this.rowDivider && !isLastRow;
  }
  /**
   * Sync up the visibility of table foot based on current data
   */
  syncUpTableFootVisibility() {
    const tableFoot = this.host.querySelector('vega-table-foot');
    if (tableFoot) {
      if (this.vegaTableDataSourceController.isCurrentDataEmpty()) {
        tableFoot.classList.add('v-hidden');
      }
      else {
        tableFoot.classList.remove('v-hidden');
      }
    }
  }
}
__decorate([
  MapToComponentField()
], VegaTableChildrenController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaTableChildrenController.prototype, "columns", void 0);
__decorate([
  MapToComponentField()
], VegaTableChildrenController.prototype, "dataSource", void 0);
__decorate([
  MapToComponentField()
], VegaTableChildrenController.prototype, "currentData", void 0);
__decorate([
  MapToComponentField()
], VegaTableChildrenController.prototype, "rowSelection", void 0);
__decorate([
  MapToComponentField()
], VegaTableChildrenController.prototype, "striped", void 0);
__decorate([
  MapToComponentField()
], VegaTableChildrenController.prototype, "rowDivider", void 0);
__decorate([
  MapToComponentField()
], VegaTableChildrenController.prototype, "vegaTableDataSourceController", void 0);
__decorate([
  MapToComponentField()
], VegaTableChildrenController.prototype, "vegaTableRenderer", void 0);
__decorate([
  MapToComponentField()
], VegaTableChildrenController.prototype, "isTemplateRenderMode", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaTableChildrenController.prototype, "currentDataObserver", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaTableChildrenController.prototype, "rowStripedObserver", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], VegaTableChildrenController.prototype, "connectedCallback", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], VegaTableChildrenController.prototype, "disconnectedCallback", null);
__decorate([
  MapToComponentMethod('componentWillLoad')
], VegaTableChildrenController.prototype, "componentWillLoad", null);
__decorate([
  MapToComponentMethod('componentDidLoad')
], VegaTableChildrenController.prototype, "componentDidLoad", null);
__decorate([
  MapToComponentMethod('clearSelection')
], VegaTableChildrenController.prototype, "clearSelection", null);
__decorate([
  MapToComponentMethod('watchCurrentData')
], VegaTableChildrenController.prototype, "watchCurrentData", null);
__decorate([
  MapToComponentMethod('watchStriped')
], VegaTableChildrenController.prototype, "watchStriped", null);
__decorate([
  MapToComponentMethod('watchRowDivider')
], VegaTableChildrenController.prototype, "watchRowDivider", null);
