import Cookies from 'js-cookie';

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,
} 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 updageMapView(data) {
  return {
    type: MAP_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: '/',
    });
  }

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

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

  Cookies.set('redirect_state', '', {
    path: '/',
  });
  Cookies.set('site', '', {
    path: '/',
  });
  localStorage.removeItem('access_token');
  // eslint-disable-next-line no-undef,no-restricted-globals
  createStoreage.remove(`__PANDA_STORE__${location.hostname}`);
  return {
    type: LOGINOUT,
    data: {
      currentMenuIndex: 0,
      complexConfig: {},
    },
  };
}
