import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { InvoicesApiService } from './invoices-api.service';
import { catchError } from 'rxjs/operators';
import { IInvoiceCreateOptionsModel } from '../model/invoice-create-options.model';

@Injectable({
  providedIn: 'root'
})
export class InvoiceCreateOptionsResolver  {
  constructor(private invoicesApi: InvoicesApiService) {
  }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<IInvoiceCreateOptionsModel> | Promise<IInvoiceCreateOptionsModel> | IInvoiceCreateOptionsModel {
    const createOpt = this.invoicesApi.createOptions() as Observable<IInvoiceCreateOptionsModel>;
    return createOpt.pipe(catchError(() => of(null)));
  }
}
