"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.cleanCache = cleanCache;
exports.generateExports = generateExports;
exports.default = _default;

function _react() {
  const data = _interopRequireDefault(require("react"));

  _react = function _react() {
    return data;
  };

  return data;
}

function _assert() {
  const data = _interopRequireDefault(require("assert"));

  _assert = function _assert() {
    return data;
  };

  return data;
}

function _lodash() {
  const data = require("lodash");

  _lodash = function _lodash() {
    return data;
  };

  return data;
}

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

const reserveLibrarys = ['umi']; // reserve library

const reserveExportsNames = ['Link', 'NavLink', 'Redirect', 'dynamic', 'router', 'withRouter', 'Route']; // reserve name

let umiExportsHook = {}; // repeated definition

function cleanCache() {
  umiExportsHook = {};
}

function generateExports(item) {
  (0, _assert().default)(item.source, 'source should be supplied.');
  (0, _assert().default)(item.exportAll || item.specifiers, 'exportAll or specifiers should be supplied.');
  (0, _assert().default)(!reserveLibrarys.includes(item.source), `${item.source} is reserve library, Please don't use it.`);

  if (item.exportAll) {
    return `export * from '${item.source}';`;
  }

  (0, _assert().default)(Array.isArray(item.specifiers), `specifiers should be Array, but got ${item.specifiers.toString()}.`);
  const specifiersStrArr = item.specifiers.map(specifier => {
    if (typeof specifier === 'string') {
      (0, _assert().default)(!reserveExportsNames.includes(specifier), `${specifier} is reserve name, you can use 'exported' to set alias.`);
      (0, _assert().default)(!umiExportsHook[specifier], `${specifier} is Defined, you can use 'exported' to set alias.`);
      umiExportsHook[specifier] = true;
      return specifier;
    } else {
      (0, _assert().default)((0, _lodash().isPlainObject)(specifier), `Configure item context should be Plain Object, but got ${specifier}.`);
      (0, _assert().default)(specifier.local && specifier.exported, 'local and exported should be supplied.');
      return `${specifier.local} as ${specifier.exported}`;
    }
  });
  return `export { ${specifiersStrArr.join(', ')} } from '${item.source}';`;
}

function _default(api) {
  api.onGenerateFiles(() => {
    generateUmiExports();
  });

  const generateUmiExports = () => {
    cleanCache();
    const umiExports = api.applyPlugins('addUmiExports', {
      initialValue: []
    }).map(generateExports);
    api.writeTmpFile('umiExports.ts', umiExports.join('\n'));
  };
}