import { assertString, assert } from "../../../shared/src/asserts.js";
import { assertDeepFrozen } from "../frozen-json.js";
function asRefs(sortedRefs) {
  return sortedRefs;
}
function toRefs(refs) {
  if (Array.isArray(refs)) {
    refs.sort();
    for (let i = 1; i < refs.length; i++) {
      assert(refs[i - 1] !== refs[i], "Refs must not have duplicates");
    }
    return asRefs(refs);
  }
  const refsArray = [...refs];
  refsArray.sort();
  return asRefs(refsArray);
}
class Chunk {
  hash;
  data;
  /**
   * Meta is an array of refs. If there are no refs we do not write a meta
   * chunk.
   */
  meta;
  constructor(hash, data, refs) {
    assert(
      !refs.includes(hash),
      "Chunk cannot reference itself"
    );
    assertDeepFrozen(data);
    this.hash = hash;
    this.data = data;
    this.meta = refs;
  }
}
function assertRefs(v) {
  if (!Array.isArray(v)) {
    throw new Error("Refs must be an array");
  }
  if (v.length > 0) {
    assertString(v[0]);
    for (let i = 1; i < v.length; i++) {
      assertString(v[i]);
    }
  }
}
function createChunk(data, refs, chunkHasher) {
  const hash = chunkHasher();
  return new Chunk(hash, data, refs);
}
export {
  Chunk,
  asRefs,
  assertRefs,
  createChunk,
  toRefs
};
//# sourceMappingURL=chunk.js.map
