import { randomUint64 } from "../../shared/src/random-uint64.js";
import { assert } from "../../shared/src/valita.js";
import { string } from "@badrap/valita";
const STRING_LENGTH = 22;
const hashRe = /^[0-9a-v-]+$/;
const emptyUUID = "0".repeat(STRING_LENGTH);
const emptyHash = emptyUUID;
const newRandomHash = makeNewRandomHashFunctionInternal();
function toStringAndSlice(n, len) {
  return n.toString(32).slice(-len).padStart(len, "0");
}
function makeNewRandomHashFunctionInternal() {
  let base = "";
  let i = 0;
  return () => {
    if (!base) {
      base = toStringAndSlice(randomUint64(), 12);
    }
    const tail = toStringAndSlice(i++, 10);
    return base + tail;
  };
}
function isHash(value) {
  return typeof value === "string" && hashRe.test(value);
}
function assertHash(value) {
  assert(value, hashSchema);
}
const hashSchema = string().assert(isHash, "Invalid hash");
export {
  STRING_LENGTH,
  assertHash,
  emptyHash,
  hashSchema,
  isHash,
  newRandomHash
};
//# sourceMappingURL=hash.js.map
