import { Injectable } from '@angular/core';
import { map } from 'rxjs/operators';
import { ListResponseArgsOfOrderListItemDTO, ResponseArgsOfOrderDTO, OrderDTO, OrderListItemDTO } from '@swagger/oms';
import { Observable } from 'rxjs';
import { OrderService } from '@swagger/oms';

@Injectable({
  providedIn: 'root',
})
export class OrdersService {
  constructor(private orderService: OrderService) {}

  getCustomerOrders(customerId: string): Observable<OrderListItemDTO[]> {
    const params: OrderService.OrderGetOrdersByBuyerNumberParams = {
      buyerNumber: customerId,
      skip: 0,
      take: 99999,
    };

    return this.orderService.OrderGetOrdersByBuyerNumber(params).pipe(
      map((response: ListResponseArgsOfOrderListItemDTO) => {
        if (response.error) {
          throw new Error(response.message);
        }

        return response.result;
      })
    );
  }

  getOrder(orderId: number): Observable<OrderDTO> {
    return this.orderService.OrderGetOrder(orderId).pipe(
      map((response: ResponseArgsOfOrderDTO) => {
        if (response.error) {
          throw new Error(response.message);
        }

        return response.result;
      })
    );
  }
}
