import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpProgressEvent, HttpEventType, HttpResponse } from '@angular/common/http';
import { Observable, of, concat } from 'rxjs';
import {AuthService} from '../service/auth.service';
import {delay, tap} from 'rxjs/operators';

/*
  Mocked backend service.
  For further details, check
  https://angular.io/guide/http#writing-an-interceptor
*/

@Injectable()
export class UploadInterceptor implements HttpInterceptor {
  constructor(private auth: AuthService){}
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    if (req.url === this.auth.getUrl('lender-upload')) {
      return next.handle(req).pipe(
        tap(event => {
          // console.log(event);
          if (event.type === HttpEventType.UploadProgress){
            console.log('progress', event);
          }
        })
      );
    }

    if (req.url === this.auth.getUrl('lender-upload-remove')) {
      // ok, not processng anything
      console.log('lender-upload-remove');
    }
    return next.handle(req);
  }
}
