import { BTreeRead } from "../btree/read.js";
import { commitFromHead, commitFromHash, DEFAULT_HEAD_NAME } from "./commit.js";
import { IndexRead } from "./index.js";
class Read {
  #dagRead;
  map;
  indexes;
  constructor(dagRead, map, indexes) {
    this.#dagRead = dagRead;
    this.map = map;
    this.indexes = indexes;
  }
  has(key) {
    return this.map.has(key);
  }
  get(key) {
    return this.map.get(key);
  }
  isEmpty() {
    return this.map.isEmpty();
  }
  getMapForIndex(indexName) {
    const idx = this.indexes.get(indexName);
    if (idx === void 0) {
      throw new Error(`Unknown index name: ${indexName}`);
    }
    return idx.map;
  }
  get closed() {
    return this.#dagRead.closed;
  }
  close() {
    this.#dagRead.release();
  }
}
function readFromDefaultHead(dagRead, formatVersion) {
  return readFromHead(DEFAULT_HEAD_NAME, dagRead, formatVersion);
}
async function readFromHead(name, dagRead, formatVersion) {
  const commit = await commitFromHead(name, dagRead);
  return readFromCommit(commit, dagRead, formatVersion);
}
async function readFromHash(hash, dagRead, formatVersion) {
  const commit = await commitFromHash(hash, dagRead);
  return readFromCommit(commit, dagRead, formatVersion);
}
function readFromCommit(commit, dagRead, formatVersion) {
  const indexes = readIndexesForRead(commit, dagRead, formatVersion);
  const map = new BTreeRead(dagRead, formatVersion, commit.valueHash);
  return new Read(dagRead, map, indexes);
}
function readIndexesForRead(commit, dagRead, formatVersion) {
  const m = /* @__PURE__ */ new Map();
  for (const index of commit.indexes) {
    m.set(
      index.definition.name,
      new IndexRead(
        index,
        new BTreeRead(dagRead, formatVersion, index.valueHash)
      )
    );
  }
  return m;
}
export {
  Read,
  readFromDefaultHead,
  readFromHash,
  readFromHead,
  readIndexesForRead
};
//# sourceMappingURL=read.js.map
