 keycode              = require 'keycode'
 _                    = require 'lodash'
 
+DEBOUNCE_WAIT      = 300
+HAS_TEXT_CLASSNAME = 'has-text'
+KEYDOWN_KEYS       = [ 'enter', 'esc', 'up', 'down' ]
 
-module.exports = class IDEFileFinder extends KDCustomHTMLView
+isKeyDownKey = _.memoize (key) -> _.indexOf KEYDOWN_KEYS, key
+
+
+module.exports =
+
+class IDEFileFinder extends KDCustomHTMLView
 
   constructor: (options = {}, data) ->
 

    @on 'ReceivedClickElsewhere', @bound 'destroy'


  handleKeyDown: (e) ->

    code = e.which or e.keyCode
    key  = keycode code

    switch key
      when 'esc'   then @destroy()
      when 'enter' then @handleEnterKey()
      when 'up'    then @handleNavigation 'up'
      when 'down'  then @handleNavigation 'down'


  search: (text) ->

    return @clearSearch()  if text is ''


    item?.getElement().scrollIntoViewIfNeeded()


  handleKeyUp: (e) ->

    code = e.which or e.keyCode
    key  = keycode code
    term = @input.getValue()

    if   term is '' then @input.unsetClass HAS_TEXT_CLASSNAME
    else @input.setClass HAS_TEXT_CLASSNAME

    @search term  if term isnt @lastTerm or not isKeyDownKey key

    return


  handleKeyDown: (e) ->

    code = e.which or e.keyCode
    key  = keycode code

    switch key
      when 'enter' then @handleEnterKey()
      when 'esc'   then @destroy()
      when 'up'    then @handleNavigation 'up'
      when 'down'  then @handleNavigation 'down'

    return


  handleEnterKey: ->
