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 { Observer } from '../../../../../helpers/change-manager/observer/observer';
import ChangeManager from '../../../../../helpers/change-manager/change-manager';
import { TABLE_ROW_SELECTION, TABLE_ROW_SELECTION_ALL, } from '../../../../../helpers/change-manager/subject/static-subject/static-subject-title';
import { isAcceptableTableObserver } from '../../../type-guard';
/**
 * The row selection controller for the table row
 */
export class VegaTableRowSelectionController extends VegaSlimmer {
  constructor() {
    super(...arguments);
    /**
     * Handles the row selection event.
     *
     * @param {boolean} checked - The "checked" parameter is a boolean value that indicates whether an item is checked or not.
     */
    this.handleRowSelect = (checked) => {
      this.checked = checked;
      ChangeManager.notify(TABLE_ROW_SELECTION, {
        host: this.host,
        checked: checked,
        rowKey: this.rowKey,
      });
    };
  }
  /**
   * Component lifecycle - [connectedCallback]{@link VegaTableRow.connectedCallback}
   */
  connectedCallback() {
    this.initRowSelectionObserver();
  }
  /**
   * Component lifecycle - [disconnectedCallback]{@link VegaTableRow.disconnectedCallback}
   */
  disconnectedCallback() {
    ChangeManager.unregister(TABLE_ROW_SELECTION_ALL, this.rowSelectionAllObserver);
    ChangeManager.unregister(TABLE_ROW_SELECTION, this.rowSelectionObserver);
  }
  /**
   * The function sets the value of the "checked" property to the provided boolean value.
   *
   * @param {boolean} checked - The "checked" parameter is a boolean value that indicates whether an item is checked or not.
   */
  setChecked(checked) {
    this.checked = checked;
  }
  /**
   * Initialize the row selection observer.
   */
  initRowSelectionObserver() {
    this.rowSelectionAllObserver = new Observer(isAcceptableTableObserver.bind(this), (payload) => {
      if (payload.checked || (payload.checked === false && payload.noneChecked)) {
        this.checked = payload.checked;
      }
    });
    ChangeManager.register(TABLE_ROW_SELECTION_ALL, this.rowSelectionAllObserver);
    this.rowSelectionObserver = new Observer(isAcceptableTableObserver.bind(this), (payload) => {
      if (payload.rowKey === this.rowKey) {
        this.checked = payload.checked;
      }
    });
    ChangeManager.register(TABLE_ROW_SELECTION, this.rowSelectionObserver);
  }
}
__decorate([
  MapToComponentField()
], VegaTableRowSelectionController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowSelectionController.prototype, "selectionType", void 0);
__decorate([
  MapToComponentField()
], VegaTableRowSelectionController.prototype, "rowKey", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaTableRowSelectionController.prototype, "checked", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], VegaTableRowSelectionController.prototype, "connectedCallback", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], VegaTableRowSelectionController.prototype, "disconnectedCallback", null);
