 module.exports =
 class LayerIterator
   constructor: (@layer, sourceIterator) ->
-    @transformDelegate = new TransformDelegate(sourceIterator)
     @position = Point.zero()
     @sourcePosition = Point.zero()
+    @transformDelegate = new TransformDelegate(sourceIterator)
 
   next: ->
     unless @transformDelegate.bufferedOutputs.length > 0
       @layer.transform.operate(@transformDelegate)
-    @position = @transformDelegate.bufferedPositions.shift()
-    @sourcePosition = @transformDelegate.bufferedSourcePositions.shift()
-    value = @transformDelegate.bufferedOutputs.shift()
-    if value is EOF
+    {@position, @sourcePosition, content} = @transformDelegate.bufferedOutputs.shift()
+    if content is EOF
       {done: true}
     else
-      {value, done: false}
+      {done: false, value: content}
 
   seek: (position) ->
     @position = Point.zero()

    @reset(Point.zero(), Point.zero())

  reset: (position, sourcePosition) ->
    @sourceIterator.seek(sourcePosition)
    @position = position
    @sourcePosition = sourcePosition
    @bufferedSourceOutput = null
    @bufferedOutputs = []
    @bufferedPositions = []
    @bufferedSourcePositions = []

  read: =>
    @bufferedSourceOutput ?= @sourceIterator.next().value

  consume: (count) =>
    @sourcePosition.column += count
    @bufferedSourceOutput = @bufferedSourceOutput.slice(count)
    @bufferedSourceOutput = null if @bufferedSourceOutput is ""

  produce: (output) =>
    switch output

    return if position.isZero()

    until @sourcePosition.compare(position) >= 0
      lastPosition = @position
      lastSourcePosition = @sourcePosition
      {done} = @next()
      break if done

    overshoot = position.column - lastSourcePosition.column
    lastPosition.column += overshoot
    @transformDelegate.reset(lastPosition, position)
    @position = lastPosition
    @sourcePosition = position

  getPosition: ->
