"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.defaultApp = exports.initMicroApps = void 0;

var _qiankun = require("qiankun");

var _micor = _interopRequireDefault(require("../config/micor"));

var _package = _interopRequireDefault(require("../package.json"));

var _constants = require("./utils/constants");

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

var initMicroApps = function initMicroApps(loader) {
  var entrys = process.env.NODE_ENV !== 'production' ? _micor["default"].dev : _micor["default"].prod;
  (0, _qiankun.registerMicroApps)(entrys.map(function (item) {
    item.loader = loader;
    item.props = {
      emitter: window.share.event,
      baseRoot: item.name,
      globalConfig: JSON.stringify(createStoreage.get('globalConfig') || {}),
      XMLHttpRequest: window.XMLHttpRequest
    };
    return item;
  }), {
    beforeLoad: [function (app) {
      // hookRequest()
      // console.log(
      //   '[LifeCycle] before load %c%s',
      //   'color: green;',
      //   JSON.stringify(app),
      // );
      console.log('[LifeCycle] before load %c%s', 'color: green;', app.name);
    }],
    beforeMount: [function (app) {
      // hookRequest()
      // ReactDOM.unmountComponentAtNode(
      //   document.getElementById('subapp-container'),
      // );
      // console.log(
      //   '[LifeCycle] before mount %c%s',
      //   'color: green;',
      //   JSON.stringify(app),
      // );
      // window.share.event.removeAllListeners();
      console.log('[LifeCycle] before mount %c%s', 'color: green;', app.name);
    }],
    afterMount: [function (app) {
      console.log('[LifeCycle] after mount %c%s', 'color: green;', app.name);
    }],
    beforeUnmount: [function (app) {
      window.share.event.removeAllListeners('changeRoute');
      console.log('[LifeCycle] after unmount %c%s', 'color: green;', app.name); // console.log(
      //   '[LifeCycle] before unmount %c%s',
      //   'color: green;',
      //   JSON.stringify(app),
      // );
    }],
    afterUnmount: [function (app) {// console.log(
      //   '[LifeCycle] after unmount %c%s',
      //   'color: green;',
      //   JSON.stringify(app),
      // );
    }]
  });
  defaultApp();
  (0, _qiankun.start)({
    sandbox: {
      experimentalStyleIsolation: true,
      loose: true
    },
    singular: true,
    scopedCSS: true,
    getPublicPath: window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__,
    excludeAssetFilter: function excludeAssetFilter(url) {
      return url.indexOf('webapi.amap.com') !== -1 || url.indexOf('api.map.baidu.com') !== -1 || url.indexOf('map.baidu.com') !== -1 || url.indexOf('pv.sohu.com') !== -1 || url.indexOf('mt0.google.cn') !== -1 || url.indexOf('mt1.google.cn') !== -1;
    }
  });
  (0, _qiankun.runAfterFirstMounted)(function () {
    console.log('[MainApp] first app mounted');
  });
  (0, _qiankun.addGlobalUncaughtErrorHandler)(function (event) {
    return console.log(event);
  });
};

exports.initMicroApps = initMicroApps;

var defaultApp = function defaultApp() {
  var config = createStoreage.get('globalConfig');

  if (config && config.token) {
    // const startWith = config.homepage ? config.homepage.split('/') : [];
    var basePath = _constants.FILTER_FOLER_REG.test(config.homepage) ? 'civweb4' : 'civweb';
    (0, _qiankun.setDefaultMountApp)("/".concat(_package["default"].name.toLocaleLowerCase(), "/").concat(basePath, "/?client=").concat(config.client) //   config.homepage
    //     ? `/${pkg.name.toLocaleLowerCase()}/${basePath}/${config.homepage}`
    //     : `/${pkg.name.toLocaleLowerCase()}/${basePath}/?client=${
    //         window.globalConfig.client
    //       }`,
    );
  }
};

exports.defaultApp = defaultApp;