import { Injectable } from '@angular/core'
import { Router, Resolve, RouterStateSnapshot, ActivatedRouteSnapshot } from '@angular/router'
import { Observable, of, EMPTY, forkJoin } from 'rxjs'
import { map, mergeMap, take, first } from 'rxjs/operators'

import { InfoDataService, FullInfo } from './info-data.service'

@Injectable()
export class InfoResolver implements Resolve<object> {
    constructor(
        private infoDataService: InfoDataService,
        private router: Router
    ) {}

    resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<FullInfo> {
        return forkJoin([
            this.infoDataService.getModelInfo(
                route.paramMap.get('type'),
            ),
            this.infoDataService.getFeatureInfo(
                route.paramMap.get('type'),
                route.paramMap.get('id'),
            )
        ]).pipe(map(
            res => new FullInfo(res[0], res[1])
        ))
    }
}
