import { Injectable } from '@angular/core';
import { OrderService, VATType, VATDTO } from '@swagger/oms';
import { map } from 'rxjs/operators';
import { BehaviorSubject, ReplaySubject } from 'rxjs';

@Injectable({ providedIn: 'root' })
export class VatService {
  private getVatsSubject: ReplaySubject<VATDTO[]>;

  constructor(private orderService: OrderService) {}

  getVats() {
    if (!this.getVatsSubject) {
      this.getVatsSubject = new ReplaySubject();

      this.orderService
        .OrderGetVATs({})
        .pipe(map((response) => response.result))
        .subscribe(this.getVatsSubject);
    }

    return this.getVatsSubject.asObservable();
  }
}
