const TOKEN_KEY = 'token';
const TOKEN_FROM_KEY = 'tokenFrom';

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

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

const setToken = (token: string) => {
  localStorage.setItem(TOKEN_KEY, token);
};

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

const setTokenFrom = (from: string) => {
  localStorage.setItem(TOKEN_FROM_KEY, from);
};

const getTokenFrom = () => {
  return localStorage.getItem(TOKEN_FROM_KEY);
};

const checkTokenFromIframe = (): boolean => {
  return getTokenFrom() === 'iframe';
};

const checkTokenFromLogin = (): boolean => {
  return getTokenFrom() === 'login';
};

const clearTokenFrom = () => {
  localStorage.removeItem(TOKEN_FROM_KEY);
};

export {
  isLogin,
  getToken,
  setToken,
  clearToken,
  setTokenFrom,
  getTokenFrom,
  checkTokenFromIframe,
  checkTokenFromLogin
};
