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 { MapToComponentField } from '../../../../../../../vega-slimmer/src/decorators/map-to-component-field';
import { MapToComponentMethod } from '../../../../../../../vega-slimmer/src/decorators/map-to-component-method';
import { VegaSlimmer } from '../../../../../../../vega-slimmer/src/decorators/vega-slimmer';
import changeManager from '../../../../../helpers/change-manager/change-manager';
import { Observer } from '../../../../../helpers/change-manager/observer/observer';
import domNodeSubjectFactory from '../../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-factory';
import { VegaInternalTableHeadCellSortEvent } from '../../../../../helpers/event-manager/event-id/vega-internal-event-id';
import { findParent } from '../../../../../utils/ui';
/**
 * Controller for the table head cell sort configuration
 */
export class VegaTableHeadCellSortConfigController extends VegaSlimmer {
  /**
   * Component lifecycle - [disconnectedCallback]
   */
  disconnectedCallback() {
    const parent = findParent(this.host, 'vega-table-head');
    if (parent) {
      changeManager.unregister(domNodeSubjectFactory.getSubject(parent, VegaInternalTableHeadCellSortEvent), this.observer);
    }
  }
  /**
   * Component lifecycle - [componentWillLoad]
   */
  componentWillLoad() {
    const topParent = findParent(this.host, 'vega-table-head');
    if (topParent) {
      this.hostTable = findParent(topParent, 'vega-table');
      this.isTemplateRenderMode = this.checkIsTemplateRenderMode();
      this.observer = new Observer(this.isAcceptableObserver.bind(this), this.observerListener.bind(this));
      changeManager.register(domNodeSubjectFactory.getSubject(topParent, VegaInternalTableHeadCellSortEvent), this.observer);
    }
  }
  /**
   * Notify other table head cells on click sort
   */
  notifyOtherHeadCellOnClickSort() {
    const parent = findParent(this.host, 'vega-table-head');
    if (parent) {
      changeManager.notify(domNodeSubjectFactory.getSubject(parent, VegaInternalTableHeadCellSortEvent), { host: this.host });
    }
  }
  /**
   * The function isAcceptableObserver checks if the input host matches a specified vega table head cell.
   *
   * @param {TableHeadCellObserverPayload} input - The `input` parameter is of type `TableHeadCellObserverPayload`
   * @returns {boolean} The `isAcceptableObserver` function is returns `true`.
   */
  isAcceptableObserver(input) {
    return input['host'] !== this.host;
  }
  /**
   * The function checks if table is in template render mode and if sortConfig is of type single
   */
  observerListener() {
    var _a;
    if (this.isTemplateRenderMode) {
      if (((_a = this.hostTable.sortConfig) === null || _a === void 0 ? void 0 : _a.type) === 'single') {
        this.sortOrder = undefined;
      }
    }
  }
  /**
   * Check whether render mode is template render
   *
   * @returns {boolean} return true if the mode is template, otherwise false
   */
  checkIsTemplateRenderMode() {
    return Array.from(this.hostTable.children).some((el) => el.tagName.startsWith('VEGA-TABLE-'));
  }
}
__decorate([
  MapToComponentField()
], VegaTableHeadCellSortConfigController.prototype, "host", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaTableHeadCellSortConfigController.prototype, "sortOrder", void 0);
__decorate([
  MapToComponentField()
], VegaTableHeadCellSortConfigController.prototype, "sortConfig", void 0);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], VegaTableHeadCellSortConfigController.prototype, "disconnectedCallback", null);
__decorate([
  MapToComponentMethod('componentWillLoad')
], VegaTableHeadCellSortConfigController.prototype, "componentWillLoad", null);
