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

@Injectable()
export class TokenInterceptor implements HttpInterceptor {

  constructor() {}

  // intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<unknown>> {
  //   return next.handle(request);
  // }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    // const tokenUrl = 'http://example.com/token'; // replace with your token url
    const token = localStorage.getItem('user_token'); 
    let authReq:any; // get the token from local storage
    if(token != '' && token != undefined && token != null){
      authReq = req.clone({
        setHeaders: {
          // 'Authorization': environment.authorization,
          'x-access-token': token
        }
        // headers: req.headers.set('x-access-token', token).set('Content-Type', 'multipart/form-data')
      });
    }else{
      authReq = req;
    }
   

    return next.handle(authReq);
  }

}
