import { appService } from './api';
import { initMicroApps } from './micro';
import { params, Storeage } from 'kit_utils';
import { actionCreators } from './containers/App/store';
import { getToken, isString } from './utils/utils';

const namespace = `__PANDA_STORE__${location.hostname}`;
window.createStoreage = new Storeage(namespace);

export const initGlobalConfig = (loader, store, render) => {
    // eslint-disable-next-line no-debugger
    // eslint-disable-next-line no-undef
    let config = createStoreage.get('globalConfig') || {};
    store.dispatch(actionCreators.updateComplexConfig({}));
    if (!getToken() || config.token == null) {
      // eslint-disable-next-line no-undef
      createStoreage.remove(namespace);
      config = {};
    }
    if (!params.getParams('client', window.location.search) && config) {
      // eslint-disable-next-line no-undef
      createStoreage.remove(namespace);
    }
  
    // eslint-disable-next-line no-undef
    if (!createStoreage.get('globalConfig')) {
      window.createStoreage = new Storeage(namespace);
    }
    if (!getToken()) {
      localStorage.removeItem('loginSite');
    }
  
    if (config.token !== null && Object.keys(config).length > 0) {
      store.dispatch(actionCreators.getConfig(config));
      // render({ appContent: '', loading: true });
      initMicroApps(loader, store);
      if (config.isNewYear) {
        updateTheme('#ff9600');
      }
    } else {
      appService.getWateWayConfig().then(res => {
        const hasGateWay =
          res && res.data && isString(res.data) ? JSON.parse(res.data) : res.data;
        if (res.code === 0 && res.data === 'true') {
          store.dispatch(
            actionCreators.getConfig(
              Object.assign({}, window.globalConfig, {
                hasGateWay,
                apiGatewayDomain: `${window.location.origin}/PandaCore`,
              }),
            ),
          );
        }
      });
      // eslint-disable-next-line react-hooks/rules-of-hooks
      const client = params.getParams('client')
    appService
        .queryConfig({
          client: client,
        })
        .then(res => {
          if (res) {
            
            const data = res;
            if (!data.client) {
              data.client = params.getParams('client') || 'city';
            }
            // Cookies.set('city', data.client);
            store.dispatch(
              actionCreators.getConfig(
                Object.assign({}, window.globalConfig, data),
              ),
            );
            
            //Cookies.set('city', params.getParams('client'))
  
            if (data.loginTemplate === '新春 - 智联.html') {
              updateTheme('#ff9600');
            }
            // eslint-disable-next-line no-new
            if (getToken()) {
              // eslint-disable-next-line no-new
              new Login(
                {
                  global: Object.assign({}, data, {
                    token: getToken(),
                  }),
                  // eslint-disable-next-line no-shadow
                  updateConfig: data =>
                    store.dispatch(actionCreators.getConfig(data)),
                  isInit: false,
                  logout: () => store.dispatch(actionCreators.logout()),
                },
                () => {
                  (async () => {
                    (await (getToken() &&
                      window.globalConfig &&
                      window.globalConfig.token)) && initMicroApps(loader, store);
                  })();
                },
                true,
              );
            }
  
            // eslint-disable-next-line no-shadow
          }
          return res;
        })
        // eslint-disable-next-line no-shadow
        .then(res => {
          // eslint-disable-next-line no-use-before-define
          // initSensorType();
          // eslint-disable-next-line no-use-before-define
          // initIsMock();
          render({ appContent: '', loading: true });
        })
        .catch(error => {
          console.log(error);
          store.dispatch(actionCreators.getConfigError(error));
        });
    }
  };