'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

exports['default'] = installSubmoduleDependencies;

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _getPackagePath = require('./getPackagePath');

var _getPackagePath2 = _interopRequireDefault(_getPackagePath);

var _getPackageRoute = require('./getPackageRoute');

var _getPackageRoute2 = _interopRequireDefault(_getPackageRoute);

var _context = require('./context');

var _context2 = _interopRequireDefault(_context);

var _getAllSubModules = require('./getAllSubModules');

var _getAllSubModules2 = _interopRequireDefault(_getAllSubModules);

var _install = require('./install');

var _install2 = _interopRequireDefault(_install);

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

function getDependenciesByModules(dependencies) {
  var choerodonConfig = _context2['default'].choerodonConfig;

  var deps = {};
  var cmps = (0, _getAllSubModules2['default'])();
  var routes = cmps.reduce(function (obj, module) {
    var packageInfo = require((0, _getPackagePath2['default'])('./target/generate-react/' + module));
    (0, _extends3['default'])(deps, packageInfo[dependencies]);
    return (0, _extends3['default'])(obj, (0, _getPackageRoute2['default'])(packageInfo, './target/generate-react/' + module));
  }, {});

  var mainPackagePath = (0, _getPackagePath2['default'])();
  var mainPackage = require(mainPackagePath);
  var main = mainPackage.main,
      name = mainPackage.name;
  // routes[name.slice(name.lastIndexOf('-') + 1)] = path.join('.', main);

  routes[name] = _path2['default'].join('.', main);

  if (!choerodonConfig.routes) {
    choerodonConfig.routes = routes;
  }
  return deps;
}

function installSubmoduleDependencies(cb) {
  var mainPackagePath = (0, _getPackagePath2['default'])();
  var mainPackage = require(mainPackagePath);

  mainPackage.dependencies = (0, _extends3['default'])(getDependenciesByModules('dependencies'), mainPackage.dependencies);
  mainPackage.peerDependencies = (0, _extends3['default'])(getDependenciesByModules('peerDependencies'), mainPackage.peerDependencies);

  _fs2['default'].writeFileSync(mainPackagePath, JSON.stringify(mainPackage));
  (0, _install2['default'])(function () {
    return cb(mainPackage);
  });
}