import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from "@angular/common/http";
import {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";
import {ErvuHttpSecurityErrorInterceptor} from "./ervu-http-security-error-interceptor";
import {AuthenticationService} from "../../security/authentication.service";

@Injectable()
export class DevHttpSecurityErrorInterceptor extends ErvuHttpSecurityErrorInterceptor
    implements HttpInterceptor {


  constructor(router: Router, messagesService: MessagesService, userService: UserService,
              authService: AuthenticationService) {
    super(router, messagesService, userService, authService);
  }

  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);
    }
  }
}
