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

import { Profile } from 'src/app/core/models';
import { ProfilesService } from 'src/app/core/services';
import { catchError } from 'rxjs/operators';

@Injectable()
export class ProfileResolver implements Resolve<Profile> {
    constructor(
        private profilesService: ProfilesService,
        private router: Router
    ) { }

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

        return this.profilesService.get(route.params['username'])
            .pipe(catchError(() => this.router.navigateByUrl('/')));

    }
}
