import { createDecoder, Bytes, createCodec } from 'scale-ts';
import { compactNumber } from './compact.mjs';

const bitSequenceDecoder = createDecoder((data) => {
  const bitsLen = compactNumber.dec(data);
  const bytesLen = Math.ceil(bitsLen / 8);
  const bytes = Bytes(bytesLen).dec(data);
  return { bytes, bitsLen };
});
const bitSequenceEncoder = (input) => {
  if (input.bitsLen > input.bytes.length * 8)
    throw new Error(
      `Not enough bytes. (bitsLen:${input.bitsLen}, bytesLen:${input.bytes.length})`
    );
  const lenEncoded = compactNumber.enc(input.bitsLen);
  const result = new Uint8Array(input.bytes.length + lenEncoded.length);
  result.set(lenEncoded, 0);
  result.set(input.bytes, lenEncoded.length);
  return result;
};
const bitSequence$1 = createCodec(bitSequenceEncoder, bitSequenceDecoder);

export { bitSequence$1 as bitSequence };
//# sourceMappingURL=bitSequence.mjs.map
