text: "use strict";
  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;
  };
  var __metadata = (this && this.__metadata) || function (k, v) {
      if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
  };
  Object.defineProperty(exports, "__esModule", { value: true });
  var core_1 = require("@angular/core");
  var ng_jhipster_1 = require("ng-jhipster");
  var JhiAlertErrorComponent = /** @class */ (function () {
      // tslint:disable-next-line: no-unused-variable
      function JhiAlertErrorComponent(alertService, eventManager) {
          var _this = this;
          this.alertService = alertService;
          this.eventManager = eventManager;
          this.alerts = [];
          this.cleanHttpErrorListener = eventManager.subscribe('devicePoolApp.httpError', function (response) {
              var i;
              var httpResponse = response.content;
              switch (httpResponse.status) {
                  // connection refused, server not reachable
                  case 0:
                      _this.addErrorAlert('Server not reachable', 'error.server.not.reachable');
                      break;
                  case 400:
                      var arr = Array.from(httpResponse.headers._headers);
                      var headers = [];
                      for (i = 0; i < arr.length; i++) {
                          if (arr[i][0].endsWith('app-error') || arr[i][0].endsWith('app-params')) {
                              headers.push(arr[i][0]);
                          }
                      }
                      headers.sort();
                      var errorHeader = null;
                      var entityKey = null;
                      if (headers.length > 1) {
                          errorHeader = httpResponse.headers.get(headers[0]);
                          entityKey = httpResponse.headers.get(headers[1]);
                      }
                      if (errorHeader) {
                          var entityName = entityKey;
                          _this.addErrorAlert(errorHeader, errorHeader, { entityName: entityName });
                      }
                      else if (httpResponse.text() !== '' && httpResponse.json() && httpResponse.json().fieldErrors) {
                          var fieldErrors = httpResponse.json().fieldErrors;
                          for (i = 0; i < fieldErrors.length; i++) {
                              var fieldError = fieldErrors[i];
                              // convert 'something[14].other[4].id' to 'something[].other[].id' so translations can be written to it
                              var convertedField = fieldError.field.replace(/\[\d*\]/g, '[]');
                              var fieldName = convertedField.charAt(0).toUpperCase() +
                                  convertedField.slice(1);
                              _this.addErrorAlert('Error on field "' + fieldName + '"', 'error.' + fieldError.message, { fieldName: fieldName });
                          }
                      }
                      else if (httpResponse.text() !== '' && httpResponse.json() && httpResponse.json().message) {
                          _this.addErrorAlert(httpResponse.json().message, httpResponse.json().message, httpResponse.json().params);
                      }
                      else {
                          _this.addErrorAlert(httpResponse.text());
                      }
                      break;
                  case 404:
                      _this.addErrorAlert('Not found', 'error.url.not.found');
                      break;
                  default:
                      if (httpResponse.text() !== '' && httpResponse.json() && httpResponse.json().message) {
                          _this.addErrorAlert(httpResponse.json().message);
                      }
                      else {
                          _this.addErrorAlert(httpResponse.text());
                      }
              }
          });
      }
      JhiAlertErrorComponent.prototype.ngOnDestroy = function () {
          if (this.cleanHttpErrorListener !== undefined && this.cleanHttpErrorListener !== null) {
              this.eventManager.destroy(this.cleanHttpErrorListener);
              this.alerts = [];
          }
      };
      JhiAlertErrorComponent.prototype.addErrorAlert = function (message, key, data) {
          this.alerts.push(this.alertService.addAlert({
              type: 'danger',
              msg: message,
              timeout: 5000,
              toast: this.alertService.isToast(),
              scoped: true
          }, this.alerts));
      };
      JhiAlertErrorComponent = __decorate([
          core_1.Component({
              selector: 'jhi-alert-error',
              template: "\n        <div class=\"alerts\" role=\"alert\">\n            <div *ngFor=\"let alert of alerts\"  [ngClass]=\"{'alert.position': true, 'toast': alert.toast}\">\n                <ngb-alert *ngIf=\"alert && alert.type && alert.msg\" [type]=\"alert.type\" (close)=\"alert.close(alerts)\">\n                    <pre [innerHTML]=\"alert.msg\"></pre>\n                </ngb-alert>\n            </div>\n        </div>"
          }),
          __metadata("design:paramtypes", [ng_jhipster_1.JhiAlertService, ng_jhipster_1.JhiEventManager])
      ], JhiAlertErrorComponent);
      return JhiAlertErrorComponent;
  }());
  exports.JhiAlertErrorComponent = JhiAlertErrorComponent;
  //# sourceMappingURL=alert-error.component.js.map