import React from 'react';

import { Helmet } from 'react-helmet';
import { connect } from 'react-redux';
import { Router, Switch } from '@wisdom-utils/runtime';
import { helpers } from '@wisdom-utils/utils';
import defaultSetting from '@wisdom-utils/components/lib/AppLayout/layouts/defaultSettings';
// eslint-disable-next-line import/extensions
import { dyRoutes } from '../../routes/config';
const pkg = require('../../../package.json');
const { renderRoutes } = helpers;
const config = require('../../../config/config');
function App(props) {
  const metaSecurity = /https/.test(window.location.protocol) ? (
    <meta httpEquiv="Content-Security-Policy" content="upgrade-insecure-requests" />
  ) : null;
  return (
    <>
      <Helmet title={`${(props.global && props.global.title) || defaultSetting.title}`}>
        <title>{`${(props.global && props.global.title) || defaultSetting.title}`}</title>
        <link rel="shortcut icon" href={`https://panda-water.cn/web4/${props.global && props.global.shortcutIcon}`} />
        {metaSecurity}
        <meta name="description" content={`${props.global && props.global.title}`} />
      </Helmet>
      <Router basename={config.base || pkg.name.toLocaleLowerCase() || ''}>
        <Switch>
          {renderRoutes(
            'panda-console-base',
            dyRoutes(props.menu || [], props.global && props.global.layout, props.global && props.global.theme || 'dark').routes,
          )}
        </Switch>
      </Router>
    </>
  );
}

const mapStateToProps = state => ({
  global: state.getIn(['global', 'globalConfig']),
  menu: state.getIn(['global', 'menu']),
});
export default connect(
  mapStateToProps,
  null,
)(App);
