import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor, HttpInterceptorFn, HttpHandlerFn } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Utils } from './utils';
/*
@Injectable()
export class JwtInterceptor implements HttpInterceptor {
  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    // add authorization header with jwt token if available
    const currentUser = Utils.getCurrentUser();//localStorage.getItem('currentUser')!;    
    console.log('I am interceptor debug for currentUser obj', currentUser);
    if (currentUser && currentUser.token) {
      //console.log('I am interceptor add authorization header with jwt token');
      request = request.clone({
        setHeaders: {
          Authorization: `Bearer ${currentUser.token}`
        }
      });
    }
    return next.handle(request);
  }
}

/*
new one 
////////////////
*/
export const JwtInterceptor: HttpInterceptorFn = (request: HttpRequest<unknown>, next: HttpHandlerFn): Observable<HttpEvent<unknown>> => {
  //const logger = inject(Logger);
  //logger.log(`Request is on its way to ${req.url}`);
  const currentUser = Utils.getCurrentUser();//localStorage.getItem('currentUser')!;    
 // console.log('I am interceptor debug for currentUser obj', currentUser);
  if (currentUser && currentUser.token && currentUser.token != "") {
   // console.log('I am interceptor add authorization header with jwt token');
    request = request.clone({
      setHeaders: {
        Authorization: `Bearer ${currentUser.token}`
      }
    });
  }
  return next(request);
}

////////////////
/*
providers: [provideHttpClient(withInterceptors([loggerInterceptor]))] 
*/