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, methodPlaceholder, VegaSlimmer } from 'vega-slimmer/core';
import { h } from '@stencil/core';
import { isNonNullable } from '../../../../../types/type-guard';
import ChangeManager from '../../../../../helpers/change-manager/change-manager';
import { TABLE_ROW_SELECTION_ALL } from '../../../../../helpers/change-manager/subject/static-subject/static-subject-title';
import { DomNodeSubjectObserverFactory } from '../../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-observer-factory';
import { VegaChange } from '../../../../../helpers/event-manager/event-id/vega-event-id';
import remoteInvocationRegistry from '../../../../../helpers/remote-invocation/remote-invocation-registry';
import domNodeSubjectFactory from '../../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-factory';
import { findParent } from '../../../../../utils/ui';
import { VegaInternalTableRowSelectAll } from '../../../../../helpers/event-manager/event-id/vega-internal-event-id';
export class VegaTableHeadRowRenderer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.renderHeaderSelectionCell = (renderInput) => {
      if (this.selectionType !== 'multiple')
        return;
      return (h("th", { rowSpan: renderInput.startRowSpan, class: {
          'vega-table-selection-column': true,
          'hidden': renderInput.isStartHidden,
        } }, !this.hideSelectAll && (h("div", { class: "vega-table-checkbox-wrapper" },
        h("vega-checkbox", { size: "small", disabled: this.isEmptyTable || this.selectAllDisabled, checked: this.checkedAll, onClick: this.handleClick, 
          /* eslint-disable-next-line react/jsx-no-bind */
          ref: (ref) => {
            DomNodeSubjectObserverFactory.addUniqueObserverToNode(ref, VegaChange, this.handleSelectAll);
          } })))));
    };
    this.handleSelectAll = (input) => {
      if (input.eventName === 'vegaChange') {
        const checked = input.detail;
        if (isNonNullable(checked) && checked !== this.checkedAll) {
          ChangeManager.notify(TABLE_ROW_SELECTION_ALL, {
            host: this.host,
            checked: checked,
          });
        }
      }
    };
    this.renderHeaderExpandableCell = (renderInput) => {
      return (this.expandable && (h("th", { rowSpan: renderInput.endRowSpan, class: {
          'vega-table-expand-action-column': true,
          'hidden': renderInput.isEndHidden,
        } })));
    };
    this.handleClick = (e) => {
      e.stopPropagation();
      const event = remoteInvocationRegistry.call(domNodeSubjectFactory.getSubject(findParent(this.host, 'vega-table'), VegaInternalTableRowSelectAll), {
        host: this.host,
        checked: !this.checkedAll,
      });
      if (event.defaultPrevented) {
        e.preventDefault();
      }
    };
  }
  /**
   * Used by {@link currentDataObserver}
   */
  connectedCallback() {
    methodPlaceholder();
  }
  /**
   * Used by {@link currentDataObserver}
   */
  disconnectedCallback() {
    methodPlaceholder();
  }
  /**
   * Used by {@link currentDataObserver}
   */
  componentDidLoad() {
    methodPlaceholder();
  }
  render() {
    var _a, _b;
    const renderInput = this.getRowRenderInput();
    return (h("tr", { class: {
        'is-group-row': renderInput.headHasGroupColumn,
        'is-pinned-row': renderInput.headHasPinnedColumn,
      } },
      h("th", { style: {
          width: (_a = this.paddingX) === null || _a === void 0 ? void 0 : _a[0],
        }, rowSpan: renderInput.startRowSpan, class: {
          hidden: renderInput.isStartHidden,
        } }),
      this.renderHeaderSelectionCell(renderInput),
      h("slot", null),
      this.renderHeaderExpandableCell(renderInput),
      h("th", { style: { width: (_b = this.paddingX) === null || _b === void 0 ? void 0 : _b[1] }, rowSpan: renderInput.endRowSpan, class: {
          hidden: renderInput.isEndHidden,
        } })));
  }
  getRowRenderInput() {
    const renderInput = {};
    const renderInputs = this.renderInput;
    const startRenderInput = renderInputs === null || renderInputs === void 0 ? void 0 : renderInputs[0];
    const endRenderInput = renderInputs === null || renderInputs === void 0 ? void 0 : renderInputs[renderInputs.length - 1];
    if (startRenderInput) {
      renderInput.headHasGroupColumn = startRenderInput.headHasGroupColumn;
      renderInput.headHasPinnedColumn = startRenderInput.headHasPinnedColumn;
      if (renderInput.headHasGroupColumn) {
        renderInput.startRowSpan =
          this.selectionType === 'multiple' && startRenderInput.level === 0
            ? startRenderInput.headMaxDepth
            : startRenderInput.rowSpan;
        renderInput.isStartHidden =
          startRenderInput.headHasGroupColumn &&
            ((this.selectionType === 'multiple' && startRenderInput.level > 0) ||
              !startRenderInput.isViewStart);
      }
    }
    if (endRenderInput && endRenderInput.headHasGroupColumn) {
      renderInput.endRowSpan = endRenderInput.rowSpan;
      renderInput.isEndHidden = startRenderInput.headHasGroupColumn && !endRenderInput.isViewEnd;
    }
    return renderInput;
  }
}
__decorate([
  MapToComponentField()
], VegaTableHeadRowRenderer.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaTableHeadRowRenderer.prototype, "paddingX", void 0);
__decorate([
  MapToComponentField()
], VegaTableHeadRowRenderer.prototype, "selectionType", void 0);
__decorate([
  MapToComponentField()
], VegaTableHeadRowRenderer.prototype, "hideSelectAll", void 0);
__decorate([
  MapToComponentField()
], VegaTableHeadRowRenderer.prototype, "selectAllDisabled", void 0);
__decorate([
  MapToComponentField()
], VegaTableHeadRowRenderer.prototype, "checkedAll", void 0);
__decorate([
  MapToComponentField()
], VegaTableHeadRowRenderer.prototype, "expandable", void 0);
__decorate([
  MapToComponentField()
], VegaTableHeadRowRenderer.prototype, "isEmptyTable", void 0);
__decorate([
  MapToComponentField()
], VegaTableHeadRowRenderer.prototype, "isTemplateRenderMode", void 0);
__decorate([
  MapToComponentField()
], VegaTableHeadRowRenderer.prototype, "renderInput", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], VegaTableHeadRowRenderer.prototype, "connectedCallback", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], VegaTableHeadRowRenderer.prototype, "disconnectedCallback", null);
__decorate([
  MapToComponentMethod('componentDidLoad')
], VegaTableHeadRowRenderer.prototype, "componentDidLoad", null);
