import Cookies from "js-cookie"

/* code */
// eslint-disable-next-line prefer-const
export let CODE401 = 403 //没有权限   管乐迷为403  管乐团为5000
export const CODE_ERR_CANCELED = "ERR_CANCELED" // 取消请求的code
export function setCODE401(code: number) {
   CODE401 = code
}

/** cookie */
const TokenKey = "classroom-Token"
const expiresDate = 7

export let TokenInvalidFlag = true //令牌是否有效
export function getToken() {
   return Cookies.get(TokenKey) && "bearer " + Cookies.get(TokenKey)
}

export function setToken(token: string) {
   TokenInvalidFlag = true
   return Cookies.set(TokenKey, token, { expires: expiresDate })
}

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

/**
 * 设置 用户类型
 */

const userType = "userType"

export function getUserType() {
   return Cookies.get(userType) as rolesType | undefined
}

export function setUserType(type: string) {
   return Cookies.set(userType, type, { expires: expiresDate })
}

export function removeUserType() {
   return Cookies.remove(userType)
}

/** 密码加上正则 */
import md5 from "md5"
const code = "plzh@"
export const md5Pwd = (pwd: string) => {
   return md5(code + pwd)
}
