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, VegaSlimmer, MapToComponentMethod } from 'vega-slimmer/core';
import ChangeManager from '../../../../helpers/change-manager/change-manager';
import { Observer } from '../../../../helpers/change-manager/observer/observer';
import { VegaInternalFieldErrorStateChange } from '../../../../helpers/event-manager/event-id/vega-internal-event-id';
import domNodeSubjectFactory from '../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-factory';
import { findParent } from '../../../../utils/ui';
/** The radio show error controller slimmer */
export class VegaRadioShowErrorController extends VegaSlimmer {
  /**
   * Component lifecycle - [connectedCallback]{@link VegaRadio.connectedCallback}
   */
  connectedCallback() {
    this.radioGroup =
      findParent(this.host, 'vega-radio-group') || findParent(this.host, 'vega-selection-tile-group');
    if (this.radioGroup) {
      this.fieldErrorObserver = new Observer(this.canAcceptFieldErrorObserver.bind(this), this.fieldErrorObserverListener.bind(this));
      ChangeManager.register(domNodeSubjectFactory.getSubject(this.radioGroup, VegaInternalFieldErrorStateChange), this.fieldErrorObserver);
    }
  }
  /**
   * Component lifecycle - [disconnectedCallback]{@link VegaRadio.disconnectedCallback}
   */
  disconnectedCallback() {
    if (this.radioGroup) {
      ChangeManager.unregister(domNodeSubjectFactory.getSubject(this.radioGroup, VegaInternalFieldErrorStateChange), this.fieldErrorObserver);
    }
  }
  /**
   * The function checks whether the current host is radio group.
   *
   * @param {FieldErrorPayload} payload - The payload parameter is of type FieldErrorPayload, which is an object containing information about field.
   * @returns {boolean} a boolean value, which indicates the current host is radio group.
   */
  canAcceptFieldErrorObserver(payload) {
    return ((payload.host.tagName === 'VEGA-RADIO-GROUP' ||
      payload.host.tagName === 'VEGA-SELECTION-TILE-GROUP') &&
      this.radioGroup === payload.host);
  }
  /**
   * 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 })
], VegaRadioShowErrorController.prototype, "showError", void 0);
__decorate([
  MapToComponentField()
], VegaRadioShowErrorController.prototype, "host", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], VegaRadioShowErrorController.prototype, "connectedCallback", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], VegaRadioShowErrorController.prototype, "disconnectedCallback", null);
