import 'whatwg-fetch';
import './public-path'
import '!file-loader?name=[name].[ext]!./images/favicon.ico';
import './global.less';
// import '@babel/polyfill';
import 'animate.css/animate.css';
import 'antd/dist/antd.less';
import 'file-loader?name=.htaccess!./.htaccess'; // eslint-disable-line import/extensions
import 'kit_utils/lib/format';
import 'sanitize.css/sanitize.css';
// import './locales/zh-CN';
// import './loader';
// import './vm';
import React from 'react';
import ReactDOM from 'react-dom';
import { event } from 'microser-data';
import _ from 'lodash';
import { Button, ConfigProvider, message, notification } from 'antd';
import { ConnectedRouter } from 'connected-react-router/immutable';
import Immutable from 'immutable';
import { params, Storeage } from 'kit_utils';
import { Provider } from 'react-redux';
import { history } from '@wisdom-utils/runtime';
import ErrorBoundary from './components/ErrorBoundary';
// import themePluginConfig from '../config/themePluginConfig';
import { appService } from './api';
import { updateTheme } from './common/helpers/settingColor';
import Container from './components/Container';
import configureStore from './configureStore';
import App from './containers/App';
import { actionCreators } from './containers/App/store';
import { LocaleContainer } from './locales/locale';
import { initMicroApps } from './micro';

import { initGlobalConfig } from './initConfig'
import './utils/event';
import Cookies from 'js-cookie';
// eslint-disable-next-line no-restricted-globals
const namespace = `__PANDA_STORE__${location.hostname}`;
window.createStoreage = new Storeage(namespace);
// eslint-disable-next-line no-underscore-dangle
const initialState = Immutable.Map();
const store = configureStore(initialState, history);
const MOUNT_NODE = document.getElementById('root');
ConfigProvider.config({
  prefixCls: 'panda-console-base'
});
const render = () => {
  // eslint-disable-next-line react-hooks/rules-of-hooks
  ReactDOM.render(
    <Provider store={store}>
      <ConnectedRouter history={history}>
        <LocaleContainer>
          <ConfigProvider prefixCls="panda-console-base">
            <ErrorBoundary>
              <Container>
                <App />
              </Container>
            </ErrorBoundary>
          </ConfigProvider>
        </LocaleContainer>
      </ConnectedRouter>
    </Provider>,
    MOUNT_NODE,
  );
};
// updateTheme('#ff9600');
const loader = (appContent, loading) => render({ appContent, loading });

const initLocale = () => {
  localStorage.setItem('umi_locale', 'zh-CN');
};
const PRODUCT_NAME = ['civ_water', 'civ_monitor'];


const initSensorType = () => {
  let filterProduct =
    (window.globalConfig && window.globalConfig.products) || [];
  filterProduct = filterProduct.filter(item =>
    PRODUCT_NAME.includes(item.PackageName),
  );
  if (filterProduct.length > 0) {
    appService.getSensorType().then(res => {
      store.dispatch(
        actionCreators.getConfig(
          Object.assign({}, window.globalConfig, {
            sensorType: res.data,
          }),
        ),
      );
    });
  }
};

const initIsMock = () => {
  appService
    .sysConfiguration({
      moduleName: '是否mock数据',
    })
    .then(res => {
      store.dispatch(
        actionCreators.getConfig(
          Object.assign({}, window.globalConfig, {
            isMock: res.data === '是',
          }),
        ),
      );
    });
};
initGlobalConfig(loader, store, render);
initLocale();
window.share && window.share.event && window.share.event.on('triggerMicro', () => {
    initMicroApps(loader, store);
});

const unlisten = history.listen(location => {
  if(location.pathname.indexOf('/civbase/user/login') > -1) {
    initGlobalConfig(loader, store, render);
  }
})

event.on('event:logout', () => {
  store.dispatch(actionCreators.logout())
});
