import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { ICartItemModel } from '@fleio-api/billing/cart/model/cart-items.model';
import { CartItemsApiService } from '@fleio-api/billing/cart/cart-items-api.service';

@Injectable({
  providedIn: 'root'
})
export class CartItemResolver  {
  constructor(private cartItemsApiService: CartItemsApiService) {
  }

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