 TokenIterator = require './token-iterator'
 {Point} = require 'text-buffer'
+{isPairedCharacter} = require './text-utils'
 
 module.exports =
 class LinesYardstick

      return cachedPosition

    textNodes = @lineNodesProvider.textNodesForScreenRow(row)
    textNodeStartIndex = 0

    for textNode in textNodes
      textNodeEndIndex = textNodeStartIndex + textNode.textContent.length
      if textNodeEndIndex > column
        indexInTextNode = column - textNodeStartIndex
        break
      else
        textNodeStartIndex = textNodeEndIndex

    if textNode?
      indexInTextNode ?= textNode.textContent.length
      leftPixelPosition = @leftPixelPositionForCharInTextNode(lineNode, textNode, indexInTextNode)

      @leftPixelPositionCache[lineId] ?= {}
      @leftPixelPositionCache[lineId][column] = leftPixelPosition

  screenPositionForPixelPosition: (pixelPosition) ->
    targetTop = pixelPosition.top
    targetLeft = pixelPosition.left
    row = Math.floor(targetTop / @model.getLineHeightInPixels())
    targetLeft = 0 if row < 0
    targetLeft = Infinity if row > @model.getLastScreenRow()
    row = Math.min(row, @model.getLastScreenRow())
    row = Math.max(0, row)

    lineNode = @lineNodesProvider.lineNodeForScreenRow(row)
    return Point(row, 0) unless lineNode

    textNodes = @lineNodesProvider.textNodesForScreenRow(row)
    lineOffset = lineNode.getBoundingClientRect().left
    targetLeft += lineOffset

    textNodeStartColumn = 0
    for textNode in textNodes
      {length: textNodeLength, textContent: textNodeContent} = textNode
      textNodeRight = @clientRectForRange(textNode, 0, textNodeLength).right

      if textNodeRight > targetLeft
        characterIndex = 0
        while characterIndex < textNodeLength
          if isPairedCharacter(textNodeContent, characterIndex)
            nextCharacterIndex = characterIndex + 2
          else
            nextCharacterIndex = characterIndex + 1

          rangeRect = @clientRectForRange(textNode, characterIndex, nextCharacterIndex)

          if rangeRect.right > targetLeft
            if targetLeft <= ((rangeRect.left + rangeRect.right) / 2)
              return Point(row, textNodeStartColumn + characterIndex)
            else
              return Point(row, textNodeStartColumn + nextCharacterIndex)
          else
            characterIndex = nextCharacterIndex

      textNodeStartColumn += textNodeLength

    Point(row, textNodeStartColumn)

  pixelPositionForScreenPosition: (screenPosition) ->
    targetRow = screenPosition.row
