import { h128 } from "../../../shared/src/hash.js";
import { parse } from "../../../shared/src/valita.js";
import { primaryKeyValueSchema } from "../../../zero-protocol/src/primary-key.js";
const DESIRED_QUERIES_KEY_PREFIX = "d/";
const GOT_QUERIES_KEY_PREFIX = "g/";
const ENTITIES_KEY_PREFIX = "e/";
const MUTATIONS_KEY_PREFIX = "m/";
function toDesiredQueriesKey(clientID, hash) {
  return DESIRED_QUERIES_KEY_PREFIX + clientID + "/" + hash;
}
function desiredQueriesPrefixForClient(clientID) {
  return DESIRED_QUERIES_KEY_PREFIX + clientID + "/";
}
function toGotQueriesKey(hash) {
  return GOT_QUERIES_KEY_PREFIX + hash;
}
function toMutationResponseKey(mid) {
  return MUTATIONS_KEY_PREFIX + mid.clientID + "/" + mid.id;
}
function toPrimaryKeyString(tableName, primaryKey, value) {
  if (primaryKey.length === 1) {
    return ENTITIES_KEY_PREFIX + tableName + "/" + parse(value[primaryKey[0]], primaryKeyValueSchema);
  }
  const values = primaryKey.map((k) => parse(value[k], primaryKeyValueSchema));
  const str = JSON.stringify(values);
  const idSegment = h128(str);
  return ENTITIES_KEY_PREFIX + tableName + "/" + idSegment;
}
function sourceNameFromKey(key) {
  const slash = key.indexOf("/", ENTITIES_KEY_PREFIX.length);
  return key.slice(ENTITIES_KEY_PREFIX.length, slash);
}
export {
  DESIRED_QUERIES_KEY_PREFIX,
  ENTITIES_KEY_PREFIX,
  GOT_QUERIES_KEY_PREFIX,
  MUTATIONS_KEY_PREFIX,
  desiredQueriesPrefixForClient,
  sourceNameFromKey,
  toDesiredQueriesKey,
  toGotQueriesKey,
  toMutationResponseKey,
  toPrimaryKeyString
};
//# sourceMappingURL=keys.js.map
