'use strict';

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

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _qs = require('qs');

var _qs2 = _interopRequireDefault(_qs);

var _home = require('../../bundles/home');

var _home2 = _interopRequireDefault(_home);

var _pageContainer = require('../pageContainer');

var _pageContainer2 = _interopRequireDefault(_pageContainer);

var _ = require('../modules/exception/404');

var _2 = _interopRequireDefault(_);

var _App = require('./App');

var _App2 = _interopRequireDefault(_App);

var _exclusive = require('../pageContainer/exclusive');

var _exclusive2 = _interopRequireDefault(_exclusive);

var _fetch = require('../system/fetch');

var _longRunOpt = require('../system/longRunOpt');

var _CacheService = require('../service/CacheService');

var _StringUtils = require('../utils/StringUtils');

var _config = require('../../config');

var _config2 = _interopRequireDefault(_config);

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

var CONTAINER_PRE = exports.CONTAINER_PRE = _config2.default.ContainerPre || 'page_container';

var getUrlPath = function getUrlPath(url) {
  return url;
};

// 要初始化数据的页面
var getPageInitData = function getPageInitData(nextState, replace, callback) {
  // 是否初始化了

  var domainLink = nextState.params.splat;
  if (domainLink && !_exclusive2.default.some(function (f) {
    return f(domainLink);
  }) && !domainLink.endsWith('.html')) {
    (function () {
      var url = getUrlPath((0, _StringUtils.trimStart)(domainLink, '/'));
      var params = Object.assign(_extends({}, nextState.params), nextState.location.state);
      if (nextState.location) {
        var query = nextState.location.query && nextState.location.query.p && atob(nextState.location.query.p);
        Object.assign(params, _qs2.default.parse(query));
      }
      // TODO: add process menu
      var handler = function handler() {
        return (0, _longRunOpt.longRunExec)(function () {
          return (0, _fetch.PFetch)(url, params).then(function (data) {
            // TODO: 如何保存此时的数据
            (0, _CacheService.savePageData)(data);
            callback();
          }).catch(function (err) {
            (0, _CacheService.savePageData)('EXCEPTION');
            callback();
            throw err;
          });
        });
      };
      if (window.__showLogin) {
        window.__initPage = handler;
      } else {
        handler();
      }
    })();
  } else {
    (0, _CacheService.savePageData)([]);
    callback();
  }
};

/* eslint-disable global-require */
var routes = function routes(homePage) {
  var home = homePage || _home2.default;
  return {
    path: '/',
    component: _App2.default,
    indexRoute: { component: home },
    childRoutes: [{
      path: '/' + CONTAINER_PRE + '/index.html', component: home
    }, {
      path: '/' + CONTAINER_PRE + '/**',
      component: _pageContainer2.default,
      // getComponents(nextState, callback) {
      //   require.ensure([], require => {
      //     callback(null, require('./pageContainer/index').default);
      //   }, 'container');
      // },
      onEnter: getPageInitData
    }, { path: '*', component: _2.default }]
  };
};

exports.default = routes;