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 { FORM_RESET_OPERATION_TRIGGERED } from '../../../../helpers/change-manager/subject/static-subject/static-subject-title';
import { VegaInternalFormReset } from '../../../../helpers/event-manager/event-id/vega-internal-event-id';
import { Observer } from '../../../../helpers/change-manager/observer/observer';
import domNodesSubjectFactory from '../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-factory';
import changeManager from '../../../../helpers/change-manager/change-manager';
import { findParent } from '../../../../utils/ui';
/**
 * Reset controller for handling form reset events in the VegaForm component.
 */
export class VegaFormResetController extends VegaSlimmer {
  /**
   * Component lifecycle - [connectedCallback]
   */
  connectedCallback() {
    this.registerFormResetChangeManager();
  }
  /**
   * Component lifecycle - [disconnectedCallback]
   */
  disconnectedCallback() {
    this.unregisterFormResetChangeManager();
  }
  /**
   * @inheritDoc
   */
  async reset(defaultValueMap = {}, option) {
    await this.vegaFormFieldController.navigateVegaFormChildren(async (element, isForm) => {
      const formFieldName = this.vegaFormFieldController.getFormFieldName(element);
      if (isForm) {
        await element.reset(defaultValueMap[formFieldName] || {}, option);
      }
    }, option);
    ChangeManager.notify(FORM_RESET_OPERATION_TRIGGERED, {
      host: this.host,
      defaultValueMap,
      option,
    });
    this.vegaFormFieldController.setIsTouched(false);
  }
  /**
   * Registers the form reset event observer with the ChangeManager.
   */
  registerFormResetChangeManager() {
    var _a;
    const outerForm = (_a = findParent(this.host, 'vega-form', { outerMost: true })) !== null && _a !== void 0 ? _a : this.host;
    this.formResetEventObserver = new Observer(this.canAcceptFormResetEvent.bind(this), this.handleResetEvents.bind(this));
    ChangeManager.register(domNodesSubjectFactory.getSubject(outerForm, VegaInternalFormReset), this.formResetEventObserver);
  }
  /**
   * Unregister the form reset event observer from the ChangeManager.
   */
  unregisterFormResetChangeManager() {
    var _a;
    const outerForm = (_a = findParent(this.host, 'vega-form', { outerMost: true })) !== null && _a !== void 0 ? _a : this.host;
    changeManager.unregister(domNodesSubjectFactory.getSubject(outerForm, VegaInternalFormReset), this.formResetEventObserver);
  }
  /**
   * Can accept form reset event.
   *
   * @param {FormEventsPayload} payload - The form events payload.
   * @returns {boolean} True if the event can be accepted, false otherwise.
   */
  canAcceptFormResetEvent(payload) {
    return this.vegaFormFieldController.isElementInCurrentForm(payload.host);
  }
  /**
   * Handles reset events by resetting the form and emitting a reset event.
   */
  async handleResetEvents() {
    await this.host.reset();
    this.resetEventEmitter.emit();
  }
}
__decorate([
  MapToComponentField()
], VegaFormResetController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaFormResetController.prototype, "vegaFormFieldController", void 0);
__decorate([
  MapToComponentField()
], VegaFormResetController.prototype, "resetEventEmitter", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], VegaFormResetController.prototype, "connectedCallback", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], VegaFormResetController.prototype, "disconnectedCallback", null);
__decorate([
  MapToComponentMethod('reset')
], VegaFormResetController.prototype, "reset", null);
