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 { h } from '@stencil/core';
import { MapToComponentField, VegaSlimmer } from 'vega-slimmer/core';
import { DomNodeSubjectObserverFactory } from '../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-observer-factory';
import { VegaClick } from '../../../../helpers/event-manager/event-id/vega-event-id';
export class VegaTableHeaderRenderer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.renderHeaderRows = () => {
      const renderInputs = this.vegaTableRenderInputController.getHeadRenderInputs();
      const rows = [];
      let levelRenderInputs = renderInputs;
      while (levelRenderInputs.length > 0) {
        rows.push(levelRenderInputs);
        levelRenderInputs = levelRenderInputs.flatMap((n) => n.children);
      }
      return rows.map((cells) => {
        var _a;
        return (h("vega-table-head-row", { hideSelectAll: (_a = this.rowSelection) === null || _a === void 0 ? void 0 : _a.hideSelectAll, ref: (ref) => {
            this.vegaTableRenderInputController.addHeadRowRenderInputMap(ref, cells);
          } }, cells.map((renderInput) => {
          if (renderInput.label) {
            return this.renderGroupCell(renderInput);
          }
          else {
            const column = this.columns.find((column) => column.key === renderInput.key ||
              column.prop === renderInput.key ||
              column.label === renderInput.key);
            return this.renderCell(renderInput, column);
          }
        })));
      });
    };
    this.renderGroupCell = (renderInput) => {
      return (h("vega-table-head-cell", { key: renderInput.key, ref: (ref) => {
          this.vegaTableRenderInputController.addHeadCellRenderInputMap(ref, renderInput);
        } }, renderInput.label));
    };
    this.renderCell = (renderInput, column) => {
      var _a;
      if (column) {
        return (h("vega-table-head-cell", { key: column.key || column.prop, textAlign: column.align, sorter: column.sorter, sortOrder: (_a = this.sortersMap.get(column.prop)) === null || _a === void 0 ? void 0 : _a.getSortOrder(), sortConfig: this.sortConfig, breakpoint: column.breakpoint, 
          /* eslint-disable-next-line react/jsx-no-bind */
          ref: (ref) => {
            this.vegaTableRenderInputController.addHeadCellRenderInputMap(ref, renderInput);
            DomNodeSubjectObserverFactory.addUniqueObserverToNode(ref, VegaClick, (input) => {
              const sortOrder = input.detail.sortOrder;
              this.vegaTableSortController.handleHeadSortClick(column, sortOrder);
            });
          } }, column.label));
      }
      else {
        return (h("vega-table-head-cell", { key: renderInput.key, ref: (ref) => {
            this.vegaTableRenderInputController.addHeadCellRenderInputMap(ref, renderInput);
          } }));
      }
    };
  }
  render() {
    return this.showHeader && h("vega-table-head", null, this.renderHeaderRows());
  }
}
__decorate([
  MapToComponentField()
], VegaTableHeaderRenderer.prototype, "showHeader", void 0);
__decorate([
  MapToComponentField()
], VegaTableHeaderRenderer.prototype, "sortersMap", void 0);
__decorate([
  MapToComponentField()
], VegaTableHeaderRenderer.prototype, "sortConfig", void 0);
__decorate([
  MapToComponentField()
], VegaTableHeaderRenderer.prototype, "columns", void 0);
__decorate([
  MapToComponentField()
], VegaTableHeaderRenderer.prototype, "rowSelection", void 0);
__decorate([
  MapToComponentField()
], VegaTableHeaderRenderer.prototype, "vegaTableSortController", void 0);
__decorate([
  MapToComponentField()
], VegaTableHeaderRenderer.prototype, "vegaTableDataSourceController", void 0);
__decorate([
  MapToComponentField()
], VegaTableHeaderRenderer.prototype, "vegaTableRenderInputController", void 0);
