export const ACCESS_TOKEN_STORAGE_KEY = 'fleet-ble-monitor.accessToken'
export const REFRESH_TOKEN_STORAGE_KEY = 'fleet-ble-monitor.refreshToken'
export const CURRENT_USER_STORAGE_KEY = 'fleet-ble-monitor.currentUser'
export const LEGACY_CURRENT_USER_ID_STORAGE_KEY =
  'fleet-ble-monitor.currentUserId'

export function getStoredAccessToken() {
  if (typeof window === 'undefined') return null

  return window.localStorage.getItem(ACCESS_TOKEN_STORAGE_KEY)
}

export function getStoredRefreshToken() {
  if (typeof window === 'undefined') return null

  return window.localStorage.getItem(REFRESH_TOKEN_STORAGE_KEY)
}

export function saveAuthTokens(accessToken: string, refreshToken: string) {
  if (typeof window === 'undefined') return

  window.localStorage.setItem(ACCESS_TOKEN_STORAGE_KEY, accessToken)
  window.localStorage.setItem(REFRESH_TOKEN_STORAGE_KEY, refreshToken)
  window.localStorage.removeItem(LEGACY_CURRENT_USER_ID_STORAGE_KEY)
}

export function clearAuthStorage() {
  if (typeof window === 'undefined') return

  window.localStorage.removeItem(ACCESS_TOKEN_STORAGE_KEY)
  window.localStorage.removeItem(REFRESH_TOKEN_STORAGE_KEY)
  window.localStorage.removeItem(CURRENT_USER_STORAGE_KEY)
  window.localStorage.removeItem(LEGACY_CURRENT_USER_ID_STORAGE_KEY)
}
