export function parseExpiration(token: string): number {
  const parts = token.split('.')
  if (parts.length !== 3) return 0
  const payload = JSON.parse(atob(parts[1]))
  return payload.exp
}

export function secondsUntilExpired(token: string): number {
  const exp = parseExpiration(token)
  const now = Date.now() / 1000
  return exp - now
}

export function isExpired(token: string): boolean {
  return secondsUntilExpired(token) <= 0
}

export function parseSubject(token: string): string {
  const parts = token.split('.')
  if (parts.length !== 3) return ''
  const payload = JSON.parse(atob(parts[1]))
  return payload.sub
}
