const TOKEN_KEY = 'craftshop_auth_token'

export function readStoredToken(): string | null {
  try {
    return localStorage.getItem(TOKEN_KEY)
  } catch (err) {
    console.warn('[persist-token] Failed to read from localStorage', err)
    return null
  }
}

export function persistToken(token: string | null): void {
  try {
    if (!token) localStorage.removeItem(TOKEN_KEY)
    else localStorage.setItem(TOKEN_KEY, token)
  } catch (err) {
    console.warn('[persist-token] Failed to write to localStorage', err)
  }
}

export function removeStoredToken(): void {
  try {
    localStorage.removeItem(TOKEN_KEY)
  } catch (err) {
    console.warn('[persist-token] Failed to remove from localStorage', err)
  }
}
