"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"), _typeof = require("@babel/runtime/helpers/typeof"), _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")), _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")), _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")), _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")), _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")), _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")), _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")), _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")), _react = _interopRequireWildcard(require("react")), _reactDom = require("react-dom"), _reactRedux = require("react-redux"), _rayProgress = _interopRequireDefault(require("ray-progress")), _amosReactRouter = require("amos-react-router"), _reactRouterRedux = require("react-router-redux"), _amosTool = require("amos-tool"), _amosFramework = require("amos-framework"), endConf = _interopRequireWildcard(require("amos-processor/lib/config/endconf")), _consts = _interopRequireDefault(require("amos-processor/lib/config/consts")), _asyncRoutes = _interopRequireDefault(require("./../routes/asyncRoutes")), _store = _interopRequireDefault(require("./../store")), _securityService = require("../services/securityService");

function _getRequireWildcardCache(e) {
  if ("function" != typeof WeakMap) return null;
  var r = new WeakMap, t = new WeakMap;
  return (_getRequireWildcardCache = function(e) {
    return e ? t : r;
  })(e);
}

function _interopRequireWildcard(e, r) {
  if (!r && e && e.__esModule) return e;
  if (null === e || "object" !== _typeof(e) && "function" != typeof e) return {
    default: e
  };
  var t = _getRequireWildcardCache(r);
  if (t && t.has(e)) return t.get(e);
  var o = {}, i = Object.defineProperty && Object.getOwnPropertyDescriptor;
  for (var a in e) if ("default" !== a && Object.prototype.hasOwnProperty.call(e, a)) {
    var u = i ? Object.getOwnPropertyDescriptor(e, a) : null;
    u && (u.get || u.set) ? Object.defineProperty(o, a, u) : o[a] = e[a];
  }
  return o.default = e, t && t.set(e, o), o;
}

function _createSuper(e) {
  var r = _isNativeReflectConstruct();
  return function() {
    var t, o = (0, _getPrototypeOf2.default)(e);
    if (r) {
      var i = (0, _getPrototypeOf2.default)(this).constructor;
      t = Reflect.construct(o, arguments, i);
    } else t = o.apply(this, arguments);
    return (0, _possibleConstructorReturn2.default)(this, t);
  };
}

function _isNativeReflectConstruct() {
  if ("undefined" == typeof Reflect || !Reflect.construct) return !1;
  if (Reflect.construct.sham) return !1;
  if ("function" == typeof Proxy) return !0;
  try {
    return Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], (function() {}))), 
    !0;
  } catch (e) {
    return !1;
  }
}

require("./../styles");

var store = (0, _store.default)(), lsTool = _amosTool.Store.lsTool, AmosConfig = endConf.AmosConfig, loginURI = AmosConfig.httpURI.loginURI, history = (0, 
_reactRouterRedux.syncHistoryWithStore)(_amosReactRouter.browserHistory, store), support = (0, 
_amosTool.browerSupport)([ "firefox/", "chrome/" ], {
  "firefox/": {
    limit: _amosTool.consts.GREATER_EQUAL,
    version: 50
  },
  "chrome/": {
    limit: _amosTool.consts.GREATER_EQUAL,
    version: 55
  }
}), App = function(e) {
  (0, _inherits2.default)(t, e);
  var r = _createSuper(t);
  function t(e) {
    var o;
    return (0, _classCallCheck2.default)(this, t), o = r.call(this, e), (0, _defineProperty2.default)((0, 
    _assertThisInitialized2.default)(o), "onUpdate", (function() {
      _rayProgress.default.done(), window.scrollTo(0, 0);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(o), "filterSystemMenus", (function() {
      return (arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : []).filter((function(e) {
        return "MENU" === e.permissionType;
      }));
    })), o.state = {
      rootRoutes: (0, _asyncRoutes.default)(),
      visible: !1
    }, o;
  }
  return (0, _createClass2.default)(t, [ {
    key: "componentWillMount",
    value: function() {
      var e = this, r = _amosTool.LocationParam.getLocationParamByName("token");
      if (!r) {
        var t = JSON.parse(lsTool.read("systemMenu"));
        return t ? void this.setState({
          rootRoutes: (0, _asyncRoutes.default)(t),
          visible: !0
        }) : void (window.location.href = loginURI);
      }
      lsTool.write(_consts.default.token, r), (0, _securityService.getSystemAllMenu)(r).then((function(r) {
        var t = e.filterSystemMenus(r), o = [];
        t.forEach((function(e) {
          e.children && e.children.length > 0 && (o = [].concat((0, _toConsumableArray2.default)(o), (0, 
          _toConsumableArray2.default)(e.children)));
        }));
        (0, _asyncRoutes.default)(o);
        e.setState({
          rootRoutes: (0, _asyncRoutes.default)(o),
          visible: !0
        }), lsTool.write("systemMenu", JSON.stringify(o));
      }), (function(e) {
        _amosFramework.AmosAlert.error("警告", e || "获取初始化菜单失败!");
      }));
    }
  }, {
    key: "render",
    value: function() {
      var e = this;
      return this.state.visible ? _react.default.createElement(_reactRedux.Provider, {
        store: store
      }, _react.default.createElement(_amosReactRouter.Router, {
        history: history,
        routes: this.state.rootRoutes,
        onUpdate: function() {
          return e.onUpdate();
        }
      })) : null;
    }
  } ]), t;
}(_react.Component);

App.displayName = "App";

var main = support ? _react.default.createElement(App, null) : _react.default.createElement(_amosFramework.DownloadBrowsers, null);

(0, _reactDom.render)(main, document.getElementById("app"));