import { Injectable, inject } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor, HttpInterceptorFn, HttpHandlerFn } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';

import { AuthenticationService } from '../user-services';
//import { ToastrService } from 'ngx-toastr';
import { NavigationExtras, Router } from '@angular/router';
/*
@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
  //private authenticationService: AuthenticationService;
  constructor(
    private router: Router,
    //   private toastr: ToastrService,
  ) { }

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    // console.log("I am intercept to catch error status = 401...", request);
    return next.handle(request).pipe(catchError(err => {
      /* no use yet
      if (err) {
        switch(err.status) {
          case 400: 
          if (err.error.errors) {
            const modalStateErrors = [];
            for (const key in err.error.errors) {
              if (err.error.errors[key]) {
                modalStateErrors.push(err.error.errors[key]);                
              }
            }
            throw modalStateErrors;
          } else {
            this.toastr.error(err.statusText, err.status);
          }
          break;
          case 401:
            this.toastr.error(err.statusText, err.status);
            break;
          case 404:
            this.router.navigateByUrl('/not-found');
            break;
          case 500: 
            const navigationExtras: NavigationExtras = {state:{error:err.error}};
            this.router.navigateByUrl('/server-error', navigationExtras);
            break;
          default:
            this.toastr.error('Something unexpected went wrong');
            console.log("this error interceptor",err);
            break;
        }
      }
     */
/*
    if (err.status === 401) {
      // auto logout if 401 response returned from api  Unauthorized 
      //   this.authenticationService.logout();
      //location.reload();
      const error = err.error.message || err.statusText;
      console.log("After I Get Error status = 401...", error, err);
      return throwError(error);
    }
    else {
      console.log("I Get Error .", err);
      return throwError(err);
    }
  }))
}
}

*/
//new one 
//https://blog.ninja-squad.com/2022/11/09/angular-http-in-standalone-applications/
////////////////
export const ErrorInterceptor: HttpInterceptorFn = (req: HttpRequest<unknown>, next: HttpHandlerFn): Observable<HttpEvent<unknown>> => {
  const authenticationService = inject(AuthenticationService);

  // logger.log(`Request is on its way to ${req.url}`);
  return next(req).pipe(catchError(err => {
    if (err.status === 401) {
      // auto logout if 401 response returned from api  Unauthorized 
      //   this.authenticationService.logout();
      //location.reload();
      const error = err.error.message || err.statusText;
     // console.log("After I Get Error status = 401...", error, err);
      return throwError(() => error);
    }
    else {
      console.log("I Get Error .", err);
      return throwError(() => err);
    }

  }));
}

////////////////

