 _ = require 'underscore'
 
 repo = git.open '.'
-
-STATUS_CODE_MAP =
-  1:
-    desc: 'added'
-    staged: true
-  2:
-    desc: 'modified'
-    staged: true
-  4:
-    desc: 'deleted'
-    staged: true
-  128:
-    desc: 'added'
-    staged: false
-  256:
-    desc: 'modified'
-    staged: false
-  512:
-    desc: 'deleted'
-    staged: false
-
-_filter_statuses_by_desc = (statuses, desc) ->
-  _.filter statuses, (status) ->
-    STATUS_CODE_MAP[status.code]?.desc is desc
-
 
 module.exports = (Impromptu, register, git) ->
   # Expose the repo object from the git-utils library


        path: path
        code: code
        staged: STATUS_CODE_MAP[code]?.staged
        desc: STATUS_CODE_MAP[code]?.desc

      done null, statuses

  # Get the number of "untracked" files
  # Untracked is defined as new files that are not staged
  register 'untracked',
    update: (done) ->
      git._status (err, statuses) ->
        statuses = _filter_statuses_by_desc(statuses, 'added')
        count = _.where(statuses, {staged: false}).length
        done err, count


      git._aheadBehind (err, aheadBehind) ->
        done err, aheadBehind?.behind

  class Statuses
    constructor: (@statuses) ->
      @modified = @_filterByState 'modified'
      @added = @_filterByState 'added'
      @deleted = @_filterByState 'deleted'

    toString: ->
      results = []
      results.push @modified if @modified.length
      results.push @added if @added.length
      results.push @deleted if @deleted.length
      results.join ' '

    _filterByState: (@state) ->
      filtered = _.filter @statuses, (status) ->
        Statuses.CODE_MAP[status.code]?.state is state

      if Statuses.formatters[state]
        filtered.toString = Statuses.formatters[state]

      filtered

    @CODE_MAP:
      1:
        state: 'added'
        staged: true
      2:
        state: 'modified'
        staged: true
      4:
        state: 'deleted'
        staged: true
      128:
        state: 'added'
        staged: false
      256:
        state: 'modified'
        staged: false
      512:
        state: 'deleted'
        staged: false

    @formatters:
      added: ->
        if @length then "+#{@length}" else ""

      modified: ->
        if @length then "∆#{@length}" else ""

      deleted: ->
        if @length then "-#{@length}" else ""

  # Returns an array of objects with 'path', 'code', 'staged', 'state'
  #
  # This command *must* be passed through a formatter before its displayed
  register '_status',
