import { fromJS } from 'immutable';

import defaultSetting from '../../../../config/defaultSetting';
import generRoutes from '../../../utils/routes';
import {
  CREATE_CONEXT,
  GENERATE_MENU,
  GET_CONFIG,
  GET_CURRENT_ROUTES,
  GET_ERROR_CONFIG,
  GET_THEME_CONFIG,
} from './constants';

export const initialState = fromJS({
  globalConfig: {},
  globalConfigError: '',
  currentRoutes: {},
  instance: null,
  menu: [],
  defaultSetting: defaultSetting,
});

/* eslint-disable default-case, no-param-reassign */
const appReducer = (state = initialState, action) => {
  switch (action.type) {
    case GET_CONFIG:
      
      console.log('updateConfig', action.data);
      window.__INITIAL_STATE__ = Object.assign({}, action.data, {
        allWidgets: action.data.widgets || [],
      });
      window.globalConfig = require('kit_global_config').default;
      createStoreage.set('globalConfig', window.globalConfig);

      const config = createStoreage.get('globalConfig');
      return state.merge({
        globalConfig: config,
        menu: generRoutes(config.widgets || []),
      });
    case GET_ERROR_CONFIG:
      return state.merge({
        globalConfigError: action.data,
      });
    case GET_CURRENT_ROUTES:
      return state.merge({
        currentRoutes: action.data,
      });
    case CREATE_CONEXT:
      return state.merge({
        instance: action.data,
      });
    case GENERATE_MENU:
      return state.merge({
        menu: action.data,
      });
    case GET_THEME_CONFIG:
      return state.merge({
        defaultSetting: action.data,
      });
    default:
      return state;
  }
};
export default appReducer;
