/*
 * @Author: sjl
 * @Date: 2023-05-07 20:34:06
 * @Descripttion:
 */
import { Persistent, BasicKeys } from '/@/utils/cache/persistent';
import { CacheTypeEnum } from '/@/enums/cacheEnum';
import projectSetting from '/@/settings/projectSetting';
import { JWT_TOKEN_KEY, REFRESH_TOKEN_KEY, LIGONG_TOKEN_KEY } from '/@/enums/cacheEnum';

const { permissionCacheType } = projectSetting;
const isLocal = permissionCacheType === CacheTypeEnum.LOCAL;

export function getAuthCache<T>(key: BasicKeys) {
  const fn = isLocal ? Persistent.getLocal : Persistent.getSession;
  return fn(key) as T;
}

export function setAuthCache(key: BasicKeys, value) {
  const fn = isLocal ? Persistent.setLocal : Persistent.setSession;
  return fn(key, value, true);
}

export function clearAuthCache(immediate = true) {
  const fn = isLocal ? Persistent.clearLocal : Persistent.clearSession;
  return fn(immediate);
}
export function getJwtToken() {
  return getAuthCache(JWT_TOKEN_KEY);
}
export function getLigongToken() {
  return getAuthCache(LIGONG_TOKEN_KEY);
}
export function getRefreshToken() {
  return getAuthCache(REFRESH_TOKEN_KEY);
}
