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 { InjectVegaSlimmer, MapToComponentField as MapToClassField, VegaSlimmer } from 'vega-slimmer/core';
import ChangeManager from '../change-manager/change-manager';
import DomNodeSubjectFactory from '../change-manager/subject/dom-node-subject/dom-node-subject-factory';
import { VegaInternalFieldErrorStateChange } from '../event-manager/event-id/vega-internal-event-id';
import { SiblingContainerNestedErrorContainerRetrievalStrategy } from './field-error-ui-controller-strategies/sibling-container-nested-error-container-retrieval-strategy';
import { SiblingContainerSelfRetrievalStrategy } from './field-error-ui-controller-strategies/sibling-container-self-retrieval-strategy';
import { SiblingContainerErrorLabelContainerRetrievalStrategy } from './field-error-ui-controller-strategies/sibling-container-error-label-container-retrieval-strategy';
import { Observer } from '../change-manager/observer/observer';
import { UPDATE_TRANSLATION } from '../change-manager/subject/static-subject/static-subject-title';
import { FormFieldMessageTranslatorSlimmer } from './form-field-message-translator';
import { htmlSanitizer, labelSanitizeStrategy } from '../../utils/sanitize';
/** The `FieldErrorUIControllerSlimmer` class is responsible for displaying error messages and managing the invalid UI state of a form field. */
export class FieldErrorUIControllerSlimmer extends VegaSlimmer {
  constructor() {
    super();
    this.formFieldMessageTranslatorSlimmer = new FormFieldMessageTranslatorSlimmer();
    this.fieldErrorUiControllerStrategies = [
      new SiblingContainerNestedErrorContainerRetrievalStrategy(),
      new SiblingContainerSelfRetrievalStrategy(),
      new SiblingContainerErrorLabelContainerRetrievalStrategy(),
    ];
    this.initTranslationObserver();
  }
  /**
   * It takes an error message and displays it in the error container.
   * TODO: Each component displays the error message and puts it in the code of the respective component.
   * The ticket is https://gethired.atlassian.net/browse/VD-2508
   *
   * @param {EvaluateResult['message']} errorMessage - The error message to display.
   */
  applyInputInvalidUI(errorMessage) {
    const inputContainer = this.getInputElementContainer();
    ChangeManager.notify(DomNodeSubjectFactory.getSubject(this.host, VegaInternalFieldErrorStateChange), {
      host: this.host,
      showError: true,
    });
    if (inputContainer) {
      inputContainer.classList.add('error');
      if (errorMessage) {
        this.errorMessage = errorMessage;
        const errorContainer = this.getErrorContainer(inputContainer);
        errorContainer.innerHTML = htmlSanitizer.sanitize(this.formFieldMessageTranslatorSlimmer.getTranslatedMessage(errorMessage), labelSanitizeStrategy);
        errorContainer.classList.remove('vega-hidden');
      }
    }
  }
  /**
   * Reset the input element's invalid UI
   * TODO: Each component hides the error message and puts it in the code of the respective component.
   * The ticket is https://gethired.atlassian.net/browse/VD-2508
   */
  resetInputInvalidUI() {
    this.errorMessage = '';
    const inputContainer = this.getInputElementContainer();
    if (inputContainer) {
      const errorContainer = this.getErrorContainer(inputContainer);
      errorContainer.classList.add('vega-hidden');
      inputContainer.classList.remove('error');
      ChangeManager.notify(DomNodeSubjectFactory.getSubject(this.host, VegaInternalFieldErrorStateChange), {
        host: this.host,
        showError: false,
      });
    }
  }
  /**
   * Get the input element container
   *
   * @returns {Nullable<HTMLElement>} The input element container.
   */
  getInputElementContainer() {
    return this.options && this.options.inputContainerGetter ? this.options.inputContainerGetter() : null;
  }
  /**
   * Get the error container, if it doesn't exist, create it
   *
   * @param {HTMLElement} inputContainer - The input element container.
   * @returns {HTMLElement | undefined} The error container.
   */
  getErrorContainer(inputContainer) {
    for (const strategy of this.fieldErrorUiControllerStrategies) {
      const errorContainer = strategy.run(inputContainer);
      if (errorContainer) {
        return errorContainer;
      }
    }
  }
  /**
   * Init translation observer of translation language changes
   */
  initTranslationObserver() {
    const observer = new Observer(this.isAcceptableTranslationObserver.bind(this), () => {
      if (!this.errorMessage)
        return;
      const inputContainer = this.getInputElementContainer();
      if (inputContainer) {
        const errorContainer = this.getErrorContainer(inputContainer);
        if (errorContainer) {
          errorContainer.innerHTML = htmlSanitizer.sanitize(this.formFieldMessageTranslatorSlimmer.getTranslatedMessage(this.errorMessage), labelSanitizeStrategy);
        }
      }
    });
    ChangeManager.register(UPDATE_TRANSLATION, observer);
  }
  /**
   * Determines whether the observer should accept changes.
   *
   * @returns {boolean} Always returns true in the current implementation.
   */
  isAcceptableTranslationObserver() {
    return true;
  }
}
__decorate([
  InjectVegaSlimmer()
], FieldErrorUIControllerSlimmer.prototype, "formFieldMessageTranslatorSlimmer", void 0);
__decorate([
  MapToClassField()
], FieldErrorUIControllerSlimmer.prototype, "host", void 0);
__decorate([
  MapToClassField()
], FieldErrorUIControllerSlimmer.prototype, "options", void 0);
