import { USERNAME_PATTERN } from "@/features/shared/nicknameAvailability";

/**
 * Whether the account may still choose its username, mirroring the backend's
 * `Username::mayClaim`: only while the stored value is empty or breaks the
 * username rule (old accounts whose name was generated from an e-mail, with
 * a `.`, `-` or `+`). A well-formed username is fixed (PDF pp.8–9, decision 9).
 */
export function mayClaimUsername(username: string | null | undefined): boolean {
  return !USERNAME_PATTERN.test(String(username ?? ""));
}

/** The one value `POST /account/` will take as a claim. */
export function isClaimableUsername(value: string): boolean {
  return USERNAME_PATTERN.test(value.trim());
}
