text: import { FormFieldState } from '../form-field-state/form-field-state';
  /**
   * Form Field Class
   */
  var FormField = /** @class */ (function () {
      /**
       *
       * @param accessCallback function called to initialize the value of a field
       * @param validationCallback function called to validate
       * @param options options for the field
       */
      function FormField(accessCallback, validationCallback, options) {
          if (validationCallback === void 0) { validationCallback = []; }
          this.accessCallback = accessCallback;
          this.validationCallback = validationCallback;
          this.messageAfterTouched = true;
          this.touched = false;
          this.shouldFocusInternal = false;
          this.externalError = '';
          this.internalValue = accessCallback();
          this.internalState = new FormFieldState();
          if (options) {
              this.messageAfterTouched = typeof options.messageAfterTouched === undefined ?
                  this.messageAfterTouched : options.messageAfterTouched;
          }
      }
      Object.defineProperty(FormField.prototype, "value", {
          /**
           * value of the field
           */
          get: function () {
              return this.internalValue;
          },
          /**
           * set the value of the field
           */
          set: function (newValue) {
              this.change(newValue);
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(FormField.prototype, "hasError", {
          /**
           * indicates if the field has error(s)
           */
          get: function () {
              return this.internalState.hasError;
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(FormField.prototype, "ExternalError", {
          /**
           * get external error
           */
          get: function () {
              return this.externalError;
          },
          /**
           * set external error and trigger validation
           */
          set: function (value) {
              this.externalError = value;
              this.validate();
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(FormField.prototype, "isTouched", {
          /**
           * indicates if the field is touched
           */
          get: function () {
              return this.touched;
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(FormField.prototype, "shouldFocus", {
          /**
           * if the field should focus
           */
          get: function () {
              return this.shouldFocusInternal;
          },
          /**
           * set should focus on field
           */
          set: function (value) {
              this.shouldFocusInternal = value;
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(FormField.prototype, "errorMessage", {
          /**
           * message to show under the form field
           */
          get: function () {
              var errorMessageToShow = '';
              if (this.hasError && ((this.messageAfterTouched && this.touched) || !this.messageAfterTouched)) {
                  errorMessageToShow = this.internalState.errorMessages[0];
              }
              return errorMessageToShow;
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(FormField.prototype, "errorMessageSummary", {
          /**
           * message to show in the error summary
           */
          get: function () {
              return this.internalState.errorMessagesSummary;
          },
          enumerable: true,
          configurable: true
      });
      /**
       * execute validations
       */
      FormField.prototype.validate = function () {
          var _this = this;
          if (this.validationCallback.length > 0) {
              var newState_1 = new FormFieldState();
              this.validationCallback.forEach(function (validationFunction) {
                  var validation = validationFunction(_this);
                  if (validation.isError) {
                      newState_1.hasError = true;
                  }
                  if (validation.errorMessages.length > 0) {
                      newState_1.errorMessages = newState_1.errorMessages.concat(validation.errorMessages);
                  }
                  if (validation.errorMessagesSummary.length > 0) {
                      newState_1.errorMessagesSummary = newState_1.errorMessagesSummary.concat(validation.errorMessagesSummary);
                  }
              });
              this.changeState(newState_1);
          }
      };
      /**
       * mark the field as touched, reset external error and trigger validation
       */
      FormField.prototype.touch = function () {
          this.touched = true;
          this.externalError = '';
          this.validate();
      };
      /**
       * reset the field without validating
       */
      FormField.prototype.reset = function () {
          this.internalValue = this.accessCallback();
          this.oldValue = this.internalValue;
          this.internalState = new FormFieldState();
          this.externalError = '';
          this.touched = false;
      };
      /**
       * changes the value and execute validations
       * @param value the new value of the field
       */
      FormField.prototype.change = function (value) {
          if (typeof value === 'object' || value !== this.oldValue) {
              this.internalValue = value;
              this.oldValue = this.internalValue;
              this.validate();
          }
      };
      FormField.prototype.changeState = function (etat) {
          this.internalState.hasError = etat.hasError;
          this.internalState.errorMessages = etat.errorMessages;
          this.internalState.errorMessagesSummary = etat.errorMessagesSummary;
      };
      return FormField;
  }());
  export { FormField };
  //# sourceMappingURL=form-field.js.map