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 { MapToComponentMethod, MapToComponentField, VegaSlimmer } from 'vega-slimmer/core';
import ChangeManager from '../../../../../helpers/change-manager/change-manager';
import { TABLE_ROW_CLICK } from '../../../../../helpers/change-manager/subject/static-subject/static-subject-title';
import { Observer } from '../../../../../helpers/change-manager/observer/observer';
import { isAcceptableTableRowObserver } from '../../../type-guard';
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';
/**
 * The row click controller for table row
 */
export class VegaTableRowClickController extends VegaSlimmer {
  /**
   * Component lifecycle - [connectedCallback]{@link VegaTableRow.connectedCallback}
   */
  connectedCallback() {
    this.initRowClickObserver();
  }
  /**
   * Component lifecycle - [disconnectedCallback]{@link VegaTableRow.disconnectedCallback}
   */
  disconnectedCallback() {
    ChangeManager.unregister(TABLE_ROW_CLICK, this.rowClickObserver);
  }
  /**
   * Row click handler
   */
  handleRowClick() {
    const rowSelectEvent = remoteInvocationRegistry.call(domNodeSubjectFactory.getSubject(this.vegaTableRowRenderer.getParentTableRef(), VegaInternalTableRowSelect), {
      host: this.host,
      rowKey: this.rowKey,
      checked: !this.checked,
    });
    if (!rowSelectEvent.defaultPrevented) {
      ChangeManager.notify(TABLE_ROW_CLICK, { host: this.host, rowKey: this.rowKey });
      this.clickEventEmitter.emit({ rowKey: this.rowKey });
    }
  }
  /**
   * Initialize row click observer
   */
  initRowClickObserver() {
    this.rowClickObserver = new Observer(isAcceptableTableRowObserver.bind(this), (payload) => {
      this.clicked = payload.host === this.host;
      if (this.selectionType === 'single') {
        if (payload.host === this.host) {
          this.vegaTableRowSelectionController.setChecked(!this.checked);
        }
        else {
          this.vegaTableRowSelectionController.setChecked(false);
        }
      }
    });
    ChangeManager.register(TABLE_ROW_CLICK, this.rowClickObserver);
  }
}
__decorate([
  MapToComponentField()
], VegaTableRowClickController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowClickController.prototype, "rowKey", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaTableRowClickController.prototype, "clicked", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowClickController.prototype, "checked", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowClickController.prototype, "selectionType", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowClickController.prototype, "vegaClick", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowClickController.prototype, "vegaTableRowSelectionController", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowClickController.prototype, "clickEventEmitter", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowClickController.prototype, "vegaTableRowRenderer", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], VegaTableRowClickController.prototype, "connectedCallback", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], VegaTableRowClickController.prototype, "disconnectedCallback", null);
