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, MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
import { Observer } from '../../../../helpers/change-manager/observer/observer';
import domNodeSubjectFactory from '../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-factory';
import { VegaInternalFieldErrorStateChange } from '../../../../helpers/event-manager/event-id/vega-internal-event-id';
import { findParent } from '../../../../utils/ui';
import ChangeManager from '../../../../helpers/change-manager/change-manager';
/**
 * Manager the showError state status in vegaSelectionChip, the value is depend on the parent group validate
 */
export class VegaSelectionChipShowErrorController extends VegaSlimmer {
  /**
   * Component lifecycle - [connectedCallback]
   */
  connectedCallback() {
    this.selectionChipGroup = findParent(this.host, 'vega-selection-chip-group');
    if (!this.selectionChipGroup)
      return;
    this.fieldErrorObserver = new Observer(this.canAcceptFieldErrorObserver.bind(this), this.fieldErrorObserverListener.bind(this));
    ChangeManager.register(domNodeSubjectFactory.getSubject(this.selectionChipGroup, VegaInternalFieldErrorStateChange), this.fieldErrorObserver);
  }
  /**
   * Component lifecycle - [disconnectedCallback]
   */
  disconnectedCallback() {
    if (!this.selectionChipGroup)
      return;
    ChangeManager.unregister(domNodeSubjectFactory.getSubject(this.selectionChipGroup, VegaInternalFieldErrorStateChange), this.fieldErrorObserver);
  }
  /**
   * The function `canAcceptFieldErrorObserver` always returns `true`.
   *
   * @returns {boolean} a boolean value, which indicates the current host is a checkbox or checkbox group.
   */
  canAcceptFieldErrorObserver() {
    return true;
  }
  /**
   * The function listens to the showError property of the returned object.
   *
   * @param {FieldErrorPayload} payload - The payload parameter is of type FieldErrorPayload, which is an object containing information about field.
   */
  fieldErrorObserverListener(payload) {
    this.showError = payload.showError;
  }
}
__decorate([
  MapToComponentField({ writable: true })
], VegaSelectionChipShowErrorController.prototype, "showError", void 0);
__decorate([
  MapToComponentField()
], VegaSelectionChipShowErrorController.prototype, "host", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], VegaSelectionChipShowErrorController.prototype, "connectedCallback", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], VegaSelectionChipShowErrorController.prototype, "disconnectedCallback", null);
