// import Cookies from 'js-cookie'

const TokenKey = 'X-Token'
const UserIdKey = "UID"

export function getToken() {
  // return Cookies.get(TokenKey)
  let token = getParam('token') || ""
  if(token){
    token = window.atob(token)
    localStorage.setItem("X-Token",token)
    return token
  }
  return localStorage.getItem(TokenKey)

}

function getParam(name) {
  let search = document.location.search;
  let pattern = new RegExp("[?&]" + name + "\=([^&]+)", "g");
  let matcher = pattern.exec(search);
  let items = null;
  if (null != matcher) {
    try {
      items = decodeURIComponent(decodeURIComponent(matcher[1]));
    } catch (e) {
      try {
        items = decodeURIComponent(matcher[1]);
      } catch (e) {
        items = matcher[1];
      }
    }
  }
  return items;
}

export function setToken(token) {
  // return Cookies.set(TokenKey, token)
  return localStorage.setItem(TokenKey,token)
}
export function getUserId() {
  return localStorage.getItem(UserIdKey)
}

export function setUserId(UserId) {
  return localStorage.setItem(UserIdKey, UserId)
}

export function removeToken() {
  // return Cookies.remove(TokenKey)
  return localStorage.removeItem(TokenKey)
}
