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 } from 'vega-slimmer/core';
import { SubStateObserverSlimmer } from '../../../../../helpers/slimmers/sub-state/sub-state-observer-slimmer';
import { findParent } from '../../../../../utils/ui';
/**
 * Controller for the table row states
 */
export class VegaTableRowStateController extends SubStateObserverSlimmer {
  // eslint-disable-next-line jsdoc/require-jsdoc
  constructor() {
    super({
      parent: 'vega-table',
      subStates: [
        {
          subStateName: 'expandable',
          parentStateName: 'expandableRows',
          // eslint-disable-next-line jsdoc/require-jsdoc
          callback: (value) => {
            this.expandable = value.includes(this.rowKey);
          },
        },
        {
          subStateName: 'hasExpandableRow',
          parentStateName: 'hasExpandableRow',
        },
        {
          subStateName: 'clickable',
          parentStateName: 'rowClickable',
        },
        {
          subStateName: 'paddingX',
          parentStateName: 'paddingX',
          // eslint-disable-next-line jsdoc/require-jsdoc
          callback: (value) => {
            const table = findParent(this.host, 'vega-table');
            if (table.dataSource.length === 0 || table.columns.length === 0) {
              this.paddingX = value;
            }
          },
        },
        {
          subStateName: 'selectionType',
          parentStateName: 'rowSelection',
          // eslint-disable-next-line jsdoc/require-jsdoc
          callback: (value) => {
            if (value) {
              const { type = 'multiple' } = value;
              this.selectionType = type;
            }
            else {
              this.selectionType = null;
            }
          },
        },
        {
          subStateName: 'columnDivider',
          parentStateName: 'columnDivider',
        },
        {
          subStateName: 'density',
          parentStateName: 'density',
        },
        {
          subStateName: 'draggable',
          parentStateName: 'rowDraggable',
        },
      ],
    });
  }
  /**
   * Component watch method - [watchRowSelection]{@link VegaTableRow.watchSelectionType}
   */
  watchSelectionType() {
    this.updateClickable();
  }
  /**
   * The `@MapToComponentMethod('componentWillLoad')` decorator is used to map the `handleComponentWillLoad` method
   * to the `componentWillLoad` lifecycle hook of the component.
   */
  handleComponentWillLoad() {
    this.updateClickable();
  }
  /**
   * The function updates the value of the "clickable" property based on the value of the
   * "selectionType" property.
   */
  updateClickable() {
    if (this.selectionType) {
      this.clickable = this.selectionType === 'single' || this.clickable;
    }
  }
}
__decorate([
  MapToComponentField()
], VegaTableRowStateController.prototype, "host", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaTableRowStateController.prototype, "expandable", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaTableRowStateController.prototype, "hasExpandableRow", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaTableRowStateController.prototype, "clickable", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaTableRowStateController.prototype, "paddingX", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaTableRowStateController.prototype, "selectionType", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaTableRowStateController.prototype, "columnDivider", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaTableRowStateController.prototype, "density", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaTableRowStateController.prototype, "draggable", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowStateController.prototype, "rowKey", void 0);
__decorate([
  MapToComponentMethod('watchSelectionType')
], VegaTableRowStateController.prototype, "watchSelectionType", null);
__decorate([
  MapToComponentMethod('componentWillLoad')
], VegaTableRowStateController.prototype, "handleComponentWillLoad", null);
