import Cookies from 'js-cookie'
const TokenKey = 'Devcould-Console-Token'
const UserKey = 'Devcould-Console-User'
const LevelKey = 'Devcould-Console-Level'
const BtnKey = 'Devcould-Console-Btn'

import CryptoJS from "crypto-js";
import config from "@/config/index";
import router from '../router'

export function getToken() {
  return localStorage.getItem(TokenKey)
}

export function setToken(token) {
  return localStorage.setItem(TokenKey, token)
}

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


export function getUser() {
  return Cookies.get(UserKey)
}

export function setUser(token) {
  return Cookies.set(UserKey, token)
}

export function removeUser() {
  return Cookies.remove(UserKey)
}


export function getLevel() {
  return Cookies.get(LevelKey)
}

export function setLevel(token) {
  return Cookies.set(LevelKey, token)
}

export function removeLevel() {
  return Cookies.remove(LevelKey)
}
export function getBtn() {
  const btn = Cookies.get(BtnKey) ?? "";
    if (btn == "") {
      return [];
    } else {
      return JSON.parse(btn);
    }
}

export function setBtn(has) {
  return Cookies.set(BtnKey, has)
}

export function removeBtn() {
  return Cookies.remove(BtnKey)
}


// function changeToken(token) {
//   if (!token) return
//   const encJson = CryptoJS.AES.encrypt(JSON.stringify({ token }), config.secretKey)
//   const encData = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(encJson))
//   window.location.path = `/?t=${encData}`
//   // router.push('/?t=' + encData)
// }

// function listener(event) {
//   const { key, newValue } = event
//   switch (key) {
//     case TokenKey:
//       changeToken(newValue)
//       break
//   }
// }

// export const registerListenerChangeEvent = () => window.addEventListener('storage', listener)





