ByteIterableBase =
  (require '../../database/impl/iterate/ByteIterableBase.coffee').
  ByteIterableBase
CompoundByteIteratorBase =
  (require './CompoundByteIteratorBase.coffee').CompoundByteIteratorBase
ExodusError =
  (require '../../util/ExodusVersion.coffee').ExodusError

# This class allows to work with many iterables as one.
class CompoundByteIterable extends ByteIterableBase
  # @private
  iterables: undefined
  # @private
  count: undefined

  # Constructor.
  #
  # @param iterables the array of iterables to use.
  @create$BufferByteIterable: (iterables, o) ->
    return CompoundByteIterable.create$ArrayByteIterable$int iterables,
      iterables.length, o

  # Constructor.
  #
  # @param iterables the array of iterables to use.
  # @param count the amount of iterables in array.
  @create$ArrayByteIterable$int: (iterables, count, o) ->
    if (count < 1)
      throw new ExodusError("Failed to initialize CompoundByteIterable")
    if !o? then o = new CompoundByteIterable
    o.iterables = iterables
    o.count = count
    return o

  getIterator: () ->
    res = CompoundByteIteratorBase.create$ByteIterator @iterables.iterator()
    res.offs = 0
    res.nextIterator$emit = () ->
      offs += 1
      return if offs < @count then @iterables[offs].iterator() else undefined
    return res

