import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve } from '@angular/router';

import { Observable } from 'rxjs/Observable';

import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';

import { TranslateService } from '@ngx-translate/core';

@Injectable()
export class LanguageResolve implements Resolve<{ [name: string]: string }> {

    constructor(private translateService: TranslateService) { }

    resolve(route: ActivatedRouteSnapshot): Observable<{ [name: string]: string }> {
        let languageParamName: string = 'lang';
        return this.translateService.use(route.params[languageParamName])
            .catch(er => {
                console.log('err in resolve', er);
                return Observable.throw('missed lang');
            });

    }
}
