import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from "@angular/common/http";
import {HttpSecurityErrorInterceptor, MessagesService, UserService} from "@webbpm/base-package";
import {Injectable} from "@angular/core";
import {Router} from "@angular/router";
import {EMPTY, Observable} from "rxjs";
import {catchError} from "rxjs/operators";

@Injectable()
export class DevHttpSecurityErrorInterceptor extends HttpSecurityErrorInterceptor
    implements HttpInterceptor {
  private router: Router;


  constructor(router: Router, messagesService: MessagesService, userService: UserService) {
    super(router, messagesService, userService);
    this.router = router;
  }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    if (window.location.hash.includes("webbpm-preview")) {
      return next.handle(req).pipe(catchError(() => {
                                                return EMPTY;
                                              }
      ));
    }
    else {
      return super.intercept(req, next);
    }
  }
}
