const TOKEN = 'accessToken'
const REFRESH_TOKEN = 'refreshToken'
const USER_INFO = 'userInfo'
const STUDENT_REGISTRATION_INFO = 'StudentRegistration'

export const tokenService = {
  getToken() {
    return localStorage.getItem(TOKEN)
  },
  saveToken(token) {
    if (token) localStorage.setItem(TOKEN, token)
  },
  removeToken() {
    localStorage.removeItem(TOKEN)
  },

  getRefreshToken() {
    return localStorage.getItem(REFRESH_TOKEN)
  },
  saveRefreshToken(newToken) {
    if (newToken) localStorage.setItem(REFRESH_TOKEN, newToken)
  },
  removeRefreshToken() {
    localStorage.removeItem(REFRESH_TOKEN)
  },

  getUserInfo() {
    const info = localStorage.getItem(USER_INFO)
    return info ? JSON.parse(info) : null
  },
  saveUserInfo(info) {
    if (info) localStorage.setItem(USER_INFO, JSON.stringify(info))
  },
  removeUserInfo() {
    localStorage.removeItem(USER_INFO)
    localStorage.removeItem(STUDENT_REGISTRATION_INFO)
  },

  hasToken() {
    return !!this.getToken()
  },

  clearAll() {
    this.removeToken()
    this.removeRefreshToken()
    this.removeUserInfo()
  },
}
