import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {AppConfig} from '../app.config';
import {BehaviorSubject, Observable} from 'rxjs';
import {tap} from 'rxjs/operators';

@Injectable()
export class LenderNameService extends BehaviorSubject<string[]> {
  public loading = false;
  constructor( private http: HttpClient, private config: AppConfig) {
    super(null);
  }

  public query(): void {
    this.fetch().subscribe( x => super.next(x));
  }

  private fetch(): Observable<string[]>{
    this.loading = true;
    return this.http.get<string[]>(this.config.getUrl('lender-list/')).pipe(
      tap(() => this.loading = false)
    );
  }
}
