 coffee      = require 'coffee-script'
 fs          = require 'fs'
 path        = require 'path'
-#detective   = require 'detective'
+detective   = require 'detective'
 {compile}   = require './utils'
 
 

      reqFolders = treePos.name.split(path.basename(treePos.name))[0][0...-1].split('/') # array of folders to move into relative to basepoint to get to the file that required dep below
      for dep in @loadDependencies(treePos.name, treePos.reqFolders, treePos.domain) # use detective to get this 'deps' fn
        branchScanUp(treePos, dep) # make sure this dep does not exist above it in the tree
        treePos.deps[dep] = {name : dep, parent: treePos, deps: {}, reqFolders: reqFolders }
        arguments.callee(treePos.deps[dep])
      return

    recursiveDetective(tree, @basePoint)
    clearParentEntries(tree)
    console.log tree

sortDependencies = (tree) -> # must flatten array into levels to get an ordered list of filenames w/resp. paths
  #need to:
  #1. add an index to the tree to indicate what level we are on (do in recursiveDetective step?)
  #2. create an ARRAY with path+level+$+level (so there will be multiple versions potentially, so remove worse ones as we go)
  for mod in tree
    stuff

  #3. then sort by level descending and remove the level from the name so that it can be used by builder verbatim : )
  arr.sort((a,b) -> b.split('$')[1] - a.split('$')[1]).map((e) -> e.split('$')[0]) # this will give you the inclusion order!


module.exports = (o) ->
  tree = (new Resolver(o)).getTree()
  if o.targetTree
    # write sanitized version of the tree to the target file for code review
    return

  sortDependencies(tree)



  reqPoint = 'models/user'
  name = './event'
  reqFolders = reqPoint.split(path.basename(reqPoint))[0][0...-1].split('/') #remove name, last slash and convert to folders
  #console.log reqFolders
  #console.log toAbsPath(name, reqFolders)

  tree =
    name : 'app'
    deps :
      'A'  :
        name : 'A'
        deps : {}
      'B'  :
        name : 'B'
        deps : {'C': {name:'C', deps: {'E':{name:'E',deps:{}}}  }, 'D' :{name:'D', deps: {}} }
  console.log JSON.stringify sanitizeTree tree

  return

###
tree = {
