import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { IListQueryParams } from '@fleio-api/base-model/list-query-params';
import { catchError } from 'rxjs/operators';
import { ICartItemsModel } from '@fleio-api/billing/cart/model/cart-item.model';
import { CartApiService } from '@fleio-api/billing/cart/cart-api.service';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';

@Injectable({
  providedIn: 'root'
})
export class CartItemsListResolver  {
  constructor(private cartApiService: CartApiService) {
  }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
    Observable<FleioObjectsList<ICartItemsModel>> | Promise<FleioObjectsList<ICartItemsModel>> |
    FleioObjectsList<ICartItemsModel> {
    return this.cartApiService.list(
      route.queryParams as IListQueryParams
    ).pipe(catchError(() => of(null)));
  }
}
