import { Injectable } from '@angular/core';
import { HttpClient, HttpRequest } from '@angular/common/http';
import { JwtHelperService } from '@auth0/angular-jwt';
import { Location } from '@angular/common';


@Injectable({
  providedIn: 'root'
})
export class SharedService {

  constructor(
    private http : HttpClient,
    private location: Location,
    
  
  ) { }

  pathApi = 'http://hospital.rabbittec.com/api';
  dataLoginUser = {};
  helper = new JwtHelperService();
  cachedRequests: Array<HttpRequest<any>> = [];

  onSignIn(form){
      console.log('from service', form);
      return this.http.post(this.pathApi + '/auth/login', form);
  }
   //return data user function
   setDataUser(user) {
    this.dataLoginUser = user.user;
    console.log('data loooogin' , this.dataLoginUser);
  }

  public getToken(): string {
    return localStorage.getItem('token');
  }

  public isAuthenticated(): boolean {
    const token = this.getToken();

    if(token) 
      return this.helper.isTokenExpired(token);
    
    return true;  
  }


  public collectFailedRequest(request): void {
    this.cachedRequests.push(request);
  }

}

