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 as MapToSlimmerField, MapToComponentMethod as MapToSlimmerMethod, VegaSlimmer, InjectVegaSlimmer, } from 'vega-slimmer/core';
import { Observer } from '../change-manager/observer/observer';
import changeManager from '../change-manager/change-manager';
import { FIELD_ERROR_MESSAGE_CHANGE } from '../change-manager/subject/static-subject/static-subject-title';
import { FieldErrorUIControllerSlimmer } from './field-error-ui-controller-slimmer';
/**
 * The current class is used to manually control the form element error message.
 * The component `vega-field-error` is used to control the error messages for form fields. to implement the error message display, we needs to know the form field error message container, field element, and other necessary options,
 * the `form-field-controller-slimmer` has the required options and will inject to the all form fields, so we create current slimmer class and inject to the `form-field-controller-slimmer` to get all options.
 * When the property `message` of the vega-field-error component changed, the target form field will receive the special subject and update the error message.
 */
export class FieldErrorControllerSlimmer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    // The host element field error UI controller.
    this.fieldErrorUIController = new FieldErrorUIControllerSlimmer();
    /**
     * This method called mean that the current form field element nest in a vega-field-error component and the parent component property `message` changed, we need to show or hide the error message.
     *
     * @param {FieldErrorEventPayload} payload payload param
     */
    this.handleFieldErrorMessageUpdate = (payload) => {
      const { message, isValid } = payload;
      if (!isValid) {
        this.fieldErrorUIController.applyInputInvalidUI(message);
      }
      else {
        this.fieldErrorUIController.resetInputInvalidUI();
      }
    };
  }
  /**
   * register the observer when the slimmer method called
   */
  handleComponentWillLoadByFieldErrorSlimmer() {
    this.fieldErrorMessageChangeObserver = new Observer(this.canAcceptFieldErrorMessageUpdate.bind(this), this.handleFieldErrorMessageUpdate);
    changeManager.register(FIELD_ERROR_MESSAGE_CHANGE, this.fieldErrorMessageChangeObserver);
  }
  /**
   * unregister the observer when the slimmer method called
   */
  handleUnregisterObserver() {
    changeManager.unregister(FIELD_ERROR_MESSAGE_CHANGE, this.fieldErrorMessageChangeObserver);
  }
  /**
   * The function checks if the host element is the parent of the field box.
   *
   * @param {FieldErrorEventPayload} payload - The payload parameter is of type FieldErrorEventPayload.
   * @returns {boolean} a boolean value.
   */
  canAcceptFieldErrorMessageUpdate(payload) {
    const { host } = payload;
    const parentFieldBox = this.host.parentElement;
    return ((parentFieldBox && parentFieldBox === host && parentFieldBox.tagName === 'VEGA-FIELD-ERROR') ||
      this.host === host);
  }
}
__decorate([
  MapToSlimmerField()
], FieldErrorControllerSlimmer.prototype, "host", void 0);
__decorate([
  MapToSlimmerField()
], FieldErrorControllerSlimmer.prototype, "options", void 0);
__decorate([
  InjectVegaSlimmer()
], FieldErrorControllerSlimmer.prototype, "fieldErrorUIController", void 0);
__decorate([
  MapToSlimmerMethod('setInjectFormFieldHelper')
], FieldErrorControllerSlimmer.prototype, "handleComponentWillLoadByFieldErrorSlimmer", null);
__decorate([
  MapToSlimmerMethod('disconnectedCallback')
], FieldErrorControllerSlimmer.prototype, "handleUnregisterObserver", null);
