/**
 * FedEx OAuth token cache.
 *
 * Lives outside any `"use server"` file because FedEx auth tokens are a
 * process-wide singleton (one FedEx OAuth client per Node.js process), not
 * per-request state. Imported by the three shipping action files that need
 * to call FedEx APIs without minting a fresh token for every request.
 */
type FedExAuthToken = {
  accessToken: string;
  expiresAt: number;
};

let cachedToken: FedExAuthToken | null = null;

export function readFedExToken(): FedExAuthToken | null {
  return cachedToken;
}

export function writeFedExToken(accessToken: string, expiresInSeconds: number): void {
  cachedToken = {
    accessToken,
    expiresAt: Date.now() + expiresInSeconds * 1000,
  };
}

export function clearFedExToken(): void {
  cachedToken = null;
}