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 { VegaInternalFieldErrorStateChange } from '../../../../helpers/event-manager/event-id/vega-internal-event-id';
import { findParent } from '../../../../utils/ui';
import { Observer } from '../../../../helpers/change-manager/observer/observer';
import domNodeSubjectFactory from '../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-factory';
/** The checkbox show error controller slimmer */
export class VegaCheckboxShowErrorController extends VegaSlimmer {
  /**
   * Component lifecycle - [connectedCallback]{@link VegaCheckbox.connectedCallback}
   */
  connectedCallback() {
    this.checkboxGroup =
      findParent(this.host, 'vega-checkbox-group') ||
        findParent(this.host, 'vega-selection-tile-group');
    this.fieldErrorObserver = new Observer(this.canAcceptFieldErrorObserver.bind(this), this.fieldErrorObserverListener.bind(this));
    ChangeManager.register(domNodeSubjectFactory.getSubject(this.checkboxGroup || this.host, VegaInternalFieldErrorStateChange), this.fieldErrorObserver);
  }
  /**
   * Component lifecycle - [disconnectedCallback]{@link VegaCheckbox.disconnectedCallback}
   */
  disconnectedCallback() {
    ChangeManager.unregister(domNodeSubjectFactory.getSubject(this.checkboxGroup || this.host, 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 })
], VegaCheckboxShowErrorController.prototype, "showError", void 0);
__decorate([
  MapToComponentField()
], VegaCheckboxShowErrorController.prototype, "host", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], VegaCheckboxShowErrorController.prototype, "connectedCallback", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], VegaCheckboxShowErrorController.prototype, "disconnectedCallback", null);
