'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports['default'] = getRoutesPath;

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _nunjucks = require('nunjucks');

var _nunjucks2 = _interopRequireDefault(_nunjucks);

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

var _context2 = _interopRequireDefault(_context);

var _escapeWinPath = require('./escapeWinPath');

var _escapeWinPath2 = _interopRequireDefault(_escapeWinPath);

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

var _getPackageRoute2 = _interopRequireDefault(_getPackageRoute);

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

var routesTemplate = _fs2['default'].readFileSync(_path2['default'].join(__dirname, '../../nunjucks/routes.nunjucks.js')).toString();

function getRoutesPath(packageInfo, configEntryName) {
  var tmpDirPath = _context2['default'].tmpDirPath,
      isDev = _context2['default'].isDev,
      _context$choerodonCon = _context2['default'].choerodonConfig,
      routes = _context$choerodonCon.routes,
      homePath = _context$choerodonCon.homePath;

  var configRoutes = routes || (0, _getPackageRoute2['default'])(packageInfo);
  var routesPath = _path2['default'].join(tmpDirPath, 'routes.' + configEntryName + '.js');
  _nunjucks2['default'].configure(routesPath, { autoescape: false });
  var homePathStr = 'createHome("/", ' + (homePath ? 'function() { return import("' + (0, _escapeWinPath2['default'])(_path2['default'].join(process.cwd(), homePath || '')) + '"); }' : null) + ', ' + (!homePath ? homePath : 'CHOERODON_HAP_FRONT_BOOT_HOME_PAGE') + ')';
  _fs2['default'].writeFileSync(routesPath, _nunjucks2['default'].renderString(routesTemplate, {
    routes: Object.keys(configRoutes).map(function (key) {
      return 'createRoute("/' + key + '", function() { return import("' + (0, _escapeWinPath2['default'])(_path2['default'].join(process.cwd(), configRoutes[key])) + '"); }, "' + key + '")';
    }).join(',\n'),
    home: homePathStr,
    source: isDev ? 'src' : 'lib'
  }));
  return routesPath;
}