// react
import React from 'react';
import { render } from 'react-dom';
// react-router
import { createHashHistory } from 'History';
import { Router, Route, useRouterHistory, IndexRedirect } from 'react-router';
// mobx
import { Provider } from 'mobx-react';
// mobx-react-router
import { syncHistoryWithStore, RouterStore } from 'mobx-react-router';

import { WeaForm } from 'comsMobx';
import { WeaAlertPage, WeaLocaleProvider, WeaTools } from 'ecCom';

// components
import Entrance from './router/Entrance';

const getLocaleLabel = WeaLocaleProvider.getLocaleLabel.bind(this, 'common');
// style
import './style';

// stores
const routing = new RouterStore();
const stores = {
  routing,
  weaFormStore: new WeaForm(),
};

const browserHistory = useRouterHistory(createHashHistory)({
  queryKey: '_key',
  basename: '/',
});

const history = syncHistoryWithStore(browserHistory, stores.routing);

const AuthPage = () => (
  <WeaAlertPage iconSize={100} >
    <p>对不起，您暂时没有权限！</p>
  </WeaAlertPage>
);
const onEnter = (nextState, replace, callback) => {
  window.ssoChecked = false;
  WeaTools.checkSSO('', false).then(() => {
    getLocaleLabel(nextState, replace, callback);
  });
};

const Root = () => (
  <Provider {...stores}>
    <Router history={history}>
      <Route key="/" path="/" component={Entrance} onEnter={onEnter}>
        <Route key="auth" path="com/auth" component={AuthPage} />
      </Route>
    </Router>
  </Provider>
);

try {
  render(<Root />, document.getElementById('container'), () => {});
} catch (e) {
  window.console ? console.log('出错了： ', e) : alert(`出错了：${e}`);
}
