/**
 * Whether a visitor on a sign-in screen should be sent home.
 *
 * A function in its own `.ts` file, like `routeAccess`, so the rule can be
 * asserted without mounting anything — `npm test` runs the source through
 * Node's type stripper, which handles `.ts` and not `.tsx`.
 */
export interface AuthRedirectRequest {
  /** False while the render is still working from the server snapshot. */
  sessionReady: boolean;
  /** Whether anyone is signed in at all. */
  signedIn: boolean;
  /** BE-129 — the reader is here to sign a *second* account in. */
  addingAccount: boolean;
}

/**
 * Three states, not two: "not known yet" is distinct from "signed out".
 *
 * Acting before the session is known would bounce a signed-*out* visitor off
 * the form they asked for, which is the mirror of the defect
 * `useSessionReady` was added for on the other guard.
 */
export function shouldRedirectWhenLoggedIn({
  sessionReady,
  signedIn,
  addingAccount,
}: AuthRedirectRequest): boolean {
  if (!sessionReady) return false;
  if (addingAccount) return false;
  return signedIn;
}
