import { HttpInterceptor, HttpEvent, HttpRequest, HttpHandler } from '@angular/common/http';
import { Observable } from 'rxjs';
import { SsoService } from 'sso';
import { Injectable } from '@angular/core';

@Injectable()
export class SsoAuthorizationInterceptor implements HttpInterceptor {
  constructor(private ssoService: SsoService) {}
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    if (req.headers.has('SKIP_SSO_AUTHORIZATION_INTERCEPTOR')) {
      return next.handle(req);
    }

    const token = this.ssoService.getToken();
    const headers = req.headers.set('Authorization', `Bearer ${token}`);
    const request = req.clone({ headers });
    return next.handle(request);
  }
}
