/* @flow */

import * as t from "./index";

/**
 * Return a list of binding identifiers associated with the input `node`.
 */

export function getBindingIdentifiers(
  node: Object,
  duplicates?: boolean,
): Object {
  let search = [].concat(node);
  let ids    = Object.create(null);

  while (search.length) {
    let id = search.shift();
    if (!id) continue;

    let keys = t.getBindingIdentifiers.keys[id.type];

    if (t.isIdentifier(id)) {
      if (duplicates) {
        let _ids = ids[id.name] = ids[id.name] || [];
        _ids.push(id);
      } else {
        ids[id.name] = id;
      }
    } else if (t.isExportDeclaration(id)) {
      if (t.isDeclaration(node.declaration)) {
        search.push(node.declaration);
      }
    } else if (keys) {
      for (let i = 0; i < keys.length; i++) {
        let key = keys[i];
        if (id[key]) {
          search = search.concat(id[key]);
        }
      }
    }
  }

  return ids;
}

/**
 * Mapping of types to their identifier keys.
 */

getBindingIdentifiers.keys = {
  DeclareClass: ["id"],
  DeclareFunction: ["id"],
  DeclareModule: ["id"],
  DeclareVariable: ["id"],
  InterfaceDeclaration: ["id"],
  TypeAlias: ["id"],

  CatchClause: ["param"],
  LabeledStatement: ["label"],
  UnaryExpression: ["argument"],
  AssignmentExpression: ["left"],

  ImportSpecifier: ["local"],
  ImportNamespaceSpecifier: ["local"],
  ImportDefaultSpecifier: ["local"],
  ImportDeclaration: ["specifiers"],

  ExportSpecifier: ["exported"],
  ExportNamespaceSpecifier: ["exported"],
  ExportDefaultSpecifier: ["exported"],

  FunctionDeclaration: ["id", "params"],
  FunctionExpression: ["id", "params"],

  ClassDeclaration: ["id"],
  ClassExpression: ["id"],

  RestElement: ["argument"],
  UpdateExpression: ["argument"],

  SpreadProperty: ["argument"],
  ObjectProperty: ["value"],

  AssignmentPattern: ["left"],
  ArrayPattern: ["elements"],
  ObjectPattern: ["properties"],

  VariableDeclaration: ["declarations"],
  VariableDeclarator: ["id"]
};

export function getOuterBindingIdentifiers(
  node: Object,
  duplicates?: boolean,
): Object {
  if (t.isFunction(node)) {
    let id = node.id;
    if (id) {
      return {
        [id.name]: duplicates ? [id] : id
      };
    } else {
      return {};
    }
  } else {
    return getBindingIdentifiers(node, duplicates);
  }
}

@@ -191,32 +191,54 @@
 ates?: boolean,%0A
+  outerOnly?: boolean%0A
 ): Object %7B%0A  le
@@ -747,32 +747,126 @@
 ation);%0A      %7D%0A
+    %7D else if (outerOnly) %7B%0A      if (t.isFunction(id)) %7B%0A        search.push(id.id);%0A      %7D%0A
     %7D else if (k
@@ -2218,187 +2218,8 @@
 t %7B%0A
-  if (t.isFunction(node)) %7B%0A    let id = node.id;%0A    if (id) %7B%0A      return %7B%0A        %5Bid.name%5D: duplicates ? %5Bid%5D : id%0A      %7D;%0A    %7D else %7B%0A      return %7B%7D;%0A    %7D%0A  %7D else %7B%0A  
   re
@@ -2265,13 +2265,15 @@
 ates
-);%0A  %7D
+, true);
 %0A%7D%0A

refactor t.getOuterBindingIdentifiers to only return the id if it's a function declaration