-ArrayUtil = require 'theoricus/utils/array_util'
 Router = require 'theoricus/core/router'
 Process = require 'theoricus/core/process'
 _ = require 'lodash'

    if @dead_processes.length
      process = @dead_processes.pop()
      process.destroy @_destroy_dead_processes
      search = route: match: process.route.match
      ArrayUtil.delete @active_processes, search
    else
      @_run_pending_processes()



  # try to finds the dependency in many ways
  _find_dependency:( process, after_find )->
    dependency = process.dependency

    # 1 - tries to find dependency within the ACTIVE PROCESSES
    dep = _.find @active_processes, (item)->
      return item.url is dependency
    return after_find dep if dep?

    # 2 - tries to find dependency within the ROUTES (using strict route name)
    dep = _.find @router.routes, (item)->
      return item.test dependency

    if dep?

      # assemble route's `at` dependency based on parent url params
      params = dep.extract_params process.dependency
      at = dep.rewrite_url_with_parms dep.at, params

      return new Process @the, @, dep, at, dependency, process, (process)=>
        after_find process

    after_find null
