import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { CoreApiService } from '@fleio-api/core/core-api.service';
import { IClientAutoInvoicingSettingsModel } from '@fleio-api/client-user/model/client-auto-invoicing-settings.model';

@Injectable({
  providedIn: 'root'
})
export class ClientAutoInvoicingSettingsResolver  {
  constructor(private coreApiService: CoreApiService) {
  }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<IClientAutoInvoicingSettingsModel> | Promise<IClientAutoInvoicingSettingsModel> |
    IClientAutoInvoicingSettingsModel {
    return this.coreApiService.getCreditAutoInvoicingSettings(
      route.params.id
    ).pipe(catchError(() => of(null)));
  }
}
