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, } from 'vega-slimmer/core';
import { VegaInternalValid } from '../event-manager/event-id/vega-internal-event-id';
import { DomNodeSubjectObserverFactory } from '../change-manager/subject/dom-node-subject/dom-node-subject-observer-factory';
/** The FormFieldValidInvokeSlimmer class extends VegaSlimmer and handles validation calls for form elements. */
export class FormFieldValidInvokeSlimmer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    /**
     * call the component validate method, complete the promise and notify the parent form update the isValid property
     *
     * @param {CallValidPayloadType} payload The element, valid parameter and a promise resolve handle
     */
    this.handleValidInvocation = async (payload) => {
      if (this.validator) {
        if (payload.timerId !== undefined) {
          clearTimeout(payload.timerId);
        }
        const validationResult = await this.validator.validate(payload.message);
        if (validationResult) {
          const { isValid, message } = validationResult;
          if (typeof payload.promiseResolve === 'function') {
            payload.promiseResolve({ isValid, message });
          }
        }
      }
    };
  }
  /**
   * register the observer when the slimmer method called
   */
  registerValidInvocationObserver() {
    DomNodeSubjectObserverFactory.addUniqueObserverToNode(this.host, VegaInternalValid, this.handleValidInvocation);
  }
  /**
   * unregister the observer when the slimmer method called
   */
  handleUnregisterObserver() {
    DomNodeSubjectObserverFactory.removeUniqueObserverFromNode(this.host, VegaInternalValid);
  }
}
__decorate([
  MapToSlimmerField()
], FormFieldValidInvokeSlimmer.prototype, "host", void 0);
__decorate([
  MapToSlimmerField()
], FormFieldValidInvokeSlimmer.prototype, "validator", void 0);
__decorate([
  MapToSlimmerMethod('setInjectFormFieldHelper')
], FormFieldValidInvokeSlimmer.prototype, "registerValidInvocationObserver", null);
__decorate([
  MapToSlimmerMethod('disconnectedCallback')
], FormFieldValidInvokeSlimmer.prototype, "handleUnregisterObserver", null);
