'use strict';Object.defineProperty(exports, "__esModule", { value: true });var _createClass = function () {function defineProperties(target, props) {for (var i = 0; i < props.length; i++) {var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);}}return function (Constructor, protoProps, staticProps) {if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;};}();var _arrayIncludes = require('array-includes');var _arrayIncludes2 = _interopRequireDefault(_arrayIncludes);
var _eslint = require('eslint');
var _doc = require('./doc');
var _namespace = require('./namespace');var _namespace2 = _interopRequireDefault(_namespace);
var _specifier = require('./specifier');var _specifier2 = _interopRequireDefault(_specifier);
var _captureDependency = require('./captureDependency');
var _patternCapture = require('./patternCapture');var _patternCapture2 = _interopRequireDefault(_patternCapture);
var _remotePath = require('./remotePath');function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}

/**
                                                                                                                                                                                                                                                                                                * sometimes legacy support isn't _that_ hard... right?
                                                                                                                                                                                                                                                                                                */
function makeSourceCode(text, ast) {
  if (_eslint.SourceCode.length > 1) {
    // ESLint 3
    return new _eslint.SourceCode(text, ast);
  } else {
    // ESLint 4, 5
    return new _eslint.SourceCode({ text: text, ast: ast });
  }
}var

ImportExportVisitorBuilder = function () {
  function ImportExportVisitorBuilder(
  path,
  context,
  exportMap,
  ExportMapBuilder,
  content,
  ast,
  isEsModuleInteropTrue,
  thunkFor)
  {var _this = this;_classCallCheck(this, ImportExportVisitorBuilder);
    this.context = context;
    this.namespace = new _namespace2['default'](path, context, ExportMapBuilder);
    this.remotePathResolver = new _remotePath.RemotePath(path, context);
    this.source = makeSourceCode(content, ast);
    this.exportMap = exportMap;
    this.ast = ast;
    this.isEsModuleInteropTrue = isEsModuleInteropTrue;
    this.thunkFor = thunkFor;
    var docstyle = this.context.settings && this.context.settings['import/docstyle'] || ['jsdoc'];
    this.docStyleParsers = {};
    docstyle.forEach(function (style) {
      _this.docStyleParsers[style] = _doc.availableDocStyleParsers[style];
    });
  }_createClass(ImportExportVisitorBuilder, [{ key: 'build', value: function () {function build(

      astNode) {var _this3 = this;
        return Object.assign({
          ExportDefaultDeclaration: function () {function ExportDefaultDeclaration() {
              var exportMeta = (0, _doc.captureDoc)(this.source, this.docStyleParsers, astNode);
              if (astNode.declaration.type === 'Identifier') {
                this.namespace.add(exportMeta, astNode.declaration);
              }
              this.exportMap.namespace.set('default', exportMeta);
            }return ExportDefaultDeclaration;}(),
          ExportAllDeclaration: function () {function ExportAllDeclaration() {
              var getter = (0, _captureDependency.captureDependency)(astNode, astNode.exportKind === 'type', this.remotePathResolver, this.exportMap, this.context, this.thunkFor);
              if (getter) {this.exportMap.dependencies.add(getter);}
              if (astNode.exported) {
                (0, _specifier2['default'])(astNode, astNode.exported, this.exportMap, this.namespace);
              }
            }return ExportAllDeclaration;}(),
          /** capture namespaces in case of later export */
          ImportDeclaration: function () {function ImportDeclaration() {
              (0, _captureDependency.captureDependencyWithSpecifiers)(astNode, this.remotePathResolver, this.exportMap, this.context, this.thunkFor);
              var ns = astNode.specifiers.find(function (s) {return s.type === 'ImportNamespaceSpecifier';});
              if (ns) {
                this.namespace.rawSet(ns.local.name, astNode.source.value);
              }
            }return ImportDeclaration;}(),
          ExportNamedDeclaration: function () {function ExportNamedDeclaration() {var _this2 = this;
              (0, _captureDependency.captureDependencyWithSpecifiers)(astNode, this.remotePathResolver, this.exportMap, this.context, this.thunkFor);
              // capture declaration
              if (astNode.declaration != null) {
                switch (astNode.declaration.type) {
                  case 'FunctionDeclaration':
                  case 'ClassDeclaration':
                  case 'TypeAlias': // flowtype with babel-eslint parser
                  case 'InterfaceDeclaration':
                  case 'DeclareFunction':
                  case 'TSDeclareFunction':
                  case 'TSEnumDeclaration':
                  case 'TSTypeAliasDeclaration':
                  case 'TSInterfaceDeclaration':
                  case 'TSAbstractClassDeclaration':
                  case 'TSModuleDeclaration':
                    this.exportMap.namespace.set(astNode.declaration.id.name, (0, _doc.captureDoc)(this.source, this.docStyleParsers, astNode));
                    break;
                  case 'VariableDeclaration':
                    astNode.declaration.declarations.forEach(function (d) {
                      (0, _patternCapture2['default'])(
                      d.id,
                      function (id) {return _this2.exportMap.namespace.set(id.name, (0, _doc.captureDoc)(_this2.source, _this2.docStyleParsers, d, astNode));});

                    });
                    break;
                  default:}

              }
              astNode.specifiers.forEach(function (s) {return (0, _specifier2['default'])(s, astNode, _this2.exportMap, _this2.namespace);});
            }return ExportNamedDeclaration;}(),
          TSExportAssignment: function () {function TSExportAssignment() {return _this3.typeScriptExport(astNode);}return TSExportAssignment;}() },
        this.isEsModuleInteropTrue && { TSNamespaceExportDeclaration: function () {function TSNamespaceExportDeclaration() {return _this3.typeScriptExport(astNode);}return TSNamespaceExportDeclaration;}() });

      }return build;}()

    // This doesn't declare anything, but changes what's being exported.
  }, { key: 'typeScriptExport', value: function () {function typeScriptExport(astNode) {var _this4 = this;
        var exportedName = astNode.type === 'TSNamespaceExportDeclaration' ?
        (astNode.id || astNode.name).name :
        astNode.expression && astNode.expression.name || astNode.expression.id && astNode.expression.id.name || null;
        var declTypes = [
        'VariableDeclaration',
        'ClassDeclaration',
        'TSDeclareFunction',
        'TSEnumDeclaration',
        'TSTypeAliasDeclaration',
        'TSInterfaceDeclaration',
        'TSAbstractClassDeclaration',
        'TSModuleDeclaration'];

        var exportedDecls = this.ast.body.filter(function (_ref) {var type = _ref.type,id = _ref.id,declarations = _ref.declarations;return (0, _arrayIncludes2['default'])(declTypes, type) && (
          id && id.name === exportedName || declarations && declarations.find(function (d) {return d.id.name === exportedName;}));});

        if (exportedDecls.length === 0) {
          // Export is not referencing any local declaration, must be re-exporting
          this.exportMap.namespace.set('default', (0, _doc.captureDoc)(this.source, this.docStyleParsers, astNode));
          return;
        }
        if (
        this.isEsModuleInteropTrue // esModuleInterop is on in tsconfig
        && !this.exportMap.namespace.has('default') // and default isn't added already
        ) {
            this.exportMap.namespace.set('default', {}); // add default export
          }
        exportedDecls.forEach(function (decl) {
          if (decl.type === 'TSModuleDeclaration') {
            if (decl.body && decl.body.type === 'TSModuleDeclaration') {
              _this4.exportMap.namespace.set(decl.body.id.name, (0, _doc.captureDoc)(_this4.source, _this4.docStyleParsers, decl.body));
            } else if (decl.body && decl.body.body) {
              decl.body.body.forEach(function (moduleBlockNode) {
                // Export-assignment exports all members in the namespace,
                // explicitly exported or not.
                var namespaceDecl = moduleBlockNode.type === 'ExportNamedDeclaration' ?
                moduleBlockNode.declaration :
                moduleBlockNode;

                if (!namespaceDecl) {
                  // TypeScript can check this for us; we needn't
                } else if (namespaceDecl.type === 'VariableDeclaration') {
                  namespaceDecl.declarations.forEach(function (d) {return (0, _patternCapture2['default'])(d.id, function (id) {return _this4.exportMap.namespace.set(
                      id.name,
                      (0, _doc.captureDoc)(_this4.source, _this4.docStyleParsers, decl, namespaceDecl, moduleBlockNode));});});


                } else {
                  _this4.exportMap.namespace.set(
                  namespaceDecl.id.name,
                  (0, _doc.captureDoc)(_this4.source, _this4.docStyleParsers, moduleBlockNode));
                }
              });
            }
          } else {
            // Export as default
            _this4.exportMap.namespace.set('default', (0, _doc.captureDoc)(_this4.source, _this4.docStyleParsers, decl));
          }
        });
      }return typeScriptExport;}() }]);return ImportExportVisitorBuilder;}();exports['default'] = ImportExportVisitorBuilder;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 18164 characters]