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

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

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