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 OrderOptionsResolver  {
  constructor(private cartItemsApiService: CartItemsApiService) {
  }

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