'use strict';

var _es6Map = require('es6-map');

var _es6Map2 = _interopRequireDefault(_es6Map);

var _getExports = require('../core/getExports');

var _getExports2 = _interopRequireDefault(_getExports);

var _importDeclaration = require('../importDeclaration');

var _importDeclaration2 = _interopRequireDefault(_importDeclaration);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function (context) {

  var fileImports = new _es6Map2.default();
  var allPropertyLookups = new _es6Map2.default();

  function handleImportDefault(node) {
    var declaration = (0, _importDeclaration2.default)(context);
    var exportMap = _getExports2.default.get(declaration.source.value, context);
    if (exportMap == null) return;

    if (exportMap.errors.length) {
      exportMap.reportErrors(context, declaration);
      return;
    }

    fileImports.set(node.local.name, {
      exportMap: exportMap,
      sourcePath: declaration.source.value
    });
  }

  function storePropertyLookup(objectName, propName, node) {
    var lookups = allPropertyLookups.get(objectName) || [];
    lookups.push({ node: node, propName: propName });
    allPropertyLookups.set(objectName, lookups);
  }

  function handlePropLookup(node) {
    var objectName = node.object.name;
    var propName = node.property.name;
    storePropertyLookup(objectName, propName, node);
  }

  function handleDestructuringAssignment(node) {
    var isDestructure = node.id.type === 'ObjectPattern' && node.init != null && node.init.type === 'Identifier';
    if (!isDestructure) return;

    var objectName = node.init.name;
    node.id.properties.forEach(function (_ref) {
      var key = _ref.key;

      if (key != null) {
        // rest properties are null
        storePropertyLookup(objectName, key.name, key);
      }
    });
  }

  function handleProgramExit() {
    allPropertyLookups.forEach(function (lookups, objectName) {
      var fileImport = fileImports.get(objectName);
      if (fileImport == null) return;

      lookups.forEach(function (_ref2) {
        var propName = _ref2.propName;
        var node = _ref2.node;

        // the default import can have a "default" property
        if (propName === 'default') {
          return;
        }
        if (fileImport.exportMap.namespace.has(propName)) {
          context.report({
            node: node,
            message: 'Caution: `' + objectName + '` also has a named export ' + ('`' + propName + '`. Check if you meant to write ') + ('`import {' + propName + '} from \'' + fileImport.sourcePath + '\'` ') + 'instead.'
          });
        }
      });
    });
  }

  return {
    'ImportDefaultSpecifier': handleImportDefault,
    'MemberExpression': handlePropLookup,
    'VariableDeclarator': handleDestructuringAssignment,
    'Program:exit': handleProgramExit
  };
}; /**
    * @fileoverview Rule to warn about potentially confused use of name exports
    * @author Desmond Brand
    * @copyright 2016 Desmond Brand. All rights reserved.
    * See LICENSE in root directory for full license.
    */
//# sourceMappingURL=data:application/json;base64,[encoded data, 7424 characters]