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 { findParent } from '../../../../../utils/ui';
/**
 * The controller for expand row.
 */
export class VegaTableExpandRowExpandController extends VegaSlimmer {
  /**
   * Component lifecycle - [connectedCallback]{@link VegaTableExpandRow.connectedCallback}
   */
  connectedCallback() {
    this.expandObserver = new Observer(this.isAcceptableTableExpandRowObserver.bind(this), (payload) => {
      if (payload.rowKey === this.rowKey) {
        this.expanded = payload.expanded;
      }
    });
    ChangeManager.register(TABLE_EXPAND_ROW, this.expandObserver);
  }
  /**
   * Component lifecycle - [disconnectedCallback]{@link VegaTableExpandRow.disconnectedCallback}
   */
  disconnectedCallback() {
    ChangeManager.unregister(TABLE_EXPAND_ROW, this.expandObserver);
  }
  /**
   * The function checks if the input is an acceptable payload for a VegaTableExpandRowObserver.
   *
   * @param {unknown} input - The `input` parameter is of type `unknown`, which means it can be any type.
   * @returns {boolean} a boolean value indicating whether the input is of type VegaTableExpandRowObserverPayload.
   */
  isAcceptableTableExpandRowObserver(input) {
    if (input['host']['tagName'] === 'VEGA-TABLE') {
      return (input['host']['tagName'] === 'VEGA-TABLE' &&
        input['host'] === findParent(this.host, 'vega-table'));
    }
    if (input['host']['tagName'] === 'VEGA-TABLE-ROW') {
      return findParent(this.host, 'vega-table') === findParent(input['host'], 'vega-table');
    }
  }
}
__decorate([
  MapToComponentField()
], VegaTableExpandRowExpandController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaTableExpandRowExpandController.prototype, "rowKey", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaTableExpandRowExpandController.prototype, "expanded", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], VegaTableExpandRowExpandController.prototype, "connectedCallback", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], VegaTableExpandRowExpandController.prototype, "disconnectedCallback", null);
