 _ = require 'underscore'
 EventEmitter = require 'event-emitter'
+SpanIndex = require 'span-index'
 Point = require 'point'
 Range = require 'range'
 


  bufferPositionFromScreenPosition: (screenPosition) ->
    screenPosition = Point.fromObject(screenPosition)
    bufferRow = 0
    currentScreenRow = 0
    for wrappedLine in @wrappedLines
      for screenLine in wrappedLine.screenLines
        if currentScreenRow == screenPosition.row
          return new Point(bufferRow, screenLine.startColumn + screenPosition.column)
        currentScreenRow++
      bufferRow++

  screenLineForRow: (screenRow) ->
    @screenLinesForRows(screenRow, screenRow)[0]

  screenLinesForRows: (startRow, endRow) ->
    screenLines = []
    currentScreenRow = 0
    for wrappedLine in @wrappedLines
      for screenLine in wrappedLine.screenLines
        screenLines.push screenLine if currentScreenRow >= startRow
        currentScreenRow++
        return screenLines if currentScreenRow > endRow

  screenLines: ->
    @screenLinesForRows(0, @screenLineCount() - 1)

  screenLineCount: ->
    count = 0
    for wrappedLine, i in @wrappedLines
      count += wrappedLine.screenLines.length
    count

_.extend(LineWrapper.prototype, EventEmitter)


  lastScreenRowForBufferRow: (bufferRow) ->
    startRow = @screenPositionFromBufferPosition([bufferRow, 0]).row
    startRow + (@index.at(bufferRow).screenLines.length - 1)

  buildWrappedLinesForBufferRows: (start, end) ->
    for row in [start..end]
