// use localStorage to store the authority info, which might be sent from server in actual project.
export function getAuthority() {
  const authorityString = localStorage.getItem('antd-pro-authority') ? localStorage.getItem('antd-pro-authority'):  "[]";
  let authority;
  try {
    authority = JSON.parse(authorityString);
  } catch (e) {
    authority = [];
  }
  return authority;
}

export function setAuthority(authority) {
  return localStorage.setItem('antd-pro-authority', JSON.stringify(Object.getOwnPropertyNames(authority)));
}

export function setCurrentUser(user) {
  return localStorage.setItem('currentUser', JSON.stringify(user));
}

export function getCurrentUser() {
  let currentUser;
  try {
    currentUser = JSON.parse(localStorage.getItem('currentUser'));
  } catch (e) {
    currentUser = {};
  }
  return currentUser;
}

export function hasAuthority(func){
  const currentUser = getCurrentUser();
  if(currentUser && currentUser.IsSuper)
    return true;
  const funcs = getAuthority()
  for(var i=0; i<funcs.length;i++){
    if(funcs[i] === func)
      return true;
  }
  return false;
}

export function hasfunctionAuthority(authority,item){
  if(authority === undefined || authority === null) return ;
  const individual = Object.keys(authority).filter(other => other === item)[0];
  if (individual) return true;
}

export function hasfunctionArrayAuthority(authority,item){
  if(authority === undefined || authority === null) return ;
  const individual = authority.filter((each) => each === item);
  if (Number(individual) !== 0) return true;
}

