import Cookies from 'js-cookie';
import { encrypt } from '@/utils/jsencrypt';

// 30 天
export const expiresTime = 30;

export const TokenKey = 'Admin-Token'

export function getToken() {
  return Cookies.get(TokenKey)
}

export function setToken(token: string) {
  return Cookies.set(TokenKey, token)
}

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

export interface AccountInfo {
  username?: string;
  password?: string;
  rememberMe?: 'true' | 'false';
}

export function getAccount() {
  return {
    username: Cookies.get("username"),
    password: Cookies.get("password"),
    rememberMe: Cookies.get("rememberMe"),
  };
}

export function setAccount(account: AccountInfo) {
  account.username && Cookies.set("username", account.username, { expires: expiresTime});
  account.password && Cookies.set("password", encrypt(account.password) as string, { expires: expiresTime});
  account.rememberMe != null && Cookies.set("rememberMe", account.rememberMe, { expires: expiresTime});
}

export function removeAccount() {
  Cookies.remove("username");
  Cookies.remove("password");
  Cookies.remove("rememberMe");
}
