import Cookies from 'js-cookie'
import * as crypto from '@/utils/crypto'

const TokenKey = 'EMP-Token'
const PubKey = 'EMP-PK'

export function getToken() {
  var token = Cookies.get(TokenKey)
  var pubKey = getPubKey()
  return token ? crypto.decryptAES(token, pubKey) : token
}

export function setToken(token) {
  var pubKey = getPubKey()
  return Cookies.set(TokenKey, crypto.encryptAES(token, pubKey))
}

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

export function getPubKey() {
  var pubKey = Cookies.get(PubKey)
  return pubKey ? crypto.decryptAES(pubKey) : pubKey
}

export function setPubKey(key) {
  return Cookies.set(PubKey, crypto.encryptAES(key))
}

export function removePubKey() {
  return Cookies.remove(PubKey)
}
