import { Injectable } from '@angular/core';
import {
  Router, Resolve,
  RouterStateSnapshot,
  ActivatedRouteSnapshot
} from '@angular/router';
import { Package } from "../../api/models/package";
import { map, Observable, of } from "rxjs";
import { PkgdashService } from '../../api/services/pkgdash.service';


@Injectable({
  providedIn: 'root'
})

export class PackageResolver implements Resolve<Package> {

  constructor(private pkgdashService: PkgdashService) { }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Package> {
    console.log("resolver with "+ route.paramMap.get('id'));
    let id = Number(route.paramMap.get('id'));

    return this.pkgdashService.packageLookup({ id: id }).pipe(
      map(rsp => rsp.package!)
    );
  }
}
