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);
};
import { throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { AuthService } from '../auth';
import { HttpError } from '../../domain';
import { AbstractAppConfig } from '../../../app.config';
import { Injectable } from '@angular/core';
var HttpErrorInterceptor = /** @class */ (function () {
    function HttpErrorInterceptor(appConfig, authService) {
        this.appConfig = appConfig;
        this.authService = authService;
    }
    HttpErrorInterceptor_1 = HttpErrorInterceptor;
    HttpErrorInterceptor.prototype.intercept = function (request, next) {
        var _this = this;
        return next.handle(request)
            .pipe(catchError(function (error) {
            var httpError = new HttpError();
            if (_this.isNetworkError(error)) {
                httpError = _this.processNetworkError();
            }
            else if (error) {
                httpError = _this.processBackendError(error);
            }
            if (_this.doRedirect(error) && (httpError.status === 401 || httpError.status === 403)) {
                _this.authService.signIn();
            }
            return throwError(httpError);
        }));
    };
    HttpErrorInterceptor.prototype.isNetworkError = function (error) {
        return error && error.error instanceof ProgressEvent && error.error.type === 'error';
    };
    HttpErrorInterceptor.prototype.processNetworkError = function () {
        // A client-side or network error occurred. Handle it accordingly.
        var httpError = new HttpError();
        httpError.message = HttpErrorInterceptor_1.GENERIC_ERROR_MSG;
        httpError.status = null;
        console.error(HttpErrorInterceptor_1.GENERIC_ERROR_MSG);
        return httpError;
    };
    HttpErrorInterceptor.prototype.processBackendError = function (error) {
        // The backend returned an unsuccessful response code.
        // The response body may contain clues as to what went wrong,
        var httpError = new HttpError();
        if (error.headers
            && error.headers.get(HttpErrorInterceptor_1.CONTENT_TYPE)
            && error.headers.get(HttpErrorInterceptor_1.CONTENT_TYPE).indexOf(HttpErrorInterceptor_1.JSON) !== -1) {
            httpError = HttpError.from(error.error || {});
        }
        if (!httpError.status && error.status) {
            httpError.status = error.status;
        }
        console.error("Backend returned code " + error.status + ", body was: " + JSON.stringify(error.error));
        return httpError;
    };
    HttpErrorInterceptor.prototype.doRedirect = function (error) {
        if (error && error.url) {
            var activityUrl = this.appConfig.getActivityUrl();
            var postcodeUrl = this.appConfig.getPostcodeLookupUrl();
            if (activityUrl && activityUrl !== '' && error.url.startsWith(activityUrl)) {
                return false;
            }
            if (postcodeUrl && postcodeUrl !== '' && error.url.startsWith(postcodeUrl)) {
                return false;
            }
        }
        return true;
    };
    var HttpErrorInterceptor_1;
    HttpErrorInterceptor.GENERIC_ERROR_MSG = 'A network error occurred while making a request';
    HttpErrorInterceptor.CONTENT_TYPE = 'Content-Type';
    HttpErrorInterceptor.JSON = 'json';
    HttpErrorInterceptor = HttpErrorInterceptor_1 = __decorate([
        Injectable(),
        __metadata("design:paramtypes", [AbstractAppConfig, AuthService])
    ], HttpErrorInterceptor);
    return HttpErrorInterceptor;
}());
export { HttpErrorInterceptor };
//# sourceMappingURL=http-error.interceptor.js.map