/**
 * Generates a deterministic UUID v5 from a namespace and name.
 * Uses SHA-1 hashing per RFC 4122 §4.3.
 */
export async function uuidv5(namespace: string, name: string): Promise<string> {
  const encoder = new TextEncoder();
  const data = new Uint8Array([...encoder.encode(namespace), ...encoder.encode(name)]);
  const hash = new Uint8Array(await globalThis.crypto.subtle.digest("SHA-1", data));

  // Set version 5 (bits 4-7 of byte 6)
  hash[6] = (hash[6] & 0x0f) | 0x50;
  // Set variant (bits 6-7 of byte 8)
  hash[8] = (hash[8] & 0x3f) | 0x80;

  const hex = Array.from(hash.slice(0, 16), (b) => b.toString(16).padStart(2, "0")).join("");
  return [
    hex.slice(0, 8),
    hex.slice(8, 12),
    hex.slice(12, 16),
    hex.slice(16, 20),
    hex.slice(20, 32),
  ].join("-");
}
