import { Injectable, } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';

import { Jump } from 'src/app/core/models';
import { JumpsService } from 'src/app/core/services';
import { catchError } from 'rxjs/operators';

@Injectable()
export class JumpResolver implements Resolve<Jump> {
    constructor(
        private _jumpsService: JumpsService,
        private router: Router
    ) { }

    resolve(
        route: ActivatedRouteSnapshot,
        state: RouterStateSnapshot
    ): Observable<any> {

        return this._jumpsService.get(route.params['slug'])
            .pipe(catchError(() => this.router.navigateByUrl('/')));
    }
}
