import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
import jwt_decode from 'jwt-decode';

@Injectable({
  providedIn: 'root'
})
export class TokenService {
  tokenKey = environment.tokenKey;

  constructor() { }

  public getToken(): string | null {
    const token = localStorage.getItem(this.tokenKey);
    if(token) {
      return token;
    } else {
      return null;
    }
  }

  public getCurrentUserId(): number | null {
    const token = this.getToken();
    if(token) {
      const decodedToken = jwt_decode<any>(token);
      const userId = decodedToken.userId;
      console.log(userId);
      
      return userId;
    } else {
      return null;
    }
  }


  public destroyToken(tokenKey : string): void { 
    localStorage.removeItem(tokenKey);
    localStorage.removeItem('ROLE');
  }
}
