const TOKEN_KEY = 'token';
const ISDP_KEY = 'saber-token';

const isLogin = () => {
  return !!localStorage.getItem(TOKEN_KEY);
};

const getToken = () => {
  return localStorage.getItem(TOKEN_KEY);
};

const setToken = (token: string) => {
  localStorage.setItem(TOKEN_KEY, token);
  const obj = {
    dataType: typeof token,
    content: token,
    datetime: new Date().getTime()
  };
  localStorage.setItem(ISDP_KEY, JSON.stringify(obj));
  document.cookie = `saber-access-token=${token};`;
};

const clearToken = () => {
  localStorage.removeItem(TOKEN_KEY);
  localStorage.removeItem(ISDP_KEY);
};

const USER_INFO_KEY = 'userInfo';
export const setSessionUserInfo = data => {
  sessionStorage.setItem(USER_INFO_KEY, JSON.stringify(data));
};
export const getSessionUserInfo = () => {
  const data = sessionStorage.getItem(USER_INFO_KEY);
  return data ? JSON.parse(data) : null;
};

export { isLogin, getToken, setToken, clearToken };
