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_EXPAND_ROW } from '../../../../../helpers/change-manager/subject/static-subject/static-subject-title';
import { Observer } from '../../../../../helpers/change-manager/observer/observer';
import { isAcceptableTableObserver } from '../../../type-guard';
import domNodeSubjectFactory from '../../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-factory';
import remoteInvocationRegistry from '../../../../../helpers/remote-invocation/remote-invocation-registry';
import { VegaInternalTableRowExpand } from '../../../../../helpers/event-manager/event-id/vega-internal-event-id';
/**
 * The row expand controller for expandable table row.
 */
export class VegaTableRowExpandController extends VegaSlimmer {
  /**
   * Component lifecycle - [connectedCallback]{@link VegaTableRow.connectedCallback}
   */
  connectedCallback() {
    this.initRowExpandObserver();
  }
  /**
   * Component lifecycle - [disconnectedCallback]{@link VegaTableRow.disconnectedCallback}
   */
  disconnectedCallback() {
    ChangeManager.unregister(TABLE_EXPAND_ROW, this.rowExpandObserver);
  }
  /**
   * The handler for expand icon click event.
   */
  handleExpandIconClick() {
    const rowExpandEvent = remoteInvocationRegistry.call(domNodeSubjectFactory.getSubject(this.vegaTableRowRenderer.getParentTableRef(), VegaInternalTableRowExpand), {
      host: this.host,
      rowKey: this.rowKey,
      expanded: !this.expanded,
    });
    if (!rowExpandEvent.defaultPrevented) {
      this.expanded = !this.expanded;
      ChangeManager.notify(TABLE_EXPAND_ROW, {
        host: this.host,
        rowKey: this.rowKey,
        expanded: this.expanded,
      });
    }
  }
  /**
   * Initialize the row expand observer.
   */
  initRowExpandObserver() {
    this.rowExpandObserver = new Observer(isAcceptableTableObserver.bind({ host: this.host }), (payload) => {
      if (payload.rowKey === this.rowKey) {
        this.expanded = payload.expanded;
      }
    });
    ChangeManager.register(TABLE_EXPAND_ROW, this.rowExpandObserver);
  }
}
__decorate([
  MapToComponentField()
], VegaTableRowExpandController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowExpandController.prototype, "rowKey", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowExpandController.prototype, "expandable", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaTableRowExpandController.prototype, "expanded", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowExpandController.prototype, "vegaTableRowRenderer", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], VegaTableRowExpandController.prototype, "connectedCallback", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], VegaTableRowExpandController.prototype, "disconnectedCallback", null);
