
module.exports = remotePackages

var registry = require("../registry")
  , containsSingleMatch = require("./contains-single-match")
  , getCompletions = require("./get-completions")

/*
  Looks up remote packages for CLI tab-completion.

  NOTE: If doVersion is true, versions in the form <name>@<version>
        will be completed.

        If doTag is true, tags in the form <name>@<tag> will be
        completed.

        If recurring in true, sequences of multiple packages can be
        completed. i.e. for schemes such as:
        npm <command> <name>[@<version> [<name>[@<version>] ...]
*/
function remotePackages (args, index, doVersion, doTag
                         , recurring, cb) {
  if (recurring || index < 3) {
    var name = (args.length + 1 === index) ? args[args.length - 1] : ""
    if (name === undefined) name = ""
    // use up-to 1 hour stale cache.  not super urgent.
    registry.get("/", null, 3600, function (er, d) {
      if (er) return cb(er)
      var remoteList = Object.keys(d)
        , found = remoteList.indexOf(name)
        , unique = found && containsSingleMatch(name, remoteList)
        , simpleMatches = getCompletions(name, remoteList)
        , uniqueMatch = unique && simpleMatches[0]
        , addTag = doTag && (unique || found || name.indexOf("@") !== -1)
        , addVer = doVersion && (unique || found || name.indexOf("@") !== -1)
        , list = []
        , pieces = (uniqueMatch || name).split("@")
        , pkgname = pieces[0]
        , extras = []
      if (unique && !addTag && !addVer) return cb(null, [name])
      if (d[pkgname] && (addTag || addVer)) {
        if (d[pkgname].versions && addVer) {
          extras = extras.concat(Object.keys(d[pkgname].versions))
        }
        if (d[pkgname]["dist-tags"] && addTag) {
          extras = extras.concat(Object.keys(d[pkgname]["dist-tags"]))
        }
        list = list.concat(extras.map(function (e) {
          return pkgname + "@" + e
        }))
      }
      if (!unique) list = list.concat(getCompletions(name, remoteList))
      return cb(null, list)
    })
  }
}

@@ -1876,24 +1876,45 @@
       list =
+ getCompletions(name,
  list.concat
@@ -1986,16 +1986,17 @@
      %7D))
+)
 %0A      %7D

Fix bug where list of remote pkg <name>@<version> pairs aren't filtered properly.