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 { FORM_ELEMENT_VALUE_CHANGE } from '../../../../helpers/change-manager/subject/static-subject/static-subject-title';
import ChangeManager from '../../../../helpers/change-manager/change-manager';
import { VegaInternalUpdateVegaInputValue } from '../../../../helpers/event-manager/event-id/vega-internal-event-id';
import { DomNodeSubjectObserverFactory } from '../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-observer-factory';
/** Value controller */
export class VegaInputValueController extends VegaSlimmer {
  /** connectedCallback */
  connectedCallback() {
    this.initValueUpdateObserver();
  }
  /** disconnectedCallback */
  disconnectedCallback() {
    this.removeValueUpdateObserver();
  }
  /**
   * Watch value changes
   *
   * @param {string} value The new value
   */
  watchValue(value) {
    this.changeEventEmitter.emit(value);
    const inputRef = this.inputRenderer.getInputRef();
    if (inputRef.value !== this.value) {
      inputRef.value = this.value;
    }
    ChangeManager.notify(FORM_ELEMENT_VALUE_CHANGE, {
      host: this.host,
      detail: this.value,
    });
  }
  /**
   * Initialize calendar clear observer
   */
  initValueUpdateObserver() {
    DomNodeSubjectObserverFactory.addUniqueObserverToNode(this.host, VegaInternalUpdateVegaInputValue, this.setValue.bind(this));
  }
  /**
   * Remove calendar clear observer
   */
  removeValueUpdateObserver() {
    DomNodeSubjectObserverFactory.removeUniqueObserverFromNode(this.host, VegaInternalUpdateVegaInputValue);
  }
  /**
   * Set value
   *
   * @param {string} newValue The new value
   */
  setValue(newValue) {
    this.value = newValue;
  }
}
__decorate([
  MapToComponentField()
], VegaInputValueController.prototype, "host", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaInputValueController.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], VegaInputValueController.prototype, "changeEventEmitter", void 0);
__decorate([
  MapToComponentField()
], VegaInputValueController.prototype, "inputRenderer", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], VegaInputValueController.prototype, "connectedCallback", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], VegaInputValueController.prototype, "disconnectedCallback", null);
__decorate([
  MapToComponentMethod('watchValue')
], VegaInputValueController.prototype, "watchValue", null);
