import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { environment as env } from '../environments/environment';
import { AuthService } from './services/auth.service';

@Injectable()
export class AppInterceptor implements HttpInterceptor {
  constructor(private readonly auth: AuthService) {
    this.baseUrl = env.baseUrl;
  }
  private readonly baseUrl: string;

  intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
    let url = `${this.baseUrl}${request.url}`;
    const token = this.auth.getToken();
    if (!token) return next.handle(request.clone({ url }));

    return next.handle(
      request.clone({
        url,
        setHeaders: { Authorization: token },
      })
    );
  }
}
