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 ChangeManager from '../../../../helpers/change-manager/change-manager';
import { FIELD_ERROR_MESSAGE_CHANGE, FORM_ELEMENT_VALIDATE, } from '../../../../helpers/change-manager/subject/static-subject/static-subject-title';
import { FormFieldControllerSlimmer } from '../../../../helpers/slimmers/form-field-controller-slimmer';
/**
 * The field error controller for vega form to set validation errors on form fields.
 */
export class VegaFormFieldValidationStateController extends VegaSlimmer {
  /**
   * Set validation states for the form fields.
   *
   * @param {VegaFormFieldValidationState[]} states - An array of validation error objects.
   */
  setValidationStates(states) {
    if (states.length === 0) {
      Array.from(this.host.querySelectorAll('[data-vega-form]'))
        .filter((child) => FormFieldControllerSlimmer.isValidFormField(child))
        .forEach((fieldHost) => {
        this.updateFieldValidationState(fieldHost, { isValid: true });
      });
      return;
    }
    states.forEach((item) => {
      const fieldHost = this.getHostByFieldName(item.fieldName);
      if (fieldHost) {
        this.updateFieldValidationState(fieldHost, {
          isValid: item.isValid,
          errorMessage: !item.isValid ? item.errorMessage : '',
        });
      }
    });
  }
  /**
   * Get the host element by field name, support nested field name with dot notation.
   *
   * @param {string} fieldName - The field name, can be nested with dot notation.
   * @returns {Nullable<FormFieldElement<unknown>>} The host element or null if not found.
   */
  getHostByFieldName(fieldName) {
    const fields = fieldName.split('.');
    let currentHost = this.host;
    for (const fieldName of fields) {
      const element = currentHost.querySelector(`[data-vega-form="${fieldName}"]`);
      if (FormFieldControllerSlimmer.isValidFormField(element)) {
        currentHost = element;
      }
      else {
        return null;
      }
    }
    return currentHost;
  }
  /**
   * Update the validation state of a form field element.
   *
   * @param {FormFieldElement<unknown>} target - The target form field element.
   * @param {Pick<VegaFormFieldValidationState, 'isValid' | 'errorMessage'>} payload - An object containing the validation state and error message.
   */
  updateFieldValidationState(target, { isValid, errorMessage }) {
    target.isValid = isValid;
    ChangeManager.notify(FIELD_ERROR_MESSAGE_CHANGE, {
      host: target,
      message: !isValid ? errorMessage : '',
      isValid: isValid,
    });
    ChangeManager.notify(FORM_ELEMENT_VALIDATE, { host: target, detail: isValid });
  }
}
__decorate([
  MapToComponentField()
], VegaFormFieldValidationStateController.prototype, "host", void 0);
__decorate([
  MapToComponentMethod('setValidationStates')
], VegaFormFieldValidationStateController.prototype, "setValidationStates", null);
