/**
 * Pure JWT auth helpers (no axios) so unit tests avoid ESM transform issues.
 */

/**
 * Decode JWT payload without verifying signature (client-side checks only).
 * @param {string} token
 * @returns {{ exp?: number } | null}
 */
export function decodeJwtPayload(token) {
  try {
    const parts = token.split(".");
    if (parts.length < 2) {
      return null;
    }
    const payload = parts[1].replace(/-/g, "+").replace(/_/g, "/");
    const padded = payload + "=".repeat((4 - (payload.length % 4)) % 4);
    return JSON.parse(atob(padded));
  } catch {
    return null;
  }
}

/**
 * @param {{ response?: { status?: number, data?: { code?: string } } }} error
 * @returns {boolean}
 */
export function isTokenNotValidError(error) {
  const status = error.response?.status;
  const code = error.response?.data?.code;
  return status === 401 && code === "token_not_valid";
}
