// Ids for rooms, tables, guests and groups: a readable prefix and twelve
// random characters ("guest_k3v8m2pq7xzt"). Random rather than counted so
// that two computers working on the same saved project can never mint the
// same id — the per-browser counters this replaced would have. Chair ids
// are derived from their table's id (chairs.ts). Projects from before keep
// their counter ids ("guest_0"); nothing reads anything into the format
// beyond persistence.ts's isSafeId.

// 32 characters, so a random byte maps onto one without modulo bias; the
// easily confused l, o, 0 and 1 are left out.
const ID_ALPHABET = "abcdefghijkmnpqrstuvwxyz23456789";
const ID_RANDOM_LENGTH = 12;

const mintId = <T extends string>(prefix: string): T => {
  const bytes = new Uint8Array(ID_RANDOM_LENGTH);
  crypto.getRandomValues(bytes);
  let id = prefix + "_";
  for (const byte of bytes) id += ID_ALPHABET[byte % ID_ALPHABET.length];
  return id as T;
};
