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 { NavigationEnd, Router } from '@angular/router';
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { catchError, map, filter } from 'rxjs/operators';
import { CaseView, Draft } from '../../../domain';
import { CaseService, CasesService } from '../../case-editor';
import { AlertService, DraftService } from '../../../services';
import { plainToClassFromExist } from 'class-transformer';
var CaseResolver = /** @class */ (function () {
    function CaseResolver(caseService, casesService, draftService, router, alertService) {
        var _this = this;
        this.caseService = caseService;
        this.casesService = casesService;
        this.draftService = draftService;
        this.router = router;
        this.alertService = alertService;
        router.events.pipe(filter(function (event) { return event instanceof NavigationEnd; }))
            .subscribe(function (event) {
            _this.previousUrl = event.url;
        });
    }
    CaseResolver_1 = CaseResolver;
    CaseResolver.prototype.resolve = function (route) {
        var cid = route.paramMap.get(CaseResolver_1.PARAM_CASE_ID);
        if (!cid) {
            // when redirected to case view after a case created, and the user has no READ access,
            // the post returns no id
            this.navigateToCaseList();
        }
        else {
            return this.isRootCaseViewRoute(route) ? this.getAndCacheCaseView(cid)
                : this.cachedCaseView ? Promise.resolve(this.cachedCaseView)
                    : this.getAndCacheCaseView(cid);
        }
    };
    CaseResolver.prototype.navigateToCaseList = function () {
        var _this = this;
        this.router.navigate(['/list/case'])
            .then(function () { return _this.alertService.success(CaseResolver_1.CASE_CREATED_MSG); });
    };
    CaseResolver.prototype.isRootCaseViewRoute = function (route) {
        // is route case/:cid
        return ((!route.firstChild || !route.firstChild.url.length) && !this.isTabViewRoute(route));
    };
    CaseResolver.prototype.isTabViewRoute = function (route) {
        // is route case/:cid#fragment
        return route.firstChild && route.firstChild.fragment;
    };
    CaseResolver.prototype.getAndCacheCaseView = function (cid) {
        var _this = this;
        if (Draft.isDraft(cid)) {
            return this.getAndCacheDraft(cid);
        }
        else {
            return this.casesService
                .getCaseView(cid)
                .pipe(map(function (caseView) {
                _this.cachedCaseView = plainToClassFromExist(new CaseView(), caseView);
                _this.caseService.announceCase(_this.cachedCaseView);
                return _this.cachedCaseView;
            }), catchError(function (error) { return _this.checkAuthorizationError(error); })).toPromise();
        }
    };
    CaseResolver.prototype.getAndCacheDraft = function (cid) {
        var _this = this;
        return this.draftService
            .getDraft(cid)
            .pipe(map(function (caseView) {
            _this.cachedCaseView = plainToClassFromExist(new CaseView(), caseView);
            _this.caseService.announceCase(_this.cachedCaseView);
            return _this.cachedCaseView;
        }), catchError(function (error) { return _this.checkAuthorizationError(error); })).toPromise();
    };
    CaseResolver.prototype.checkAuthorizationError = function (error) {
        // TODO Should be logged to remote logging infrastructure
        console.error(error);
        if (CaseResolver_1.EVENT_REGEX.test(this.previousUrl) && error.status === 404) {
            this.router.navigate(['/list/case']);
            return of(null);
        }
        if (error.status !== 401 && error.status !== 403) {
            this.router.navigate(['/error']);
        }
        return Observable.throw(error);
    };
    var CaseResolver_1;
    CaseResolver.EVENT_REGEX = new RegExp('\/trigger\/.*?\/submit$');
    CaseResolver.PARAM_CASE_ID = 'cid';
    CaseResolver.CASE_CREATED_MSG = 'The case has been created successfully';
    CaseResolver = CaseResolver_1 = __decorate([
        Injectable(),
        __metadata("design:paramtypes", [CaseService,
            CasesService,
            DraftService,
            Router,
            AlertService])
    ], CaseResolver);
    return CaseResolver;
}());
export { CaseResolver };
//# sourceMappingURL=case.resolver.js.map