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 { Injectable } from '@angular/core';
import { throwError } from 'rxjs';
import { Response } from '@angular/http';
import { HttpError } from '../../domain/http/http-error.model';
import { AuthService } from '../auth';
var HttpErrorService = /** @class */ (function () {
    function HttpErrorService(authService) {
        this.authService = authService;
    }
    HttpErrorService_1 = HttpErrorService;
    HttpErrorService.prototype.setError = function (error) {
        this.error = error;
    };
    HttpErrorService.prototype.removeError = function () {
        var error = this.error;
        this.error = null;
        return error;
    };
    HttpErrorService.prototype.handle = function (error, redirectIfNotAuthorised) {
        if (redirectIfNotAuthorised === void 0) { redirectIfNotAuthorised = true; }
        var httpError = new HttpError();
        if (error instanceof Response) {
            if (error.headers
                && error.headers.get(HttpErrorService_1.CONTENT_TYPE)
                && error.headers.get(HttpErrorService_1.CONTENT_TYPE).indexOf(HttpErrorService_1.JSON) !== -1) {
                try {
                    httpError = HttpError.from(error.json() || {});
                }
                catch (e) {
                    console.error(e, e.message);
                }
            }
            if (!httpError.status) {
                httpError.status = error.status;
            }
        }
        else if (error) {
            if (error.message) {
                httpError.message = error.message;
            }
            if (error.status) {
                httpError.status = error.status;
            }
        }
        if (redirectIfNotAuthorised && (httpError.status === 401 || httpError.status === 403)) {
            this.authService.signIn();
        }
        return throwError(httpError);
    };
    var HttpErrorService_1;
    HttpErrorService.CONTENT_TYPE = 'Content-Type';
    HttpErrorService.JSON = 'json';
    HttpErrorService = HttpErrorService_1 = __decorate([
        Injectable(),
        __metadata("design:paramtypes", [AuthService])
    ], HttpErrorService);
    return HttpErrorService;
}());
export { HttpErrorService };
//# sourceMappingURL=http-error.service.js.map