import Cookies from 'js-cookie';
import { store } from '@wisdom-utils/utils';
import {
  CLEAR_RCENT_KEYWORD,
  COMPLEX_CONFIG,
  COMPLEX_PATHNAME,
  CREATE_CONEXT,
  EXTEND_OPEN_KEY,
  GENERATE_MENU,
  GET_CONFIG,
  GET_CURRENT_INDEX,
  GET_CURRENT_ROUTES,
  GET_ERROR_CONFIG,
  GET_MICRO_MOUNTED_STATUS,
  GET_PATHNAME,
  GET_RCENT_KEYWORD,
  GET_RECENT_PRODUCT,
  GET_RECENT_VISITED,
  GET_THEME_CONFIG,
  LOGIN_MODE,
  LOGINOUT,
  SELECT_MENU_KEY,
  SLIDER_MENU_MODE,
  VALIDATE_AUTH,
  MAP_VIEW,
  AMAP_VIEW,
  PD_VIEW,
} from './constants';
export function getConfig(data) {
  return {
    type: GET_CONFIG,
    data,
  };
}

export function getConfigError(error) {
  return {
    type: GET_ERROR_CONFIG,
    error,
  };
}

export function getCurrentRoute(data) {
  return {
    type: GET_CURRENT_ROUTES,
    data,
  };
}
export function createContext(data) {
  return {
    type: CREATE_CONEXT,
    data,
  };
}

export function generateMenu(data) {
  return {
    type: GENERATE_MENU,
    data,
  };
}

export function updageSetting(data) {
  return {
    type: GET_THEME_CONFIG,
    data,
  };
}

export function updateRecentKeywords(data) {
  return {
    type: GET_RCENT_KEYWORD,
    data,
  };
}

export function updateRecentVisited(data) {
  return {
    type: GET_RECENT_VISITED,
    data,
  };
}

export function updateCurrentIndex(data) {
  return {
    type: GET_CURRENT_INDEX,
    data,
  };
}

export function updateRecentProduct(data) {
  return {
    type: GET_RECENT_PRODUCT,
    data,
  };
}

export function clearRecentProduct() {
  return {
    type: CLEAR_RCENT_KEYWORD,
  };
}

export function updateCollapsed(data) {
  return {
    type: SLIDER_MENU_MODE,
    data,
  };
}

export function changeLoginMode(data) {
  return {
    type: LOGIN_MODE,
    data,
  };
}

export function updateAuthValidate(data) {
  return {
    type: VALIDATE_AUTH,
    data,
  };
}

export function updatePathname(data) {
  return {
    type: GET_PATHNAME,
    data,
  };
}

export function updateSelectedKeys(data) {
  return {
    type: SELECT_MENU_KEY,
    data,
  };
}

export function updateOpenKeys(data) {
  return {
    type: EXTEND_OPEN_KEY,
    data,
  };
}

export function updateComplexConfig(data) {
  return {
    type: COMPLEX_CONFIG,
    data,
  };
}

export function updateComplexPathName(data) {
  return {
    type: COMPLEX_PATHNAME,
    data,
  };
}

export function updateMicroMounted(data) {
  return {
    type: GET_MICRO_MOUNTED_STATUS,
    data,
  };
}

export function updageAMapView(data) {
  return {
    type: AMAP_VIEW,
    data,
  };
}

export function updageMapView(data) {
  return {
    type: MAP_VIEW,
    data,
  };
}

export function updatePdCesiumView(data) {
  return {
    type: PD_VIEW,
    data,
  };
}

export function logout(data) {
  // eslint-disable-next-line no-undef
  createStoreage.remove('PANDA_STORE');
  if (window.globalConfig) {
    Cookies.set(window.globalConfig.client, '', {
      path: '/',
    });
    window.globalConfig.token = null;
    window.globalConfig.access_token = null;
    window.globalConfig.apiGatewayDomain = window.location.origin;
    window.globalConfig.baseURI = window.location.origin;
    window.globalConfig.hasGateWay = false;
    window.globalConfig.userInfo = null;
    window.qiankunIsCache = null;
    window.qiankunStarted = false;
    // window.globalConfig.hasGateWay = ;
  }

  Cookies.set('token', '', {
    path: '/',
  });

  Cookies.set('loginMode', '', {
    path: '/',
  });

  Cookies.set('redirect_state', '', {
    path: '/',
  });
  Cookies.set('site', '', {
    path: '/',
  });
  Cookies.set('singleToken', '', {
    path: '/',
  });
  localStorage.removeItem('access_token');
  localStorage.removeItem('password_token');
  localStorage.removeItem('password_pwdRegex');
  localStorage.removeItem('password_pwdRegexTip');
  localStorage.removeItem('password_needChange');
  // eslint-disable-next-line no-undef,no-restricted-globals
  createStoreage.remove(`__PANDA_STORE__${location.hostname}`);
  // eslint-disable-next-line no-undef,no-restricted-globals
  createStoreage.remove(`__PANDA_STORE__MICRO_${location.hostname}`);
  localStorage.removeItem('JmReport-Access-Token');

  const products =
    window.globalConfig &&
    (window.globalConfig.products || []).map(item => {
      if (item.PackageName === 'civweb4') {
        return 'web4_console';
      }
      return `${item.PackageName}-main`;
    });
  if (products) {
    Object.keys(products).forEach(item => {
      window[products[item]] &&
        window[products[item]] &&
        window[products[item]].unmount &&
        window[products[item]].unmount({ store });
    });
  }
  if (window.refreshCurrentToken) clearInterval(window.refreshCurrentToken);
  store.set('event:dropCache');
  window.location.reload();
  return {
    type: LOGINOUT,
    data: {
      currentMenuIndex: 0,
      complexConfig: {},
      globalConfig: window.globalConfig,
    },
  };
}