// @ts-expect-error — no published types, local .d.ts provides them
import MurmurHash3 from "murmurhash3js-revisited"

/**
 * Deterministic 6-char Base64URL hash from content + position.
 * Same inputs always produce the same ID, regardless of caller.
 * Used by both the indexer and the editor so block IDs remain
 * consistent across processes.
 */
export function contentBlockId(content: string, index: number): string {
  const encoder = new TextEncoder()
  const data = encoder.encode(`${index}:${content}`)
  const hexHash = MurmurHash3.x64.hash128(data)
  const hexBytes = hexHash.substring(0, 12)
  const byteBuffer = new Uint8Array(6)
  for (let i = 0; i < 6; i++) {
    byteBuffer[i] = parseInt(hexBytes.substr(i * 2, 2), 16)
  }
  return btoa(String.fromCharCode(...byteBuffer))
    .replace(/\+/g, "-")
    .replace(/\//g, "_")
    .replace(/=+$/, "")
}
