-_ = require 'underscore'
+_ = require 'lodash'
 
 MATH_MARKER_BLOCK  = '\u200c\u200c\u200c' # zero-width non-joiner
 MATH_MARKER_INLINE = '\u200b\u200b\u200b' # zero-width space

  # schedule a Mathjax pass if there is at least one [data-math] or <math> element present

  if windowImpl.MathJax?.Hub?.Queue? and root.querySelector(COMBINED_MATH_SELECTOR)
    typesetDocument(windowImpl)


# The following should be called once and configures MathJax.

      node.textContent = "#{MATH_MARKER_INLINE}#{formula}#{MATH_MARKER_INLINE}"
    latexNodes.push(node)

  windowImpl.MathJax.Hub.Queue(
    ->
      if _.isEmpty(latexNodes)
        return

      windowImpl.MathJax.Hub.Typeset(latexNodes)
    ->
      mathMLNodes = _.toArray(root.querySelectorAll(MATH_ML_SELECTOR))
      if _.isEmpty(mathMLNodes)
        return

      # style the entire document because mathjax is unable to style individual math elements
      windowImpl.MathJax.Hub.Typeset( root )
    ->
      # Queue a call to mark the found nodes as rendered so are ignored if typesetting is called repeatedly
      # uses className += instead of classList because IE
      for node in latexNodes
        node.className += " #{MATH_RENDERED_CLASS}"
  )

_.memoize.Cache = WeakMap
# memoize'd getter for typeset document function so that each node's
# typeset has its own debounce
getTypesetDocument = _.memoize((root, windowImpl) ->
  # Install a debounce around typesetting function so that it will only run once
  # every Xms even if called multiple times in that period
  _.debounce(typesetDocument, 100).bind(null, root, windowImpl)
)

# typesetMath is the main exported function.
# It's called by components like HTML after they're rendered
