import { HttpEvent, HttpInterceptorFn, HttpRequest, HttpHandlerFn } from "@angular/common/http";
import { inject } from "@angular/core";
import { Observable, finalize } from "rxjs";
import { LoaderService } from "../services/loader.service";

export const loadingInterceptor: HttpInterceptorFn = (
  req: HttpRequest<unknown>,
  next: HttpHandlerFn
): Observable<HttpEvent<unknown>> => {

  const loader = inject(LoaderService);

  const isRefresh = req.url.includes('/auth/RefreshToken');
  if (!isRefresh) {
    loader.increase();
  }

  return next(req).pipe(

    // catchError(err => {
    //   if (!isRefresh) loader.decrease();
    //   throw err;
    // }),

    finalize(() => {
      if (!isRefresh) loader.decrease();
    })
  );
};
