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 { Fragment, h } from '@stencil/core';
import { MapToComponentField, MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
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 { VegaInternalIconManager } from '../../../../../helpers/icon/internal-icon-manager';
import chevronDown from '../../../../../global/icons/chevron-down';
import remoteInvocationRegistry from '../../../../../helpers/remote-invocation/remote-invocation-registry';
import domNodeSubjectFactory from '../../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-factory';
import { VegaInternalTableRowSelect } from '../../../../../helpers/event-manager/event-id/vega-internal-event-id';
import { findParent } from '../../../../../utils/ui';
export class VegaTableRowRenderer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.renderRowSelection = () => {
      if (this.selectionType !== 'multiple')
        return;
      return (h("td", { class: "vega-table-selection-column" },
        h("div", { class: "vega-table-checkbox-wrapper" },
          h("vega-checkbox", { size: "small", checked: this.checked, 
            /* eslint-disable-next-line react/jsx-no-bind */
            onClick: this.handleClick, 
            /* eslint-disable-next-line react/jsx-no-bind */
            ref: (ref) => {
              DomNodeSubjectObserverFactory.addUniqueObserverToNode(ref, VegaChange, this.onRowSelectionChange);
            } }))));
    };
    this.renderExpandIcon = () => {
      return (this.expandable && (h("td", { class: "vega-table-expand-action-column" },
        h("div", { class: "vega-table-expand-icon-wrapper" },
          h("vega-icon", { icon: VegaInternalIconManager.getIconKey('chevron-down'), color: "text-link", onClick: this.onExpandIconClick })))));
    };
    this.onRowClick = () => {
      this.vegaTableRowClickController.handleRowClick();
    };
    this.onRowSelectionChange = (input) => {
      if (input.eventName === 'vegaChange') {
        this.vegaTableRowSelectionController.handleRowSelect(input.detail);
      }
    };
    this.handleClick = (e) => {
      e.stopPropagation();
      const rowSelectEvent = remoteInvocationRegistry.call(domNodeSubjectFactory.getSubject(this.parentTableRef, VegaInternalTableRowSelect), {
        host: this.host,
        rowKey: this.rowKey,
        checked: !this.checked,
      });
      if (rowSelectEvent.defaultPrevented) {
        e.preventDefault();
      }
    };
    this.onExpandIconClick = async (e) => {
      e.stopPropagation();
      const expandEvent = this.expandEventEmitter.emit({
        expanded: !this.expanded,
        rowKey: this.rowKey,
      });
      if (expandEvent && !expandEvent.defaultPrevented) {
        this.vegaTableRowExpandController.handleExpandIconClick();
      }
    };
  }
  connectedCallback() {
    this.setParentTableRef();
  }
  getParentTableRef() {
    return this.parentTableRef;
  }
  render() {
    var _a, _b;
    return (h(Fragment, null,
      this.renderExpandRowSpace(this.rowKey),
      h("tr", { class: {
          'vega-table-row': true,
          'checked': this.checked,
          'clickable': this.clickable,
          'current-clicked-row': this.clicked,
          'expand-row': this.expanded,
        }, 
        /* eslint-disable-next-line react/jsx-no-bind */
        onClick: this.clickable ? this.onRowClick : null },
        h("td", { style: { width: (_a = this.paddingX) === null || _a === void 0 ? void 0 : _a[0] } }),
        this.renderRowSelection(),
        h("slot", null),
        this.renderExpandIcon(),
        h("td", { style: { width: (_b = this.paddingX) === null || _b === void 0 ? void 0 : _b[1] } }))));
  }
  renderExpandRowSpace(rowKey) {
    return (this.expandable && (h("tr", { key: `vega-table-expand-row-space-${rowKey}`, class: "expand-row-space" })));
  }
  setParentTableRef() {
    this.parentTableRef = findParent(this.host, 'vega-table');
  }
}
(() => {
  VegaInternalIconManager.register({ 'chevron-down': chevronDown });
})();
__decorate([
  MapToComponentField()
], VegaTableRowRenderer.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowRenderer.prototype, "rowKey", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowRenderer.prototype, "checked", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowRenderer.prototype, "clickable", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowRenderer.prototype, "clicked", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowRenderer.prototype, "expanded", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowRenderer.prototype, "paddingX", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowRenderer.prototype, "expandable", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowRenderer.prototype, "vegaClick", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowRenderer.prototype, "selectionType", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowRenderer.prototype, "vegaTableRowSelectionController", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowRenderer.prototype, "vegaTableRowExpandController", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowRenderer.prototype, "vegaTableRowClickController", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowRenderer.prototype, "expandEventEmitter", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], VegaTableRowRenderer.prototype, "connectedCallback", null);
